2012/01/31

32-bit DLLs 使用於 64-bit OS 發生 BadImageFormatException 錯誤之解決方法

今日在 64-bit 的主機使用 VS2010 編譯一支過去寫的程式
突然出現 BadImageFormatException 的錯誤訊息
原程式使用 VS2008 編譯, 當時好像沒有出現該錯誤, 或是我原本使用的 32-bit 主機來編譯
Anyway, 只是因為有一小段 code 要修改, 且把整個程式升級成 VS2010 的專案才浮現的錯誤
還好在看完 MSDN 上的 troubleshooting 之後, 動手變更編譯的 Target CPU: Any CPU 改成 x86 就搞定了



參考 MSDN 的分享:

32-bit DLLs on a 64-bit OS

This exception can occur when a .NET application running on a 64-bit platform attempts to load a DLL that does not support native 64-bit execution. If the application is built in platform-agnostic (i.e. "Any CPU") mode, it will launch in 64-bit mode on the host operating system, leading to this problem. To fix this, you can force the application to run in 32-bit mode by compiling for x86 architecture explicitly, forcing the program to launch in WoW64 mode on the 64-bit system, and allowing the 32-bit DLL to be accessed.