[英]How do I marshal a pointer to a pointer to an int?
Umanaged 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.