呼叫 C++ C# 中的 .dll 引发运行时错误
以下行在 C# GUI 中生成运行时错误:
int x = myclass.number_from_dll();
我正在使用 Microsoft Visual Studio 2008。
C# 中的代码是:
class myclass
{
[DllImport("strat_gr_dll.dll", EntryPoint = "number_from_dll")]
public static extern int number_from_dll();
}
C++ .dll 中的代码是:
// This is an example of an exported function.
DLL int number_from_dll(void)
{
return 42;
}
.NET 中的运行时错误是:
An attempt was made to load a program with an incorrect format.
(Exception from HRESULT: 0x8007000B)
The following line is generating a runtime error in a C# GUI:
int x = myclass.number_from_dll();
I'm using Microsoft Visual Studio 2008.
The code in C# is:
class myclass
{
[DllImport("strat_gr_dll.dll", EntryPoint = "number_from_dll")]
public static extern int number_from_dll();
}
The code in the C++ .dll is:
// This is an example of an exported function.
DLL int number_from_dll(void)
{
return 42;
}
The runtime error from .NET is:
An attempt was made to load a program with an incorrect format.
(Exception from HRESULT: 0x8007000B)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
项目 + 属性,构建选项卡,平台目标 = x86。
您的 C/C++ DLL 是以 32 位模式编译的。但是您的 C# 程序运行在 64 位版本的 Windows 上,并且将以 64 位模式运行。那个组合不匹配。创建 DLL 的 64 位版本是另一种解决方案。构建 + 配置管理器、平台组合、新、x64。
Project + Properties, Build tab, Platform Target = x86.
Your C/C++ DLL was compiled in 32-bit mode. But your C# program is running on a 64-bit version of Windows and will run in 64-bit mode. That mix don't match. Creating a 64-bit version of you DLL is another solution. Build + Configuration Manager, Platform combo, New, x64.