2010-08-02 121 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