“An attempt was made to load a program with an inc

2018-12-31 23:38发布

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

15条回答
与君花间醉酒
2楼-- · 2019-01-01 00:11

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.

查看更多
梦醉为红颜
3楼-- · 2019-01-01 00:11

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 :)

查看更多
呛了眼睛熬了心
4楼-- · 2019-01-01 00:18

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!

enter image description here

查看更多
登录 后发表回答