Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BadImageFormatException when loading 32 bit DLL, target is x86

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.

like image 918
Coder Avatar asked Apr 28 '10 10:04

Coder


People also ask

How do I resolve system Badimageformatexception?

SOLUTION: If you are on IIS7, Right Click/ Advanced Settings on the Specific Application Pool of the website and select True on Enable 32-Bit Applications. It should work.

Can I run a 32 bit DLL on a 64 bit machine?

On 64-bit Windows, a 64-bit process cannot load a 32-bit dynamic-link library (DLL). Additionally, a 32-bit process cannot load a 64-bit DLL. However, 64-bit Windows supports remote procedure calls (RPC) between 64-bit and 32-bit processes (both on the same computer and across computers).

What is Badimageformatexception?

The exception that is thrown when the file image of a dynamic link library (DLL) or an executable program is invalid.

How do you fix an attempt was made to load a program with an incorrect format exception from Hresult 0x8007000B?

(Exception from HRESULT: 0x8007000B) Print. Solution: This issue arises when there is a 32 bit/64 bit mismatch between the Argon2 DLL and the application that you are using. Basically you will need to ensure that if you are targeting 64 bit you're using the 64 bit binary and vice versa.


2 Answers

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.

like image 93
Eric Smith Avatar answered Sep 21 '22 07:09

Eric Smith


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

enter image description here

like image 30
RckLN Avatar answered Sep 20 '22 07:09

RckLN