Skip to main content
 首页 » 编程设计

c#之我是否正确调用了这个 C++ DLL

2024年02月23日17lovecherry

我需要从C#调用一个C++ DLL函数,C++函数定义是

DWORD Init(char*[],int,int,int); 

我在 C# 中这样调用它

[DllImport("DLLTest.dll")] 
unsafe public static extern int Init(string[] detailsarray, int amount, int ID1, int ID2); 
 
//then in main... 
 
string[] details = new string[3]; 
details[0] = "blahblah"; 
details[1] = "bloobloo"; 
details[2] = "1234"; 
 
int result = Init(details, 1, 16, 170); 

当我运行它时,我得到一个类型为 System.BadImageFormatException 的未处理异常。我相信当 C++ DLL 需要 char*s 时传递字符串是可以的,我也认为在 C# 中使用 int 代替 C++ 中的 DWORD 是可以的,但如果我是正确的,请告诉我。

谢谢。

编辑 - 我应该补充一点,我已经在 C++ 应用程序中测试了 DLL,它工作正常。

请您参考如下方法:

如果您尝试将 32 位 DLL 加载到 64 位进程或相反,则经常会发生 BadImageFormatException。如果 DLLTest.dll 是 32 位 DLL,则需要为 x86 平台而非任何 CPU 编译 C# DLL

http://msdn.microsoft.com/en-us/library/zekwfyz4(v=vs.100).aspx

http://davidstechtips.com/wp-content/uploads/2010/06/VisualStudioPlatformTargetx86.png