2010-08-02 118 views
1

下面一行是在C#GUI生成运行时错误:调用从C#中的C++的.dll引发运行时错误

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) 
+0

您是否用友好名称导出该功能? – 2010-08-02 19:13:16

回答

4

项目+属性,生成标签,平台目标= 86。

您的C/C++ DLL是以32位模式编译的。但是,您的C#程序在64位版本的Windows上运行,并且将以64位模式运行。这混合不匹配。创建一个64位版本的DLL是另一种解决方案。 Build + Configuration Manager,平台组合,新增功能,x64。

+0

谢谢soooooo多 - 我只是花了最后4个小时试图追踪这个问题,你是一个传奇! – Contango 2010-08-02 19:50:29