“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楼-- · 2018-12-31 23:51

In my case I was using a native DLL in C#. This DLL depended on couple of other DLLs that were missing. Once those other DLLs were added everything worked.

查看更多
妖精总统
3楼-- · 2018-12-31 23:52

If you encounter this error when you click green arrow button to run the application, but still want to run the app in 64 bit. You can do this in VS 2013, 2015, and 2017

Go to: Tools > Options > Projects and Solutions > Web Projects > Use the 64 bit version of IIS Express

查看更多
长期被迫恋爱
4楼-- · 2018-12-31 23:55

A bit off topic for this post, but searching for this error message brought me here.

If you are building through team system and getting this error, the build definition process tab has a "MSBuild Platform" setting. If this is set to "Auto", you may experience this problem. Changing it to "X86" can also resolve the error.

查看更多
琉璃瓶的回忆
5楼-- · 2018-12-31 23:58

In Visual Studio, Right Click your project -> On the left pane click the Build tab,

Project properties, build tab

under Platform Target select x86 (or more generally the architecture to match with the library you are linking to)

Project properties, platform target

I hope this helps someone! :)

查看更多
君临天下
6楼-- · 2019-01-01 00:00

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

查看更多
浅入江南
7楼-- · 2019-01-01 00:00

Building on the answer of @paibamboo

He said: Go to: Tools > Options > Projects and Solutions > Web Projects > Use the 64 bit version of IIS Express

My coworker had this box checked (he explicitly looked for it), but had the error message in question. After some hours he unchecked the box and checked it again. Lo and behold: The code now ran with success.

It seems, that there are two places where the state of this box ist saved which became out of sync. Un- and rechecking it synced it again.

Question for more knowledgable users: Was there an update or something last week (for VS 2015) which de-synced the states?

查看更多
登录 后发表回答