Reputation: 135
Umanaged C++:
int foo(int ** New_Message_Pointer);
How do I marshall this to C#?
[DllImport("example.dll")]
static extern int foo( ???);
Upvotes: 5
Views: 1944
Reputation: 595
You can declare function like this:
[DllImport("example.dll")]
static extern int foo(IntPtr New_Message_Pointer)
To call this function and pass pointer to int array for e.g you can use following code:
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();
And then pass ppUnmanagedBuffer to your function:
foo(ppUnmanagedBuffer);
Upvotes: 5
Reputation: 15247
You'll want it to be
static extern int foo(IntPtr New_Message_Pointer)
The hard part would probably be what to do with it once you have that IntPtr...
You might want to take a look at this question from SO, which deals with pointer-to-a-pointer-to-a-struct. It's different, but may get you going in the right direction.
Upvotes: 1