在 C# 中封送 IntPtr 数组
我想从 C# 中的安全托管代码调用接收指针数组 (void**) 的 C API 中的函数。
我有相应的 IntPtr 对象托管数组,但 MSDN 文档中宣传的 Marshal 方法似乎不足以向具有正确内容的非托管内存块提供 IntPtr。
我本来希望通过“Marshal.AllocHGlobal”获得一个 IntPtr,然后使用“Marshal.Copy”分配正确的内容,但似乎该函数尚未针对 IntPtr 数组进行重载。
关于最好的方法有什么想法吗?
提前致谢。
From safe, managed code in C#, I would like to call a function in a C API that receives an array of pointers (void**).
I have the corresponding managed array of IntPtr objects, but the Marshal methods advertised in the documentation at MSDN do not seem sufficient to provide an IntPtr to an unmanaged block of memory with the correct content.
I had hoped to obtain an IntPtr with 'Marshal.AllocHGlobal' and then assign the correct content using 'Marshal.Copy', but it seems the function has not been overloaded for an array of IntPtr.
Any thoughts on the best way to do this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
P/Invoke 编组器已完成此操作,您无需提供帮助。只需将函数参数声明为数组即可:
以防万一:虽然您不必在此处使用 unsafe 关键字,但它并不安全。当 C 代码写入超过您分配的块的末尾时,它很容易损坏堆。
The P/Invoke marshaller already does this, you don't have to help. Just declare the function argument as an array:
Just in case: although you don't have to use the unsafe keyword here, there isn't anything safe about it. The C code can easily corrupt the heap when it writes past the end of the block you allocated.
将数组作为 IntPtr[] 传递,IntPtr 默认情况下封送为 void*。不
需要不安全。
...
//C/C++
// 注意,使用时stdcall是默认的调用约定
呼唤!!!
Pass the array as an IntPtr[], IntPtr are by default marshaled as void*. No
need for unsafe.
...
//C/C++
// note that stdcall is the default calling convention when using
PInvoke!!!!