Troubleshooting Exceptions: System.BadImageFormatException
Updated: April 2011
A BadImageFormatException exception is thrown when the file image of a DLL or executable program is not a valid managed executable.
Reflecting on C++ executable files may throw this exception. This is most likely caused by the C++ compiler stripping the relocation addresses or the .Reloc section from the executable file. To preserve the .relocation address in a C++ executable file, specify /fixed:no when linking.
For more causes of this exception, see the BadImageFormatException class.
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.
- 10/27/2009
- Mayank H Parmar