5

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.

Community
  • 1
  • 1
Kairan
  • 5,342
  • 27
  • 65
  • 104
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 31 '13 at 19:06
  • 1
    "Any CPU" should let it run on... any CPU. Are you trying to make it _only_ run 32-bit even on a 64-bit platform or is the problem that it won't run on 32-bit at all? – Joachim Isaksson Jan 31 '13 at 19:07
  • 3
    "Not a valid win32 application" generally indicates that the target machine does not have the .net framework installed. – Sam Axe Jan 31 '13 at 19:09
  • @Dan-o I don't think so. What makes you say that? – David Heffernan Jan 31 '13 at 19:21
  • 1
    David is right, when .NET framework is missing you get a message that mscoree.dll could not be found. – Carsten Schütte Jan 31 '13 at 19:23
  • What operating system is the 32 bit machine? – Reed Copsey Jan 31 '13 at 19:27
  • 1
    @DavidHeffernan: I've seen the message before in my own adventures - and I *thought* it was due to the netfx not being installed - but its been so long ago I can't remember for sure. I'm willing to defer to your judgement here and say I have a faulty memory on this topic. – Sam Axe Jan 31 '13 at 19:28
  • @Dan/@David: I think I was rememebering the same, but MSDN told me different some minutes ago. – Carsten Schütte Jan 31 '13 at 19:29
  • @CarstenSchütte Any chance of you posting that link. – David Heffernan Jan 31 '13 at 19:32
  • @Dan-o: .net framework ver could be the case, its something my prof suggested, you would think the error would be different like Carsten mentioned. – Kairan Jan 31 '13 at 19:58
  • @ReedCopsey: I have Win 7 64-Bit, my prof thinks also that since I am on 64-bit it automatically compiles to 64-bit, but that wouldnt make sense if I changed the settings I mentioned in my post. The testing operating system is XP Pro SP3 and he did indicate no .NET 4.5, i think he has an older version – Kairan Jan 31 '13 at 19:59
  • @Kairan You'll need to target .NET 4, not 4.5. – Reed Copsey Jan 31 '13 at 20:20
  • @David: Already closed the browser yesterday, and today I inly found a german page: http://www.matrix44.net/old/runtime_part1.html - but is has a screenshot of the message. Please not that this is for .NET 1.1, so really old. – Carsten Schütte Feb 01 '13 at 09:20

5 Answers5

6

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

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    Windows XP does not support .NET 4.5, so if the 32 bit machine you're building for is a XP machine, you'll need to use .NET 4.0. – jessehouwing Jan 31 '13 at 19:26
  • I am going to try to change the .NET ver for my app and if this fixes I will select your solution as I think this may be reason, my friend pc is not using .NET 4.5, he is using XP SP3 as well - if that has any reason to the problem – Kairan Jan 31 '13 at 20:02
  • @Kairan You can't use .NET 4.5 with XP - so you'll need to target .NET 4, and have him install .NET 4. – Reed Copsey Jan 31 '13 at 20:19
3

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

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

Carsten Schütte
  • 4,408
  • 1
  • 20
  • 24
0

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

Mahmut EFE
  • 5,137
  • 5
  • 46
  • 56
0

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.

Michaël
  • 357
  • 3
  • 9