2013-07-12 15 views

risposta

5

È possibile dichiarare la funzione in questo modo:

[DllImport("example.dll")] 
static extern int foo(IntPtr New_Message_Pointer) 

chiamare questa funzione e passare il puntatore a int array per esempio, è possibile utilizzare codice seguente:

Int32[] intArray = new Int32[5] { 0, 1, 2, 3, 4, 5 }; 

// Allocate unmamaged memory 
IntPtr pUnmanagedBuffer = (IntPtr)Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Int32)) * intArray.Length); 

// Copy data to unmanaged buffer 
Marshal.Copy(intArray, 0, pUnmanagedBuffer, intArray.Length); 

// Pin object to create fixed address 
GCHandle handle = GCHandle.Alloc(pUnmanagedBuffer, GCHandleType.Pinned); 
IntPtr ppUnmanagedBuffer = (IntPtr)handle.AddrOfPinnedObject(); 

e quindi passare ppUnmanagedBuffer alla funzione :

foo(ppUnmanagedBuffer); 
1

Ti vuoi che sia

static extern int foo(IntPtr New_Message_Pointer) 

La parte più difficile sarebbe probabilmente che cosa fare con esso una volta che avete che IntPtr ...

Si potrebbe desiderare di prendere una guarda a this question from SO, che si occupa del puntatore a un puntatore a una struct. È diverso, ma potrebbe farti andare nella giusta direzione.

Problemi correlati