非托管 C++:
int foo(int ** New_Message_Pointer);
我如何将其编组为 C#?
[DllImport("example.dll")]
static extern int foo( ???);
非托管 C++:
int foo(int ** New_Message_Pointer);
我如何将其编组为 C#?
[DllImport("example.dll")]
static extern int foo( ???);
您可以像这样声明函数:
[DllImport("example.dll")]
static extern int foo(IntPtr New_Message_Pointer)
要调用此函数并将指针传递给 int 数组,例如,您可以使用以下代码:
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();
然后将 ppUnmanagedBuffer 传递给您的函数:
foo(ppUnmanagedBuffer);
你会希望它成为
static extern int foo(IntPtr New_Message_Pointer)
一旦你拥有了 IntPtr,困难的部分可能就是如何处理它......
您可能想看看SO 中的这个问题,它处理指向结构的指针。这是不同的,但可能会让你朝着正确的方向前进。