Target CPU and why you may get: “An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)”

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 4.83 out of 5)
 

An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

I remember getting quite annoyed a long time ago when I would get that error message above. Although it happened a few years ago I decided to write a brief post about it now anyways. You need to remember that this error message could possibly occur in different scenario’s and not just the way I got it.

I was trying to setup an example using VB 2005 that used a static.DLL file I made with C++. Whenever I would call a function from the DLL I would get the error message below…

‘BadImageFormatException was unhandled’

“An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)”

Picture of the Bad Format Error Message


It took some time and the advice of someone to find out why the problem kept popping up.

I use as my main computer an Intel Core2 based Processor/CPU which has 64 Bit support (x64 or AMD64, Intel64 is a clone of AMD64, which is the code name). Anyways, I use Windows Vista 64 Bit. That’s obviously not a problem in itself. I made the .DLL I mentioned above long before I upgraded to Vista 64. (BTW this error message will popup as well when running Windows7 64 Bit, and newer as well) The C++ .NET .DLL I made was based on Visual Studio.NET 2003 which will only compile 32 bit assemblies and was written when I still used Windows XP Professional 32 Bit (x86). Of course that’s not a problem in itself either. (Microsoft says .NET older than 2005 should Not be used for programming on any variant of Vista). The problem was when the two came together. I use Visual Studio 2005 Standard Edition which can target both 32 Bit and 64 Bit CPU’s and by default the profile is – Target CPU: AnyCPU. Since my CPU was running on a 64 bit Operating System, Visual Studio.NET would try to target using 64 Bit profiling instead of 32-Bit which the .DLL was written in. And that turned out to be the reason I kept getting that incorrect format error message. The program was targeting 64 Bit mode but the .DLL library I was using was 32 bit.


Picture of the Target CPU Compile Screen

To fix the problem all I had to do was change the: Target CPU: to ‘x86’ which is 32 Bit mode under the:

‘Project’ Menu: then under your project’s ‘Properties’: then under the ‘Compile’ Tab, and then ‘Advanced Compiler Settings’ which will have a small ComboBox near the bottom to select the ‘Target CPU’ to use, which I set to x86 in my case. But ‘x64′ is also available. In version 2010 of Visual Basic you can target Intel’s ‘Itanium’ processor as well..

I then re-ran the program and the calls to the .dll worked exactly like its supposed too. So if you ever get that Bad Format error message and your programming in 64 Bit mode you may try changing the Target CPU to x86 instead of the default AnyCPU (Or vice-versa).

Note: Visual Basic 2005/2008/2010 Express Edition IDE can also target a cpu architecture or platform but you change the settings in a different way using the configuration manager.

Jason

Revisited: 2015

Leave a Reply