【问题标题】:Calling a C++ .dll from C# throws a runtime error从 C# 调用 C++ .dll 会引发运行时错误
【发布时间】:2010-08-02 19:10:34
【问题描述】:

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

【问题讨论】:

  • 您是否使用友好名称导出该函数?

标签: c# .net dllimport dll


【解决方案1】:

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

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

【讨论】:

  • 非常感谢 - 我刚刚花了 4 个小时试图找出这个问题,你是个传奇!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
  • 2015-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多