DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • Your Application Has an Unindexed Attack Surface. Do You Know What’s in It?
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector

Trending

  • Gitember: Building a Git Client Beyond Commit, Push, and Pull
  • Gen AI Debugging Tools Compared: Genkit vs Vercel AI SDK vs Mastra
  • Why 200 OK Is the Most Dangerous Lie in Enterprise Integration
  • PWA on iOS, Part 3: Chrome on iPhone Is WebKit Too. How One Engine Sets the Rules for PWAs on iOS

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

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

By 
Sasha Goldshtein
·
Apr. 07, 12 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
40.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

64-bit 32-bit application

Opinions expressed by DZone contributors are their own.

Related

  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • Your Application Has an Unindexed Attack Surface. Do You Know What’s in It?
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook