简体   繁体   中英

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

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.

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)

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

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

My above was similar to what was done here: 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.

Setting your project to target AnyCPU should 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 AnyCPU or 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).

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


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

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.

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.

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.

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

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM