BadImageFormatException during .Net assembly load issue

I just got this when deploying 32 bit DLLs onto a 64 bit server running IIS 7.

To fix it, I had to set "Enable 32-Bit Applications" to True in the Advanced Settings of my application pool.


The BadImageFormatException is raised when the assembly file can be found, but is not a proper assembly, or is corrupted. For instance, if you FTP'ed the files to the server and the transfer was interrupted, the DLL file may have been transferred only partially, causing this error to happen.

On 64 bit vs 32 bit: when you use P/Invoke or COM Interop, some blogger reports that switching to a specific target compilation may help your situation. This means: if you interface with a 32 bit dll, make sure that you compile for x86, forcing it to run under WoW32, otherwise you'll receive this exception. This fix is confirmed here and here.

Alternatively, you can set your whole system to default 32-bit by running the command:

Ldr64.exe setwow

from the Framework64 directory.

A common solution is to rebuild the file, or to at least re-publish it.