[英]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.