5

Umanaged C++:

int  foo(int **    New_Message_Pointer);

How do I marshall this to C#?

[DllImport("example.dll")]
static extern int foo( ???);
Tim
  • 14,999
  • 1
  • 45
  • 68
user2134127
  • 135
  • 1
  • 2
  • 9

2 Answers2

5

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);
SergeyIL
  • 575
  • 5
  • 11
1

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.

Community
  • 1
  • 1
Tim
  • 14,999
  • 1
  • 45
  • 68