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.
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.
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).
The exception that is thrown when the file image of a dynamic link library (DLL) or an executable program is invalid.
(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.
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.
Recompile the dll with the option "Any CPU" in Build -> Platform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With