I'm calling functions from a 32-bit unmanaged DLL on a 64-bit system. What I get is:
BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
At first, I had my projects set to the Any CPU platform, so I changed them both to x86, but this error is still occurring. That's really the only fix I know for this.
The DLLs aren't corrupt or anything, because I can use them with other programs (that I don't have the source to). I thought that perhaps it wasn't finding a dependency, but I checked and they're all there. Plus, wouldn't it throw a DllNotFoundException
in that case?
What else can I do? And before you say "Use a 64-bit unmanaged DLL instead," let me point out that there isn't one. ;)
In my case, I was running tests through MSTest and found out that I was deploying both a 32-bit and 64-bit DLL to the test directory. The program was favoring the 64-bit DLL and causing it to fail.
TL;DR Make sure you only deploy 32-bit DLLs to tests.
In my case it was wrong content of the file. DLL was downloaded from the web, but content of the DLL was HTML page :D Try to check if it is binary file, if it seems like correct DLL :)
If you are using Any CPU, you might encounter this issue if the Prefer 32-bit option is checked:
Make sure you uncheck this option in the project's property's Build tab!