Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"An attempt was made to load a program with an incorrect format" even when the platforms are the same

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. ;)

like image 971
David Brown Avatar asked Jan 07 '10 21:01

David Brown


People also ask

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.

How do you fix an attempt was made to load a program with an incorrect format?

Simply right-click on Project > go to Properties > Build > check Prefer 32-bit. You can also use preprocessor conditions, but in this case you need to compile two different assemblies. In other words, compile a 32-bit assembly for 32-bit platforms, and a separate 64-bit assembly for 64-bit platforms.

Could not be opened -- An attempt was made to load a program with an incorrect format?

“An attempt was made to load a program with an incorrect format.” That means that the assembly, which was to be loaded, was in an unexpected format. The format, in this case, refers most likely to the 64-bit build of an application being deployed to IIS, which is being run in 32-bits.


2 Answers

Somehow, the Build checkbox in the Configuration Manager had been unchecked for my executable, so it was still running with the old Any CPU build. After I fixed that, Visual Studio complained that it couldn't debug the assembly, but that was fixed with a restart.

like image 45
David Brown Avatar answered Sep 19 '22 19:09

David Brown


If you try to run 32-bit applications on IIS 7 (and/or 64-bit OS machine), you will get the same error. So, from the IIS 7, right click on the applications' application pool and go to "advanced settings" and change "Enable 32-Bit Applications" to "TRUE".

Restart your website and it should work.

enter image description here

like image 144
bluwater2001 Avatar answered Sep 16 '22 19:09

bluwater2001