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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The RISC-V Architecture
  • Auto-Instrumentation in Azure Application Insights With AKS
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide
  • Why Mocking Sucks

Trending

  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Emerging Data Architectures: The Future of Data Management
  • Rust and WebAssembly: Unlocking High-Performance Web Apps
  • Segmentation Violation and How Rust Helps Overcome It

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

By 
Sasha Goldshtein user avatar
Sasha Goldshtein
·
Apr. 07, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
39.1K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • The RISC-V Architecture
  • Auto-Instrumentation in Azure Application Insights With AKS
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide
  • Why Mocking Sucks

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!