DCSIMG
What AnyCPU Really Means As Of .NET 4.5 and Visual Studio 11 - All Your Base Are Belong To Us

All Your Base Are Belong To Us

Mostly .NET internals and other kinds of gory details

What AnyCPU Really Means As Of .NET 4.5 and Visual Studio 11

The 32-bit and 64-bit development story on Windows seemingly never stops causing problems for developers. It’s been a decade since 64-bit processors have started popping up in the Windows consumer environment, but we just can’t get it right. If you forget some of the gory details, here are a couple of reminders:

  • On a 64-bit Windows system, both the 32-bit and 64-bit versions of system DLLs are stored. The 64-bit DLLs are in C:\Windows\System32, and the 32-bit DLLs are in C:\Windows\SysWOW64.
  • When a 32-bit process opens a file in C:\Program Files, it actually reads/writes to C:\Program Files (x86).
  • There are separate views of (most of) the registry for 32-bit and 64-bit applications. You can change the 64-bit registry location and it wouldn’t be visible to 32-bit applications.

These differences are hardly elegant as they are, but they allow 32-bit applications to run successfully on a 64-bit Windows system. While unmanaged applications always had to choose the native target—x86, x64, or ia64 in the Visual Studio case—managed code has the additional choice of AnyCPU.

What AnyCPU used to mean up to .NET 4.0 (and Visual Studio 2010) is 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 64-bit process. IL is compiled to x64 machine code.
  • If the process runs on an Itanium Windows system (has anyone got one? ;-)), it runs as a 64-bit process. IL is compiled to Itanium machine code.

Prior to Visual Studio 2010, AnyCPU was the default for most .NET projects, which was confusing to some developers: when they ran the application on a 64-bit Windows system, the process was a 64-bit process, which may cause unexpected results. For example, if the application relies on an unmanaged DLL of which only a 32-bit version is available, its 64-bit version won’t be able to load that component.

In Visual Studio 2010, x86 (and not AnyCPU) became the default for most .NET projects—but otherwise the semantics haven’t changed.

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.

The difference, then, between “Any CPU 32-bit preferred” and “x86” is only this: a .NET application compiled to x86 will fail to run on an ARM Windows system, but an “Any CPU 32-bit preferred” application will run successfully.

To inspect these changes, I created a new C# console application in Visual Studio 11 that prints the values of Environment.Is64BitOperatingSystem and Environment.Is64BitProcess. When I ran it on my 64-bit Windows system, the result was as follows:

Is64BitOperatingSystem = True
Is64BitProcess         = False

Inspecting the project’s properties shows the following (in the current Visual Studio UI “Prefer 32-bit” is grayed out and unchecked, where in actuality it is enabled…):

image

Inspecting the executable with CorFlags.exe shows the following:

Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32
CorFlags  : 131075
ILONLY    : 1
32BITREQ  : 0
32BITPREF : 1
Signed    : 0

After changing the 32BITPREF setting with CorFlags.exe (using the /32bitpref- option), the output was as follows:

Is64BitOperatingSystem = True
Is64BitProcess         = True


I am posting short updates and links on Twitter as well as on this blog. You can follow me:@goldshtn

Comments

Olivier said:

Great article! By the way, I really miss the VS2008 behavior that defaulted to Any Cpu for all types of projects... I like being able to set the configuration by myself (and I hate those Mixed platforms being generated)! Seems it'll be the case with VS11

# April 4, 2012 1:14 PM

Richard Thorne said:

I assume the "Prefer 32bit" box not being checked is a bug in VS? Otherwise this is distinctly unintuitive and backwards :-/

# April 5, 2012 3:27 AM

Adam Graves said:

Don't you have this backward?

On a 64-bit Windows system, both the 32-bit and 64-bit versions of system DLLs are stored. The 64-bit DLLs are in C:\Windows\System32, and the 32-bit DLLs are in C:\Windows\SysWOW64.

# April 5, 2012 4:32 PM

Springy said:

So without the use of CorFlags.exe all my AnyCPU applications which REALLY need to be able to consume more than 3 GB of RAM when run on adequate hardware will out of a sudden stop working as expected?

I hope Microsoft will introduce an option on the "Compatibility" tab (Explorer -> EXE -> Properties) which allows to overwrite both an existing and a non-existing 32BITPREF flag: Forcing a 32BITPREF=1 app to run on 64-bit anyways or forcing an old AnyCPU app to run as 32-bit, just as .NET4-x64 would have been uninstalled.

# April 5, 2012 6:52 PM

Sasha Goldshtein said:

@Richard: Yes, I think it's a bug.

@Springy: I haven't tested it, but I think that when you convert an existing AnyCPU project to .NET 4.5 it will retain the original AnyCPU behavior. If you stay with .NET 4.0 it will be the old AnyCPU behavior anyway.

@Adam: No, unfortunately it's Microsoft that have it backwards :-) Mainly for compatibility reasons. SysWOW64 is named after "Windows on Windows 64", which is the layer that allows 32-bit processes to run on 64-bit Windows.

# April 9, 2012 12:30 PM

Object Synergy said:

Its true. I assume the "Prefer 32bit" box not being checked is a bug in VS? Otherwise this is distinctly unintuitive and backwards :-/

# April 10, 2012 3:51 PM

Noam Sheffer said:

There is more,

When AnyCPU application is located under "C:\Program Files" the loader will start the x64 CLR and when located under "C:\Program Files (x86)" it will start as x86

# April 19, 2012 9:59 AM

AceHack said:

Their is one thing you forgot to mention about the difference between x86 and AnyCPU 32-bit perferred.  When hosted in IIS and the app pool "Enable 32-bit Applications" is disabled then a x86 app will fail to run but a AnyCPU 32-bit perferred app will run in 64-bit mode.  Thanks.

# June 4, 2012 8:59 PM

zf said:

With the strategy of defaults x86 in VS2010 and 32b-preferred in VS2012 Windows for PCs are going to be never free from the burden of 32bit apps backward compatibility.

This is going to be insane in perspective of 5-10 years, such as is taking care of 16-bits apps nowdays.

# June 12, 2012 2:29 PM

Pablo Montilla said:

It would appear that 32 bit required can only be applied to EXEs and not to DLLs. That's why you see the checkbox grayed out.

# October 18, 2012 2:04 AM

Sasha Goldshtein said:

@Pablo: This was a console application (so an EXE). However, this bug was fixed in RTM.

# October 24, 2012 8:01 AM

Woody said:

@Sasha Goldshtein in re: @Pablo

If a Windows Forms Applications first built as x86 then changed to AnyCPU, the Prefer 32-bit checkbox remains grayed out.

# October 31, 2012 9:49 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: