【发布时间】:2015-04-16 05:45:39
【问题描述】:
总结: 我正在尝试使用带有 cdecl 调用约定 的 C++ dll,除非我得到这个方法签名,否则一切运行良好:
int SaveToBuffer( char **buf, int *buf_size );
根据我的阅读,我应该这样使用它:
[DllImport("entry.dll",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "SaveToBuffer")]
private static int SaveToBuffer( ref sbyte[] buf, ref int buf_size );
如果从 C# 程序崩溃中调用此函数,这将不起作用。 我想这与 Cdecl 调用模型有关,应该使用 Marshal.AllocHGlobal(value), 我无法想象它应该如何正确地完成。
我也试过这个:
[DllImport("entry.dll",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "SaveToBuffer")]
private static int SaveToBuffer( IntPtr buf, ref int buf_size );
然后分配足够的内存
IntPtr data=Marshal.AllocHGlobal(128000);
int bufSize=128000;
var sCode=SaveToBuffer(data,bufSize ); /* value of scode idicate succses*/
以这种方式调用我从 SaveToBuffer 获得返回值,指示函数成功但是:bufSize 返回 0 以及我应该如何从 IntPtr 读取我的数据。
我完全坚持这一点。
【问题讨论】: