tags:

views:

1154

answers:

5

I'd like to offer my users correct links to an upgraded version of my program based on what platform they're running on, so I need to know whether I'm currently running on an x86 OS or an x64 OS.

The best I've found is using Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"), but I would think there would be some built-in facility for this?

A: 

Check the size of IntPtr with Marshal.SizeOf. 32 bit = 4 bytes, 64 bit = 8 bytes.

Edit: I am not sure this is what you are looking for after reading the question again.

leppie
This would differentiate between a 32-bit and a 64-bit platform, although the 64-bit platform could be, say, Itanium and not x64. (Unlikely, I know, but possible.)
millenomi
You don't need to use Marshal.SizeOf, you can just use IntPtr.Size
Wilka
-1: This tells whether this .NET process is x64
Ruben Bartelink
A: 

Check just IntPtr.Size . You need to have target platform as AnyCPU.

from here

Jakub Kotrla
-1: Duplicate of Leppie's answer (and wrong for same reason)
Ruben Bartelink
(I realise its different, but the best place to point that out is in a comment on the other answer. But I also realize that there probably werent even comments back in the day and/or you didnt have enough rep to make them...)
Ruben Bartelink
+3  A: 

Call IsWow64Process to find out if your 32-bit process is running in WOW64 on a 64-bit operating system. You can call GetNativeSystemInfo to find out exactly what it is: the wProcessorArchitecture member of SYSTEM_INFO will be PROCESSOR_ARCHITECTURE_INTEL for 32-bit, PROCESSOR_ARCHITECTURE_AMD64 for x64 and PROCESSOR_ARCHITECTURE_IA64 for Intel's Itanium.

Mike Dimmick
+1: Explicit about what each call does, not misleading
Ruben Bartelink
A: 

You can determine a lot via environment variables as used in http://stackoverflow.com/questions/194157/c-how-to-get-program-files-x86-on-vista-x64/194223#194223 [And this happened to suit me better than Mike's answer which I +1'd as I happen to be interested in finding the Program Files directory name]

Ruben Bartelink
+3  A: 

Environment.Is64BitOperatingSystem and Environment.Is64BitProcess are being introduced in .NET 4. For .NET 2 you'll need to try out some of the other answers.

Richard Szalay