C#调用C++ dll中uchar*参数

C#调用C++ dll中uchar*参数,第1张

我的方式是:
C++程序中的uchar*->C#程序中的IntPtr->byte[]

当然很多人都说直接:
C++程序中的uchar*->C#程序中byte[]

但是我的实践过程中,在实时性较高的程序中,第二种方式总是会让数据加载不完全,导致程序经常崩溃,具体原因不知。

另外需要注意的是,要及时释放IntPtr内存。

[DllImport("Yourc++File", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr YourCplusplusFunction();
int SIZE = xxx;
byte[] buffer = new byte[SIZE];
...
IntPtr ptr = Marshal.AllocHGlobal(SIZE);
ptr = YourCplusplusFunction();
Marshal.Copy(ptr, buffer, 0, SIZE);
Marshal.FreeHGlobal(ptr);

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/713681.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-24
下一篇 2022-04-24

发表评论

登录后才能评论

评论列表(0条)

保存