【发布时间】:2020-08-26 09:39:41
【问题描述】:
我将一个 C DLL 导入到我的 C# 程序中,并且必须编组一个 ByRef 结构参数。
这是 C 中函数的签名:
extern "C" {
int __stdcall FuncToImport(const char* stringToMarshall, StructFromDLL* structToMarshall);
...
...
...
}
这是我尝试在 C# 中导入它的方式
[DllImport("ImportedDll.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr FuncToImport(string stringToImport, ref HandleRef structToImport);
在 Main 中调用它时出现异常: System.Runtime.InteropServices.MarshalDirectiveException: '"parameter #7" cannot be marshalled: HandleRefs cannot be marshalled as ByRef or not managed/managed Parameter..'
正确编组参数的可能方法是什么?
【问题讨论】:
-
HandleRef 是一个非常神秘的选择。您需要在 C# 代码中声明该结构。
标签: c# c import marshalling dllimport