Skip to main content
 首页 » 编程设计

c#之从 C# 调用 C++ .dll 会引发运行时错误

2024年02月24日18bonelee

以下行在 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) 

请您参考如下方法:

项目 + 属性,构建选项卡,平台目标 = x86。

您的 C/C++ DLL 是在 32 位模式下编译的。但是你的 C# 程序运行在 64 位版本的 Windows 上,将以 64 位模式运行。那组合不匹配。创建 DLL 的 64 位版本是另一种解决方案。构建 + 配置管理器,平台组合,新,x64。