16

What does the Any CPU - Prefer 32 bit option do?

While I am aware that WinRT can not handle exe and can only run Windows Store apps, there are several questions exist on StackOverflow that ask the same question and both reference this blog that says:

In .NET 4.5 and Visual Studio 11 the cheese has been moved. The default for most .NET projects is again AnyCPU, but there is more than one meaning to AnyCPU now. There is an additional sub-type of AnyCPU, “Any CPU 32-bit preferred”, which is the new default (overall, there are now five options for the /platform C# compiler switch: x86, Itanium, x64, anycpu, and anycpu32bitpreferred). When using that flavor of AnyCPU, the semantics are the following:

  • If the process runs on a 32-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.
  • If the process runs on a 64-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.
  • If the process runs on an ARM Windows system, it runs as a 32-bit process. IL is compiled to ARM machine code.

However, after purchasing my Surface RT, I created a Hello World program, set it to Any CPU, checked the Prefer 32 Bit flag, compiled and copied it to my Surface. When I ran the program the OS told me that it could not run the program and that I should look to the marketplace as it would for any x86/x64 exe. The exact message displayed was: "This app can't run on your PC. To find apps for this PC, open the Windows Store."

So what does this actually do and is it possible to compile an Any CPU application for Window RT on ARM?

Community
  • 1
  • 1
Dragonseer
  • 2,874
  • 7
  • 29
  • 42
  • 1
    What was the *reason* the OS told you it couldn't run the program? Was it specifically due to CPU incompatibility, or was it some other reason? – Greg Hewgill Oct 31 '12 at 01:15
  • Updated question. The exact message displayed was: "This app can't run on your PC. To find apps for this PC, open the Windows Store." The same message when trying to run other exe's, such as the Chrome installer. – Dragonseer Oct 31 '12 at 01:25
  • 4
    Windows RT cannot run desktop apps (normal .exe files). you have to create a marketplace app in order to be able to run it on the surface. – m0sa Oct 31 '12 at 01:30
  • @m0sa: Correct and this has been my understanding as well. However, when I came up the blog post and StackOverflow questions, I decided to look into what Any CPU - 32 bit preferred actually does. The blog post is cited several places on the internet as well as here on StackOverflow, so I'm trying to determine if there is some merit to it. – Dragonseer Oct 31 '12 at 01:33
  • Just get a developer license: http://msdn.microsoft.com/en-us/library/windows/apps/hh974578.aspx so you can deploy your app to your machine – NotMe Oct 24 '13 at 02:25
  • Related: [What is the purpose of the “Prefer 32-bit” setting in Visual Studio 2012 and how does it actually work?](https://stackoverflow.com/q/12066638/4975230) – jrh Jul 19 '18 at 13:07

2 Answers2

16

Based solely on the quote you specified, it suggests that it means the following:

  • If the CPU supports 32-bit processing, then the final machine code will be 32-bit (ARM or x86, doesn't matter).

  • If not, then the machine code will be 64-bit.

In the days of old, AnyCPU meant "I am platform agnostic." This typically meant that you would get 64-bit on 64-bit platforms, and 32-bit otherwise.

However, there are reasons why you may want 32-bit even on a 64-bit CPU: Pointers are half the size, so you use less memory; code is smaller, so more fits into the cache, etc. But, there was no way to force the CLR to use 32-bit, and still retain 64-bit in the cases it was necessary.

Now, with AnyCPU - 32 bit preferred, you can have your cake (64-bit support when necessary) and eat it too (32-bit support when possible)

Note: If I am not mistaken, Itanium would be a platform that only supports 64-bit, not 32-bit code. There may or may not be others.

Disclaimer: I am not an expert, this is just from various blog posts that I've read over the last 6 months or so

Mike Caron
  • 14,351
  • 4
  • 49
  • 77
  • 3
    WoW64 is an optional install on servers – Mark Sowul Nov 23 '13 at 01:29
  • It would be so cool if the user had a choice whether one day he is low on memory and just wants an app to operate against small data, so 32-bit would be sufficient, and another day he could be able to run the AnyCPU app with full 64 bit support. Or an app using DirectShow (or other similar framework) can (will!) behave totally different using x86 or x64. – springy76 Jan 28 '14 at 12:41
  • It's my understanding that Ia64 is not compatible with regular x64 though -- you have to specifically target Ia64, right? – BrainSlugs83 Sep 01 '14 at 23:16
  • The debate if to use 32-bit before 64-bit or vice versa has no real good answer yet. There are pros and cons from both sides as where 64-bit processors handles 64-bit variables faster than 32-bit variables, but they take more memory etc. – einord Mar 03 '15 at 09:52
1

Windows RT requires exe files to be signed for it to run. But if you jailbreak your RT then it can run any exe file as long as it is supported on Windows on ARM (.NET or native ARM) apps

More on this option on What AnyCPU Really Means As Of .NET 4.5 and Visual Studio 11

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • "the cheese has been moved" -- linking to the blog post which raised this question could create a stack overflow ;-) – springy76 Jan 28 '14 at 11:05