作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
非托管 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);
关于c# - 如何将指针编码为指向 int 的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17619307/
我是一名优秀的程序员,十分优秀!