C# 如何编译 32 位(我的操作系统是 64 位) - 错误 exe 不是有效的 Win32 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14633205/
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-08-10 12:30:53  来源:igfitidea点击:

How to compile 32-bit (my OS is 64-bit) - Error exe is not a valid Win32 application

c#visual-studiovisual-studio-2012x8632-bit

提问by Kairan

As title suggests I am compiling C# app using VS 2012 on a 64-bit machine. I would like the program being built to run on a 32-bit machine.

正如标题所示,我正在 64 位机器上使用 VS 2012 编译 C# 应用程序。我希望正在构建的程序在 32 位机器上运行。

Right now the only help I found online was for: Menu>Build>Configuration> Active Solution Platform defaulted to Any CPU, and I tried that but didn't work on 32-bit machine (unless I did something wrong)

现在我在网上找到的唯一帮助是:菜单>构建>配置>活动解决方案平台默认为任何 CPU,我尝试过但在 32 位机器上不起作用(除非我做错了什么)

Tried Add a new one to the Debug configuration for platform x86 with build checked

尝试在 x86 平台的调试配置中添加一个新配置并检查构建

Compiled and ran the app on 32-bit machine getting error A.exe is not a valid Win32 application.

在 32 位计算机上编译并运行该应用程序,出现错误 A.exe 不是有效的 Win32 应用程序。

My above was similar to what was done here: Link to Stack Overflow Similar Question

我的上述内容与此处所做的类似:Link to Stack Overflow Similar Question

UPDATE 1:The target OS is WinXP SP3 but we dont believe it has .NET 4.5 on it. I will be testing to see if compiling earlier ver of the app in .NET 4.0 will solve the problem and fix the problem. The problem may not be what the error message is displaying.

更新 1:目标操作系统是 WinXP SP3,但我们认为它没有 .NET 4.5。我将测试以查看在 .NET 4.0 中编译早期版本的应用程序是否会解决问题并修复问题。问题可能不是错误消息显示的内容。

采纳答案by David Heffernan

That error is the Win32 error ERROR_BAD_EXE_FORMAT. It's generated by the loader and is what happens when you try to run a 64 bit process on a 32 bit operating system. There are other ways to see that error, but this is by far the most common reason for it to occur on a .exe file.

该错误是 Win32 错误ERROR_BAD_EXE_FORMAT。它由加载程序生成,当您尝试在 32 位操作系统上运行 64 位进程时会发生这种情况。还有其他方法可以查看该错误,但这是迄今为止发生在 .exe 文件上的最常见原因。

To compile a 32 bit process you need to target x86 in your project configuration. Another alternative would be to target AnyCPU. That will result in a 32 bit process when executed on a 32 bit OS, and a 64 bit process when executed on a 64 bit OS. It would appear that your build targets x64.

要编译 32 位进程,您需要在项目配置中以 x86 为目标。另一种选择是针对 AnyCPU。这将导致在 32 位操作系统上执行时为 32 位进程,而在 64 位操作系统上执行时将导致 64 位进程。看起来您的构建目标是 x64。

回答by Reed Copsey

Setting your project to target AnyCPUshould allow it to run on a 32bit machine, provided you aren't using a library which loads 64bit native code. If you have any dependencies, you'll need to make sure to use AnyCPUor 32-bit versions of those dependencies, as well. Also, make sure you have the proper .NET Framework installed on the 32bit machine (.NET 4.5 by default, if you're using VS 2012 with a new project).

将您的项目设置为目标AnyCPU应该允许它在 32 位机器上运行,前提是您没有使用加载 64 位本机代码的库。如果您有任何依赖项,则还需要确保使用AnyCPU这些依赖项的 32 位版本。此外,请确保在 32 位计算机上安装了正确的 .NET Framework(如果您在新项目中使用 VS 2012,则默认为 .NET 4.5)。

Note that the default in VS 2012 for new projects is AnyCPU, with the Prefer 32 Bitoption checked. This will cause it to always run as a 32bit application, even on your 64bit OS.

请注意,VS 2012 中新项目的默认值是 AnyCPU,并Prefer 32 Bit选中该选项。这将导致它始终作为 32 位应用程序运行,即使在您的 64 位操作系统上也是如此。



Note that, since your friend is running XP sp3, you can't use .NET 4.5. .NET 4.5 is not supported on Windows XP. You will need to change your application to target .NET 4.0, which will then work on the XP machine (if he installs the 4.0 framework).

请注意,由于您的朋友运行的是 XP sp3,您不能使用 .NET 4.5。Windows XP 不支持 .NET 4.5。您需要将应用程序更改为面向 .NET 4.0,然后该应用程序将在 XP 计算机上运行(如果他安装了 4.0 框架)。

回答by Carsten Schütte

The Platform name (shown on the top of the properties, page "Build") is only a name. The same for "Active Solution Platform" in configuration editor. This is a bit confusing.

平台名称(显示在属性顶部的“构建”页面)只是一个名称。配置编辑器中的“Active Solution Platform”也是如此。这有点令人困惑。

You have to make sure that the "Target platform" setting is really set to "AnyCPU" or "x86".

您必须确保“目标平台”设置确实设置为“AnyCPU”或“x86”。

回答by Mahmut EFE

I had same problem. I strugle with it. and the and I found a solution. The solution is :

我有同样的问题。我与它斗争。我找到了解决方案。解决办法是:

Firstly choose platform target "x86". After that build your project as "release mode" not "Debug mode". finally you can run on any platform (32 bit or 64 bit).

首先选择平台目标“ x86”。之后将您的项目构建为“发布模式”而不是“调试模式”。最后,您可以在任何平台(32 位或 64 位)上运行。

回答by Micha?l

If none of the solutions presented above helped you, then try the following: Open the project properties and click General in the left column. Change Platform Toolset to the one with Windows XP in it.

如果以上提供的解决方案都没有帮助您,请尝试以下操作: 打开项目属性并单击左列中的常规。将平台工具集更改为装有 Windows XP 的平台工具集。

For example, in Visual Studio 2015 it will default be set to "Visual Studio 2015 (v140)". To be able to run on Windows XP, you have to change this to "Visual Studio 2015 - Windows XP (v140_xp)".

例如,在 Visual Studio 2015 中,它将默认设置为“Visual Studio 2015 (v140)”。为了能够在 Windows XP 上运行,您必须将其更改为“Visual Studio 2015 - Windows XP (v140_xp)”。

Now do a full rebuild, and the exe should work on Windows XP.

现在进行完全重建,该 exe 应该可以在 Windows XP 上运行。