Kris Stanton's WebLog

The things I learn working for the CLR team

Simple way to check if you're on a 64-bit machine

In some of my tests, I need to check if I'm on a 64-bit machine or not.  Yesterday, I spent some time looking through the System.Environment class or System.Diagnostics class to see if there was a static method or something that I could call to check if the machine I was on was a 64-bit machine.  I was thinking to PInvoke to GetSystemInfo, but gosh darn it, there had to be an easier way!  I talked to Josh Williams again (is it hard to tell we hang out?), and he said to just do a IntPtr.Size (static method) and since IntPtr is designed to be an integer whose size is platform specific, it will be 8 if you are on a 64-bit machine and 4 if you are on a 32-bit machine.  Duh!  I was so amazed at how simple this was that I wanted to share it with y'all...
Published Tuesday, April 20, 2004 10:50 AM by kstanton

Comments

 

Josh Williams said:

There has been discussion about whether or not to add some APIs to System.Environment or System.Diagnostics to allow one to deduce whether or not they are on a 32bit or 64bit system... Right now we don't have any with the caveat that if you _really_ need to know then maybe you should be thinking about your design... Should you really be using seperate 32bit and 64bit assemblies that run in 32bit or 64bit processes with your bitness specific knowledge compiled explicitly into those assemblies? Is there a better way to do what you're doing such that it doesn't have to be bitness specific?

That said, there are some times when it is hard to get around the need for that knowledge, and for those times as Kris mentions above there is IntPtr.Size, mind you this is significantly faster than using Marshal.SizeOf(typeof(IntPtr)) (or whatever the proper code is... ya'll get the point) as IntPtr.Size is a hard coded property of the IntPtr that is conditionally compiled to be either 4 or 8 depending on whether you're in a process that loaded up the 32bit version of mscorlib.dll or the 64bit version...

-josh
April 20, 2004 11:04 AM
 

Scott Sargent said:

Just a quick question in regards to integer size and 32/64 bit. We're trying to find the definitive answer to a question about the size of an integer. In the x86/32 framework a C# integer is defined as System.Int32. Does this change on a 64bit machine, is it System.Int64 or does it remain System.Int32?

-- Scott
April 27, 2004 7:27 AM
 

Kris Stanton said:

I double checked with Josh, and yes in C# and VB, "int" is System.Int32 no matter if you are on a 32bit or 64bit machine. You would have to specify Int64 if you wanted a 64bit int, or use IntPtr for it to change depending on the platform.
April 27, 2004 11:57 AM
 

Dirk's Blog said:

March 5, 2006 5:08 PM
 

Feed Search Engine - All Fresh Articles And News Are Here said:

November 25, 2007 6:06 PM
 

Drugs and Movies » Kris Stanton’s WebLog : Simple way to check if you’re on a 64-bit machine said:

April 16, 2008 8:57 PM
Anonymous comments are disabled

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker