2012年5月23日 星期三

C# 在 64-bit 平台載入 32-bit DLL 出錯 (0x8007000B)


備份資料
環境:
C#
Microsoft Visual Studio 2008
Microsoft Vista Enterprise 64-bit

在 C# 程式碼中,載入 32-bit DLL,編譯時出現錯誤:
未處理的例外狀況: System.BadImageFormatException: 試圖載入格式錯誤的程式。
(發生例外狀況於 HRESULT: 0x8007000B)


原因:
Managed applications (e.g. C#) can be compiled for any target platform,
thus running the program on a 64-bit processor
will make it compile its MSIL to 64-bit byte code.

If your C++ DLL is 32-bit
then it cannot be used obviously,
and you will get a BadImageFormatException.

解法一:
使用 64-bit 的 DLL,如果有的話.....

解法二:
C#專案 → 滑鼠右鍵 → 屬性 → (標籤)建置 → 平台目標 → Any CPU 改成 x86
→ 上面的標籤 → 滑鼠右鍵 → 儲存選取項目

沒有留言:

張貼留言