.net 即使平台相同,“尝试加载格式不正确的程序”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2023766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:45:31  来源:igfitidea点击:

"An attempt was made to load a program with an incorrect format" even when the platforms are the same

.net64-bitpinvoke32-bitbadimageformatexception

提问by David Brown

I'm calling functions from a 32-bit unmanaged DLL on a 64-bit system. What I get is:

我正在从 64 位系统上的 32 位非托管 DLL 调用函数。我得到的是:

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

BadImageFormatException: 试图加载格式不正确的程序。(来自 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.

起初,我将我的项目设置为 Any CPU 平台,所以我将它们都更改为 x86,但仍然出现此错误。这真的是我知道的唯一解决方法。

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 DllNotFoundExceptionin that case?

DLL 没有损坏或任何东西,因为我可以将它们与其他程序(我没有源代码)一起使用。我想也许它没有找到依赖项,但我检查了一下,它们都在那里。另外,DllNotFoundException在这种情况下它不会抛出一个吗?

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

我还可以做些什么?在您说“改用 64 位非托管 DLL”之前,让我指出没有。;)

采纳答案by David Brown

Somehow, the Buildcheckbox in the Configuration Manager had been unchecked for my executable, so it was still running with the old Any CPU build. After I fixed that, Visual Studio complained that it couldn't debug the assembly, but that was fixed with a restart.

不知何故,配置管理器中的构建复选框已取消选中我的可执行文件,因此它仍在使用旧的 Any CPU 构建运行。在我解决这个问题后,Visual Studio 抱怨它无法调试程序集,但通过重新启动修复了这个问题。

回答by bluwater2001

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".

如果您尝试在 IIS 7(和/或 64 位操作系统机器)上运行 32 位应用程序,您将收到相同的错误。因此,在 IIS 7 中,右键单击应用程序的应用程序池并转到“高级设置”并将“启用 32 位应用程序”更改为“TRUE”。

Restart your website and it should work.

重新启动您的网站,它应该可以工作。

enter image description here

在此处输入图片说明

回答by Marvin Thobejane

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

Visual Studio 中,右键单击您的项目-> 在左侧窗格中单击Build选项卡,

Project properties, build tab

项目属性,构建选项卡

under Platform Targetselect x86 (or more generally the architectureto match with the library you are linking to)

Platform Target 下选择 x86(或者更普遍的是与您链接到的库相匹配的架构

Project properties, platform target

项目属性、平台目标

I hope this helps someone! :)

我希望这可以帮助别人!:)

回答by Denis

I just had this problem also. Tried all the suggestions here, but they didn't help.

我也刚遇到这个问题。尝试了这里的所有建议,但没有帮助。

I found another thing to check that fixed it for me. In Visual Studio, right-click on the project and open "Properties". Click on the "Compile" (or "Build") tab and then click on "Advanced Compile Options" at the bottom.

我找到了另一件事来检查它为我修复了它。在 Visual Studio 中,右键单击项目并打开“属性”。单击“编译”(或“构建”)选项卡,然后单击底部的“高级编译选项”。

Check the dropdown "Target CPU". It should match the "Platform" you are building. That is, if you are building "Any CPU" then "Target CPU" should say "Any CPU". Go through all of your Platforms by making them active and check this setting.

检查下拉菜单“目标 CPU”。它应该与您正在构建的“平台”相匹配。也就是说,如果您正在构建“Any CPU”,那么“Target CPU”应该是“Any CPU”。通过使它们处于活动状态来浏览所有平台并检查此设置。

回答by paibamboo

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, 2017, and 2019

如果您在单击绿色箭头按钮运行应用程序时遇到此错误,但仍想以 64 位运行应用程序。您可以在 VS 2013、2015、2017 和 2019 中执行此操作

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

转到:工具 > 选项 > 项目和解决方案 > Web 项目 > 使用 64 位版本的 IIS Express

回答by Drew Noakes

If you are using Any CPU, you might encounter this issue if the Prefer 32-bitoption is checked:

如果您使用Any CPU,并且选中Prefer 32-bit选项,您可能会遇到此问题:

Make sure you uncheckthis option in the project's property's Buildtab!

确保在项目属性的Build选项卡中取消选中此选项!

enter image description here

在此处输入图片说明

回答by StingyHyman

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.

如果您通过团队系统构建并收到此错误,则构建定义过程选项卡具有“MSBuild 平台”设置。如果将其设置为“自动”,您可能会遇到此问题。将其更改为“X86”也可以解决错误。

回答by Tomasz Stypich

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.

就我而言,我在 C# 中使用了本机 DLL。这个 DLL 依赖于其他几个丢失的 DLL。添加其他 DLL 后,一切正常。

回答by MilConDoin

Building on the answer of @paibamboo

以@paibamboo 的答案为基础

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

他说:转到:工具>选项>项目和解决方案>Web项目>使用64位版本的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?

对知识渊博的用户的问题:上周(对于 VS 2015)是否有更新或使状态不同步的内容?

回答by Shaul Behr

Also see this answer, which solved the same problem for me.

另请参阅此答案,它为我解决了同样的问题。

Posted by Luis Mack on 5/12/2010 at 8:50 AM I've found the same problem, only for a specific project when compiling on a 64-bit machine. A fix that SEEMS to work is to manually alter one character in the image stream EVERY TIME the usercontrol or form is edited in the designer

 AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w

Change to

 AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj0yLjAuMC4w

That is 00LjAuMC4wback to 0yLjAuMC4wat the end of the line (00 back to 0y)

Luis Mack 于 2010 年 5 月 12 日上午 8 点 50 分发表 我发现了同样的问题,仅适用于在 64 位机器上编译时的特定项目。一种似乎有效的修复方法是每次在设计器中编辑用户控件或表单时手动更改图像流中的一个字符

 AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w

改成

 AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj0yLjAuMC4w

即行尾的00LjAuMC4w回到0yLjAuMC4w(00 回到 0y)