kernel32

Zero_Base

Mitglied
Hallo,


Ich arbeite an eine Anwendung, die shared memory auslesen soll. Um das via Memory Mapped File zu machen, müssen viele aufrufe von kernel32.dll implementiert werden.

Hier ist eine der Funktionen (in VB):
Private Declare Sub CopyMemoryRead Lib "kernel32" Alias "RtlMoveMemory" ( _
pDst As Any, _
ByVal Src As Long, _
ByVal ByteLen As Long)

Wie kann man diesen Aufruf in C# umschreiben?

Mein versuch funktioniert leider nicht, da ich nicht weiß was man an stelle "As any" in C# nehmen soll
[DllImport ("kernel32", SetLastError=true)]
public static extern void CopyMemoryRead (long Dst , long pSrc,long ByteLen);

Vielen Dank,

Zero_Base
 
Fang selber auch grad mit der Win32 API an. Hab gestern aber einen heißen Tipp vom Alex bekommen,
wo man sich diesbezüglich informieren kann.
Guck mal hier rein: [thread=240853]Win32 API - Handle auf ein String Objekt? - Thread[/thread]

Pinvoke lässt sich auch in das VS 2005 integrieren... afaik

MfG,
cosmo
 
Es klappt aber trotzdem nicht..........


public static extern void CopyMemoryRead (IntPtr Dst, IntPtr pSrc, long ByteLen);

[StructLayout(LayoutKind.Sequential,Pack = 1, CharSet = CharSet.Unicode)]
public struct MyData
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=11)]
public char[] strName;
[MarshalAs( UnmanagedType.ByValArray,SizeConst = 16)]
public char[] strID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
public char[] strDatum;
}

IntPtr addressOfStructure1 = Marshal.AllocHGlobal(40);

CopyMemoryRead (Marshal.StructureToPtr(this.myData,this.addressOfStructure1,true), myMBMMem, 1024);



Dabei kriege ich ein Fehler: void kann nicht zu einem IntPtr konvertiert werden. Wie soll ich meine Structur in IntPtr umwandeln? was mache ich falsh?

Vielen Dank in voraus,

Zero_Base
 
Hi.

Die Methode Marshal.StructureToPtr gibt nichts (void) zurück.

Aus dem MSDN http://windowssdk.msdn.microsoft.co...rvices_Marshal_StructureToPtr_2_ffb40adb.asp:
Code:
public struct Point
{
    public int x;
    public int y;
}

class Example
{

    static void Main()
    {

        // Create a point struct.
        Point p;
        p.x = 1;
        p.y = 1;

        Console.WriteLine("The value of first point is " + p.x + " and " + p.y + ".");

        // Initialize unmanged memory to hold the struct.
        IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(p));

        try
        {

            // Copy the struct to unmanaged memory.
            Marshal.StructureToPtr(p, pnt, false);

            // Create another point.
            Point anotherP;

            // Set this Point to the value of the 
            // Point in unmanaged memory. 
            anotherP = (Point)Marshal.PtrToStructure(pnt, typeof(Point));

            Console.WriteLine("The value of new point is " + anotherP.x + " and " + anotherP.y + ".");

        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
Der 3. Parameter gibt übrigens an ob das alte Objekt gelöscht werden soll oder nicht.

Gruß
 
Vielen Dank für die Antwort, deepthroat!!

Es funktioniert leider auch nicht:

IntPtr buffer= Marshal.AllocHGlobal(Marshal.SizeOf(myData));
Point p;
Marshal.StructureToPtr(this.myData,buffer,true);
p=(Point)Marshal.PtrToStructure(buffer, typeof(MyData));


Bei der letzten Zeile kriege ich eine Fehlermeldung : Specified cast is not valid.

Zero_Base
 
Das war doch nur ein Beispiel direkt aus dem MSDN. Du mußt natürlich überall statt Point deine Struktur MyData da einsetzen:
Code:
IntPtr buffer= Marshal.AllocHGlobal(Marshal.SizeOf(myData));
Marshal.StructureToPtr(this.myData,buffer,true);

myData = (MyData)Marshal.PtrToStructure(buffer, typeof(MyData));
Marshal.FreeHGlobal(buffer);

Gruß
 
Kriege immer noch ein Fehler: :(


CopyMemoryRead (myData, myMBMMem, buffer.ToInt32());

Fehlermeldung sagt aus, dass myData kann nicht zu System.IntPtr umgewandelt wetden :(
 

Neue Beiträge

Zurück