“尝试加载格式不正确的程序”即使平台相同
我正在从 64 位系统上的 32 位非托管 DLL 调用函数。我得到的是:
BadImageFormatException:尝试加载格式不正确的程序。 (HRESULT 异常:0x8007000B)
起初,我将项目设置为 Any CPU 平台,因此我将它们都更改为 x86,但此错误仍然发生。这确实是我所知道的唯一解决办法。
DLL 没有损坏或任何东西,因为我可以将它们与其他程序一起使用(我没有源代码)。我想也许它没有找到依赖项,但我检查了一下,它们都在那里。另外,在这种情况下它不会抛出 DllNotFoundException 吗?
我还能做什么?在您说“改用 64 位非托管 DLL”之前,我要指出的是,不存在这样的DLL。 ;)
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. ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
这篇文章有点偏离主题,但搜索此错误消息将我带到了这里。
如果您通过团队系统进行构建并收到此错误,则构建定义过程选项卡具有“MSBuild Platform”设置。如果将其设置为“自动”,您可能会遇到此问题。将其更改为“X86”也可以解决该错误。
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.
就我而言,这是文件的错误内容。 DLL是从网上下载的,但DLL的内容是HTML页面:D
尝试检查它是否是二进制文件,如果它看起来是正确的 DLL :)
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 :)
基于@paibamboo 的回答,
他说:转到:工具>选项>项目与解决方案>网络项目>使用 64 位版本的 IIS Express
我的同事选中了此框(他明确地查找了它),但出现了有问题的错误消息。几个小时后,他取消选中该框并再次选中。你瞧:代码现在运行成功了。
看来,有两个地方保存了该盒子的状态,但变得不同步。取消并重新检查它会再次同步。
对于更有知识的用户的问题:上周(VS 2015)是否有更新或其他东西使状态不同步?
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?
另请参阅此答案,它为我解决了同样的问题。
Also see this answer, which solved the same problem for me.
就我而言,我使用一个微小的 .exe 通过反射重新加载引用的 DLL。因此,我只需执行以下步骤即可节省时间:
从解决方案资源管理器上的项目属性中的“构建”选项卡中,我选择来自 x86 的目标平台
In my case, I am using a tiny .exe that reloads the referenced DLLs via Reflection. So I just do these steps which saves my day:
From project properties on solution explorer, at build tab, I choose target platfrom x86
就我而言,我通过 MSTest 运行测试,发现我正在将 32 位和 64 位 DLL 部署到测试目录。该程序偏爱 64 位 DLL,导致其失败。
TL;DR 确保仅部署 32 位 DLL 进行测试。
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.
我能够通过将我的构建版本与服务器上的 .NET 版本相匹配来解决此问题。
我双击 .exe 只是为了看看会发生什么,它告诉我安装 4.5....
所以我降级到 4.0 并且它起作用了!
因此请确保您的版本匹配。它在我的开发盒上运行良好,但服务器具有较旧的 .NET 版本。
I was able to fix this issue by matching my build version to the .NET version on the server.
I double clicked the .exe just to see what would happen and it told me to install 4.5....
So I downgraded to 4.0 and it worked!
So make sure your versions match. It ran on my dev box fine, but server had older .NET version.
如果您要导入非托管 DLL,请
在 DLL 导入方法中使用。
If you are importing unmanaged DLL then use
in your DLL import method.
如果您在 Visual Studio 中运行 Web 应用程序项目,则可能是您尝试使用 64 位 DLL,但 IIS Express 正在 32 位模式下运行。要更改它,请转到“工具”>“选项>项目与解决方案> Web 项目并指定使用 64 位 IIS Express
If you're running a web application project in Visual Studio, then it could be that you're trying to use a 64-bit DLL but IIS Express is running in 32-bit mode. To change that, go to Tools > Options > Projects and Solutions > Web Projects and specify to use 64-bit IIS Express
我以“Windows”方式解决了这个问题。检查所有设置、清理解决方案并重建它后,我只需关闭解决方案并重新打开它即可。然后就成功了,所以VS可能在清理过程中没有去掉一些东西。
当逻辑解决方案不起作用时,我通常会转向不合逻辑(或看似不合逻辑)的解决方案。 Windows 没有让我失望。 :)
I got this issue solved in the 'Windows' way. After checking all my settings, cleaning the solution and rebuilding it, I simply close the solution and reopened it. Then it worked, so VS probably didn't get rid of some stuff during cleaning.
When logical solutions don't work, I usually turn to illogical (or seemingly illogical) ones. Windows doesn't let me down. :)
我们在 .NET core 中遇到了同样的问题。解决方案是下载 32 位 .netcore 运行时,并让您的项目目标为
x86
在您的
csproj
文件中添加这用于 Windows 计算机,您必须调整 Linux/OSX 的路径等
We were having the same issue in .NET core. The solution was to download 32-bit .netcore runtime, and having your project target
x86
In your
csproj
file addThis was used for a Windows machine, you'd have to adjust paths and such for Linux/OSX
就我而言,我没有将正确的项目设置为启动项目。我转到解决方案设置并选择了正确的启动项目并且它起作用了
In my case, I didn't have the correct project set as the start-up project. I went to solution settings and selected the correct startup project and it worked
就我而言,发布后也发生了同样的错误。我之前曾用另一个平台配置发布过。
解决方案是先清理发布文件夹,然后就可以了。
(或者将“删除现有文件”选项设置为 true)
In my case this same error happened after publishing. I had published before with another platform configuration.
The solution was to cleanup the publish folder first, then it worked.
(alternatively set the "delete existing files" option to true)
可能导致此异常的另一个原因是 Dll 的目标平台缺少 C++ Redistributables。在虚拟机上进行测试时,我很难找到答案。
Another reason that may cause this exception, is the C++ Redistributables missing for the target platform of your Dll. I had a tough time finding out when testing on a VM.
对于.net core,请确保Ijwhost.dll位于输出目录中,有时它没有被复制,这会导致错误。请参阅 https://github.com/dotnet/runtime/issues/38231 以及https://stackoverflow.com/a/58773266/9665729
For .net core, make sure Ijwhost.dll is in the output directory, sometimes it isn't being copied and this will cause the error. See https://github.com/dotnet/runtime/issues/38231 and also https://stackoverflow.com/a/58773266/9665729
另请注意,正在使用的 dll 文件(在我的例子中为“WebView2Loader.dll”)的版本非常重要。当我尝试在 MMC 管理单元中使用“HTMLView”或“FormView”类型的 WebView2 组件时,我遇到了与“Microsoft.WebView2.FixedVersionRuntime.101.0.1210.39.x64”几乎相同的问题。
我只是将引用的 dll 文件复制到项目可访问的正确路径中(您可以先将其放在项目输出文件旁边进行测试),然后 WebView2 浏览器开始按预期运行。微软的错误消息有时(至少在我的情况下)有点误导性,并且没有传达足够的、中肯的信息。
我收到“BadImageFormatException”,通常在您混合平台目标(例如,在针对 x86 的应用程序中使用以 X64 编译的 dll 文件,反之亦然)或混合本机代码和 .NET 时发生,但这根本不是我的问题。我希望这可以帮助那些可能陷入困境的人。
Please also note that the version of used dll file(s) (in my case "WebView2Loader.dll") which is in use is very crucial. I had almost the same problem with "Microsoft.WebView2.FixedVersionRuntime.101.0.1210.39.x64" when I tried to use the WebView2 component in the MMC Snap-Ins with types of "HTMLView" or "FormView".
I just copied the referenced dll file in a proper path that was accessible for the project (you could just put it beside your project output files first to test it) and then WebView2 browser started to function as expected. Microsoft error messages sometimes (at least in my case) was a little bit misleading and did not convey enough and to the point information.
I received "BadImageFormatException" that normally occurs when you mix platform targets (for example using a dll file compiled in X64 in an application that targeted for x86 or vice versa) or mix native code and .NET but that was not my problem at all. I hope this help one who may stuck in.
当我想从 C#/WPA 调用本机 DLL 时,我也遇到了这个问题。以下步骤适用于我的项目。属性 -> 构建 -> 平台目标 x64/x86(更改此选项即可工作)。
I also had this issue when I want to call a native DLL from C#/WPA. The below steps work for my project. Properities->Build->Platform target x64/x86(change this option, then works).
如果您尝试在 IIS 7(和/或 64 位操作系统计算机)上运行 32 位应用程序,您将收到相同的错误。因此,在 IIS 7 中,右键单击应用程序的应用程序池,然后转到“高级设置”并将“启用 32 位应用程序”更改为“TRUE”。
重新启动您的网站,它应该可以工作。
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.
不知何故,配置管理器中的构建复选框未选中我的可执行文件,因此它仍在使用旧的 Any CPU 构建运行。在我解决这个问题后,Visual Studio 抱怨它无法调试程序集,但通过重新启动就解决了这个问题。
Somehow, the Build checkbox 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.
在 Visual Studio 中,右键单击您的项目并选择属性 ->在左侧窗格中,单击
构建选项卡,
平台下的 目标选择 x86(或者更一般地选择与您要链接的库匹配的架构)
In Visual Studio, Right Click your project and select properties -> On the left pane click the Build tab,
under Platform Target select x86 (or more generally the architecture to match with the library you are linking to)
如果您在单击绿色箭头按钮运行应用程序时遇到此错误,但仍想以 64 位运行应用程序。您可以在 VS 2013、2015、2017 和 2019 中执行此操作
转到:工具 >选项>项目与解决方案>网络项目>使用 64 位版本的 IIS Express
或者您可以在“项目属性”>“每个项目”中执行此操作。网页>位数
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
Go to: Tools > Options > Projects and Solutions > Web Projects > Use the 64 bit version of IIS Express
Or you can do it per project at Project Properties > Web > Bitness
我刚刚也遇到了这个问题。尝试了这里的所有建议,但没有帮助。
我找到了另一件事来检查,为我解决了这个问题。在 Visual Studio 中,右键单击该项目并打开“属性”。单击“编译”(或“构建”)选项卡,然后单击底部的“高级编译选项”。
检查下拉菜单“目标 CPU”。它应该与您正在构建的“平台”相匹配。也就是说,如果您正在构建“任何 CPU”,那么“目标 CPU”应该为“任何 CPU”。通过激活所有平台来浏览它们并检查此设置。
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.
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,并且选中首选 32 位选项,则可能会遇到此问题:
确保在项目属性的构建选项卡中取消选中此选项!
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!
就我而言,我使用的是 C# 中的本机 DLL。该 DLL 依赖于其他几个缺失的 DLL。一旦添加了其他 DLL,一切就都正常了。
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.
我们遇到了类似的问题,我们设法通过将平台目标设置为 x86 来解决它。
We had a similar issue and we managed to fix it by setting the Platform target to x86.
使用 Visual Studio 2019,当我想运行测试(直接从 VS 进行 MSTest)时,我遇到了类似的问题。就我而言,我只有一个 x64 本机 DLL,并且收到了此错误消息。首先,我认为这是因为 Visual Studio 作为 x86 运行,但此页面帮助我解决了问题:
将单元测试作为 64 位进程运行
它表示
我都做了(我明确设置了 x64),然后我的测试开始工作。
With Visual Studio 2019 I had a similar issue when I wanted to run tests (MSTest directly from VS). In my case I only had an x64 native DLL and I received this error message. First, I thought it is because Visual Studio runs as x86 but this page helped me to solve the issue:
Run unit test as a 64-bit process
It says
I did both (I explicitly set x64) and then my tests started to work.