Alternative Cause of BadImageFormatException in .NET Assembly?

I am working on a .NET 3.5 console application in C # that uses an unmanaged DLL version of VC ++. It worked without problems when I worked on it a few weeks ago, but now I return to it and now I get a BadImageFormatException exception ("An attempt was made to load a program with the wrong format" (Exception from HRESULT: 0x8007000B)).

My development workstation runs on 64-bit Windows 7, and I do a lot of work with unmanaged code, so I immediately noticed that the .NET assembly and the VC ++ library had x86 targets. They did.

To be sure, I cleaned and rebuilt the VC ++ library and .NET assembly, but to no avail.

No system does anything particularly unusual. The VC ++ library loads the binary data file and performs some mathematical processing of the content. The .NET compilation has DllImports for the library and some code to connect it. All this worked a few weeks ago.

So, now I have to wonder whether there is another reason for the BadImageFormatException exception, which is less common than the x86 / x64 conflict I could work with.

Thanks.

EDIT: I get the same error, regardless of the x86 or x64 mode, but when "Any processor" is installed, the execution passes this point, but the execution is interrupted when the VC ++ library is subsequently called without exception. Regardless of whether this is due to this problem, is there something that β€œany processor” is different from x86 and x64 that can shed light on this?

+6
c ++ c # visual-studio-2008 visual-studio dllimport
source share
5 answers

When I get this error, it is always caused by loading a 32-bit DLL in a 64-bit process.

Install the EXE file to compile on x86 and see if it works.

+4
source share

Perhaps you are trying to download the assembly created for CLR 4.0 on CLR 2.0.

+3
source share

Check for .dll loading conflict!

I called the C ++ / CLI dll from C #; the C ++ / CLI library is a wrapper for a third-party third-party DLL.

It turns out I had two DLLs with the same name, as in the path (libeay32.dll).

To detect the source of the problem, I installed window debugging tools: http://www.microsoft.com/whdc/devtools/debugging/default.mspx

Run "gflags" (in the folder "c: \ Program Files \ Debugging Tools ...") to enable the display of "loader" bindings

i.e.

> gflags -i <my test app.exe> +sls 

then run the application in cdb (console debugger) or windbg and go through the exit to find out which DLL caused the exception.

eg.

 > cdb -g <my test app.exe> 

Renaming the "wrong" libeay32.dll showed the problem, but this is only a temporary solution!

The same troubleshooting approach may work for you anyway.

+3
source share

Given your use of the native code, I think the most likely problem is that you are trying to load the native DLL, as if it were a .Net assembly. This is one scenario in which a BadImageFormatException will occur.

Try to run the application and set it to throw for BadImageFormatException and see which DLL is loading. If this is native, then the problem.

+2
source share

In my case, disabling Enable unmanaged code debugging on the Debug tab of the properties of the EXE project, ironically, did the trick if it was installed.

To be honest, I'm not sure why this is causing the problem.

0
source share

All Articles