BadImageFormatException when loading 32 bit DLL, target is x86

72,143

Solution 1

From what I understand, an assembly specifically built for x86 and running in a 64-bit operating system can only load libraries built for x86 or a BadImageFormatException will be thrown. In a 64-bit OS, an assembly built for Any CPU or x64 will throw the same exception when trying to load an x86 library.

So, assuming nothing incredibly weird is going on, I would ensure that you've set your application to build as x86 by opening the project properties and clicking on the Build tab. Ensure 'Platform Target' is set as 'x86' and not Any CPU.

Alternatively, you could try to find a 64-bit version of the DLL for testing purposes.

Solution 2

Recompile the dll with the option "Any CPU" in Build -> Platform.

enter image description here

Solution 3

OK, seems like a false alert. It was not related to bitness, there was just other DLL missing that freetype depends on. However error messages could be more helpful.

Solution 4

Got the same error when calling a 64-bit C Dll from C#. I had to manually change C# Properties->Build->Platform target from Any Cpu to x64. Apparently Any Cpu is sometimes NoCpu.

Solution 5

Besides, for web-application needs resolve to run 32-Bit Applications in IIS 7. See http://www.fishofprey.com/2009/04/badimageformatexception-in-iis-70-on-64.html

Share:
72,143
Coder
Author by

Coder

Updated on June 20, 2020

Comments

  • Coder
    Coder about 4 years

    I have a DLL (FreeType) which is certainly 32-bit (header: IMAGE_FILE_MACHINE_I386).

    I want to use it from C# code, using DllImport.

    Target of my application is x86, IntPtr.Size is 4, process is 32-bit.

    But I get BadImageFormatException (Exception from HRESULT: 0x8007000B). What can be wrong?

    Of course I use 64-bit Windows 7.