Application built exactly the same way in both Debug and Release configurations doesn't work in Release on Win7: BadImageFormatException

0

I'm developing an application on a Win7 x64 machine.

There's some code that makes a call to an external .dll file. The .dll was built for 32-bit machines. The problem I'm having is when running the app in the debugger in Debug configuration, it works fine. As soon as I switch to Release, any entry into the method that makes the .dll call fails with

An unhandled exception of type 'System.BadImageFormatException' occurred in MyCool.exe

Additional information:  is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)

Both Release and Debug configurations are the same and the application is being built for x86. This problem only occurs on Windows 7 machines. Our customers running XP have no problem. Admittedly, we have no customers running XP x64 so I haven't been able to verify that.

Here is an image of my solution configurations:

My Configurations

I've changed the Platform: option to every possible combination but it still isn't working. Is there some hidden stuff going on with the Debug configuration?

EDIT: I figured this out but I'm really not understanding the problem.

In each project's Build tab in the project properties (Compile tab for VB projects) I have to set Target CPU to x86 instead of AnyCPU.

Maybe this is for another question but: Why? What's the difference between setting the entire solution's platform to x86 and setting the Target CPU to x86 on a per-project basis. Also, in the image above: what's the purpose of the Platform field then??

.net
configuration
badimageformatexception
asked on Stack Overflow Feb 19, 2015 by Brandon • edited Feb 19, 2015 by Brandon

1 Answer

0

This is because AnyCPU will run as 64 bit, but your DLL is likely compiled as 32 bit DLL. You cannot cross call 32 bit DLLs from 64 bit executable.

In setting your app to x86, this will force it to be compiled (and run) as a 32 bit application.

answered on Stack Overflow Mar 2, 2015 by wwelles

User contributions licensed under CC BY-SA 3.0