What AnyCPU Really Means As Of .NET 4.5 and Visual Studio 11
Join the DZone community and get the full member experience.
Join For Freethe 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…):
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
Opinions expressed by DZone contributors are their own.
Comments