4

In C#, how can I know programmatically if the Operating system is x64 or x86

I found this API method on the internet, but it doesn't work

[DllImport("kernel32.dll")]
public static extern bool IsWow64Process(System.IntPtr hProcess, out bool lpSystemInfo);

public static bool IsWow64Process1
{
   get
   {
       bool retVal = false;
       IsWow64Process(System.Diagnostics.Process.GetCurrentProcess().Handle, out retVal);
       return retVal;
   }
}

Thanks in advance.

Homam
  • 23,263
  • 32
  • 111
  • 187
  • what is the error it returns? could you post the return value of the IsWow64Process API? Also, is your .NET DLL compiled as AnyCPU? – obelix Aug 14 '10 at 13:55
  • 1
    also, try a search - bing returns http://stackoverflow.com/questions/336633/how-to-detect-windows-64-bit-platform-with-net as a result. – obelix Aug 14 '10 at 13:57
  • @obelix: There's no error, but my OS version is 64 and it returns false – Homam Aug 14 '10 at 13:57
  • Homam, if you compiled for AnyCPU, then your process may well be a native 64bit process and not under WoW64. That function may well be false validly on a 64bit OS. You've only checked whether your process is being emulated- it may be native. – Puppy Aug 14 '10 at 14:01
  • possible duplicate of [Right way to detect cpu architecture?](http://stackoverflow.com/questions/2017409/right-way-to-detect-cpu-architecture) – Hans Passant Aug 14 '10 at 15:03

5 Answers5

8

In .NET 4.0 you can use the new Environment.Is64BitOperatingSystem property.

And this is how it's impemented

public static bool Is64BitOperatingSystem
{
    [SecuritySafeCritical]
    get
    {
        bool flag;
        return ((Win32Native.DoesWin32MethodExist("kernel32.dll", "IsWow64Process") && Win32Native.IsWow64Process(Win32Native.GetCurrentProcess(), out flag)) && flag);
    }
}

Use reflector or similar to see exactly how it works.

Jesper Palm
  • 7,170
  • 31
  • 36
2

bool x86 = IntPtr.Size == 4;

thelost
  • 6,638
  • 4
  • 28
  • 44
  • 5
    this might not be correct - http://stackoverflow.com/questions/336633/how-to-detect-windows-64-bit-platform-with-net – obelix Aug 14 '10 at 13:58
  • 2
    This will show you whether you're running on 32bit **or** you're running under WOW64 on 64bit. It won't give you the actual *platform*, but will tell you the current runtime environment. – Reed Copsey Sep 04 '12 at 23:03
1

If you build against AnyCPU, and you run on a 64-bit system, it will run on the 64-bit version of the framework. On a 32-bit system, it will run on the 32-bit version of the framework. You can use this to its advantage by simply checking the IntPtr.Size property. If the Size = 4, you are running on 32-bit, Size = 8, you are running on 64-bit.

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
  • This has gotten more complicated with .NET 4.5. The default is now "Any CPU (32-bit preferred)". See http://blogs.microsoft.co.il/blogs/sasha/archive/2012/04/04/what-anycpu-really-means-as-of-net-4-5-and-visual-studio-11.aspx – Hank May 07 '13 at 17:39
0

Have a look at this:

http://msdn.microsoft.com/en-us/library/system.environment_members.aspx

I think you are looking for System.Environment.OSVersion

Burt
  • 7,680
  • 18
  • 71
  • 127
0

The following is from this answer, so don't upvote me for it :)

if (8 == IntPtr.Size
    || (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
{
    // x64
}
else
{
    // x86
}
Community
  • 1
  • 1
Niels van der Rest
  • 31,664
  • 16
  • 80
  • 86