none
How to check whether the system is 32 bit or 64 bit ?

    Question

  • Hi,

     

    I want to know programmatically whether the system is 32 bit or 64 bit in C#. My program has been compiled for 32 bit for sure but it is not working on 64 bit, so i need to put some checks.

     

    Can someone help ?

     

    I have seen the post in MSDN blogs itself but couldnt find it working for C#.

     

    Thanks in advance.

     

    Kulvinder Singh

    Thursday, July 19, 2007 4:58 AM

Answers

  • It's pretty amazing how many people get this one wrong. There is a lot of misinformation on the subject. The following code works and will not crash on certain operating systems (because it checks for the existance of IsWow64Process which does not exist on earlier OSes).

    public static class Wow
    {
      public static bool Is64BitProcess
      {
        get { return IntPtr.Size == 8; }
      }
    
      public static bool Is64BitOperatingSystem
      {
        get
        {
          // Clearly if this is a 64-bit process we must be on a 64-bit OS.
          if (Is64BitProcess)
            return true;
          // Ok, so we are a 32-bit process, but is the OS 64-bit?
          // If we are running under Wow64 than the OS is 64-bit.
          bool isWow64;
          return ModuleContainsFunction("kernel32.dll", "IsWow64Process") && IsWow64Process(GetCurrentProcess(), out isWow64) && isWow64;
        }
      }
    
      static bool ModuleContainsFunction(string moduleName, string methodName)
      {
        IntPtr hModule = GetModuleHandle(moduleName);
        if (hModule != IntPtr.Zero)
          return GetProcAddress(hModule, methodName) != IntPtr.Zero;
        return false;
      }
    
      [DllImport("kernel32.dll", SetLastError=true)]
      [return:MarshalAs(UnmanagedType.Bool)]
      extern static bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] out bool isWow64);
      [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
      extern static IntPtr GetCurrentProcess();
      [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
      extern static IntPtr GetModuleHandle(string moduleName);
      [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError=true)]
      extern static IntPtr GetProcAddress(IntPtr hModule, string methodName);
    }
    
    Monday, June 21, 2010 3:16 AM
  • 1[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] 
    2[return: MarshalAs(UnmanagedType.Bool)] 
    3public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo); 
    4 
    5public bool Is64Bit() 
    6
    7    bool retVal; 
    8 
    9    IsWow64Process(Process.GetCurrentProcess().Handle, out retVal); 
    10 
    11    return retVal; 
    12



    After a couple minutes digging around and finding the various posts, this simple section of code told me if the current process is running on a 64 bit OS and is being emulated for 32 bit.
    • Proposed as answer by ZaChickster Tuesday, December 09, 2008 4:58 PM
    • Marked as answer by Martin Xie - MSFT Wednesday, December 10, 2008 2:28 AM
    Tuesday, December 09, 2008 4:58 PM
  •  

     

     

     You can find out using  IntPtr.Size (static method)

     

    Try the following link

    http://blogs.msdn.com/kstanton/archive/2004/04/20/116923.aspx

    Thursday, July 19, 2007 5:48 AM
  • Please check this thread and this blog which you might have seen.

     

    How to detect programmatically 32/64-bit Windows?

     

    BOOL Is64BitWindows()
    {
    #if defined(_WIN64)
    return TRUE;  // 64-bit programs run only on Win64
    #elif defined(_WIN32)
    // 32-bit programs run on both 32-bit and 64-bit Windows
    // so must sniff
    BOOL f64 = FALSE;
    return IsWow64Process(GetCurrentProcess(), &f64) && f64;
    #else
    return FALSE; // Win64 does not support Win16
    #endif
    }

    Monday, July 23, 2007 6:26 AM

All replies

  •  

     

     

     You can find out using  IntPtr.Size (static method)

     

    Try the following link

    http://blogs.msdn.com/kstanton/archive/2004/04/20/116923.aspx

    Thursday, July 19, 2007 5:48 AM
  • Please check this thread and this blog which you might have seen.

     

    How to detect programmatically 32/64-bit Windows?

     

    BOOL Is64BitWindows()
    {
    #if defined(_WIN64)
    return TRUE;  // 64-bit programs run only on Win64
    #elif defined(_WIN32)
    // 32-bit programs run on both 32-bit and 64-bit Windows
    // so must sniff
    BOOL f64 = FALSE;
    return IsWow64Process(GetCurrentProcess(), &f64) && f64;
    #else
    return FALSE; // Win64 does not support Win16
    #endif
    }

    Monday, July 23, 2007 6:26 AM
  • Hi Martin,

     

    Thanks for the reply.

     

    I have two applications.

     

    (a) A Window App on which the code is working fine to give the OS Version as intended. The OS is 64 bit and it rightly displays

    (b) An Outlook VSTO Addin (A plugin which gets added in Outlook 2007) but the same code says that the OS is 32 bit.

     

    Both of the above applications have been developed on a 32 bit machine.

     

    Any ideas ?

     

     

     

    Thursday, August 02, 2007 8:28 AM
  • It's actually telling you if the Process is 32 bit or 64 bit, rather than the underlying operating system.

    I assume that Outlook 2007 is running as a 32-bit process, therefore the Addin is also running as 32-bit.
    Thursday, August 02, 2007 8:41 AM
  • Thanks for the reply Matthew.

     

    Can you help me in telling how can i check whether the OS is 32 bit or 64 bit ? My addin is being loaded in Outlook 2007 which is 32 bit. How do my Addin knows that the running OS is 64 bit or 32 bit ?

     

    Regards

    Kulvinder Singh

     

    Thursday, August 02, 2007 8:55 AM
  • Hi Martin,

     

    Can you write the code below in C# ?

     

    How to detect programmatically 32/64-bit Windows?

     

    BOOL Is64BitWindows()
    {
    #if defined(_WIN64)
    return TRUE;  // 64-bit programs run only on Win64
    #elif defined(_WIN32)
    // 32-bit programs run on both 32-bit and 64-bit Windows
    // so must sniff
    BOOL f64 = FALSE;
    return IsWow64Process(GetCurrentProcess(), &f64) && f64;
    #else
    return FALSE; // Win64 does not support Win16
    #endif
    }

     

     

    Regards

    Kulvinder Singh

    Thursday, August 02, 2007 8:57 AM
  • Hi Kulvinder,

     

    We can't write such code in C# bucause of no preprocess mechanism.

     

    However, I would like to suggest you to make a DLL to encapsulate such function, then call it in C# by P/Invoke approach.

     

     

    Thanks,

    Martin Xie

    Friday, August 03, 2007 8:36 AM
  • Hi Martin,

     

    I finally wrote the code in VC++.NET but the problem is that even though i added the header file stdio.h and windows.h in my CPP project and added a kernel32.dll in the linker, i am not able to compile the code.

     

    The compiler is not recognizing the "IsWow64Process" method. What do i need to you ?

    Friday, August 03, 2007 2:47 PM
  • I had a requirement to check whether a remote machine is x64 or x32. I used the following KB.
    http://support.microsoft.com/?id=888731

    It will work on local machine as well.
    Tuesday, September 09, 2008 2:46 PM
  • 1[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] 
    2[return: MarshalAs(UnmanagedType.Bool)] 
    3public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo); 
    4 
    5public bool Is64Bit() 
    6
    7    bool retVal; 
    8 
    9    IsWow64Process(Process.GetCurrentProcess().Handle, out retVal); 
    10 
    11    return retVal; 
    12



    After a couple minutes digging around and finding the various posts, this simple section of code told me if the current process is running on a 64 bit OS and is being emulated for 32 bit.
    • Proposed as answer by ZaChickster Tuesday, December 09, 2008 4:58 PM
    • Marked as answer by Martin Xie - MSFT Wednesday, December 10, 2008 2:28 AM
    Tuesday, December 09, 2008 4:58 PM
  • return false on my VISTA X64... any idea?
    Wednesday, March 04, 2009 9:26 PM
  •  "return false on my VISTA X64... any idea?"

    Do you have x86 targeted?  If not, AnyCpu should return false.
    • Proposed as answer by Paolo72 Wednesday, March 04, 2009 11:45 PM
    Wednesday, March 04, 2009 9:55 PM
  • isnt that C++?
    Tuesday, November 24, 2009 4:45 PM
  • finally found a solution to this


    Console

     

    .WriteLine(System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"));

    if its a 64-bit machine that has 32-bit OS installed, it returns x86

    if its a 64-bit machine that has 64-bit os , it returns AMD64.


    I will add code to say if contains 64, System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE)  then it is a 64-bit OS!!!!!!!! 

    • Proposed as answer by Matthew Watson Wednesday, November 25, 2009 9:38 AM
    Tuesday, November 24, 2009 4:51 PM
  • Ronan001, i was wondering if you could help me with a problem Im having... your answer is closest to what im looking for.

    Basically i'm trying to create a new folder in the 'Program Files' folder. But because my program is going to be distributed it may well be used on either 32 Bit OS's or 64 Bit OS's.

    Heres what i want the code to do:

    I want it to find weather the system is 32 Bit or 64 Bit. If its 32 Bit i want it to change the text in a TextBox or Label. If its a 64 Bit OS i want it to change the text in a TextBox or Label aswell. I want it in an 'If' statement if possible.



    I would really appreciate it if you could help me with this, im new to VB and not great with it :)
    Wednesday, December 23, 2009 1:20 AM
  • Best way to do that would be target your app to x86

    Then use something like this

    string Loc = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
    
    Directory.CreateDirectory(Loc + @"\YourFolderName");

    Using this, 64 bit WOW should auto change it to x86 program files folder and normal program files folder on 32bit

    Thus making it compatible with both.

    Hope this helps

    • Edited by Dermot2k Saturday, May 08, 2010 10:07 PM IE formatted code badly
    Saturday, May 08, 2010 10:06 PM
  • With the arrival of .Net 4, we now have two new methods (click for details):

    Environment.Is64BitOperatingSystem()

    Environment.Is64BitProcess()

    Monday, May 10, 2010 8:49 AM

  • .WriteLine(System. Environment .GetEnvironmentVariable( "PROCESSOR_ARCHITECTURE" ));

    if its a 64-bit machine that has 32-bit OS installed, it returns x86
    if its a 64-bit machine that has 64-bit os , it returns AMD64.
    ....

    The above statement is not entirely correct.

    Environment variable PROCESSOR_ARCHITECTURE is always x86 on 32-bit Windows OS, regardless or processor type, even if the OS is installed on 64-bit hardware.

    On 64-bit Windows, PROCESSOR_ARCHITECTURE has different values in different processes. It is x86 for 32-bit processes , and is AMD64 only for 64-bit processes .

    See http://blogs.msdn.com/b/david.wang/archive/2006/03/26/howto-detect-process-bitness.aspx

     

    Monday, June 21, 2010 1:28 AM
  • It's pretty amazing how many people get this one wrong. There is a lot of misinformation on the subject. The following code works and will not crash on certain operating systems (because it checks for the existance of IsWow64Process which does not exist on earlier OSes).

    public static class Wow
    {
      public static bool Is64BitProcess
      {
        get { return IntPtr.Size == 8; }
      }
    
      public static bool Is64BitOperatingSystem
      {
        get
        {
          // Clearly if this is a 64-bit process we must be on a 64-bit OS.
          if (Is64BitProcess)
            return true;
          // Ok, so we are a 32-bit process, but is the OS 64-bit?
          // If we are running under Wow64 than the OS is 64-bit.
          bool isWow64;
          return ModuleContainsFunction("kernel32.dll", "IsWow64Process") && IsWow64Process(GetCurrentProcess(), out isWow64) && isWow64;
        }
      }
    
      static bool ModuleContainsFunction(string moduleName, string methodName)
      {
        IntPtr hModule = GetModuleHandle(moduleName);
        if (hModule != IntPtr.Zero)
          return GetProcAddress(hModule, methodName) != IntPtr.Zero;
        return false;
      }
    
      [DllImport("kernel32.dll", SetLastError=true)]
      [return:MarshalAs(UnmanagedType.Bool)]
      extern static bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] out bool isWow64);
      [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
      extern static IntPtr GetCurrentProcess();
      [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
      extern static IntPtr GetModuleHandle(string moduleName);
      [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError=true)]
      extern static IntPtr GetProcAddress(IntPtr hModule, string methodName);
    }
    
    Monday, June 21, 2010 3:16 AM
  • This is not reliable if you want to find whether it is a 64 bit operating system.

    IntPtr.Size is dependant on the process rather than machine.

    If you are running a 32 bit(x86) process on 64 bit machine, IntPtr.Size still returns 4 and not 8.

    Wednesday, June 01, 2011 6:26 AM
  • If you are talking about Tergiver's code, then you are wrong. He is explicitly handling the case where IntPtr.Size returns 4 by going on to call ModuleContainsFunction() to see if he can call IsWow64Process, and then going on to call it if so.

    However, as I stated before, you can just call the following methods from .Net 4:


    Environment.Is64BitOperatingSystem()

    Environment.Is64BitProcess()

    Wednesday, June 01, 2011 8:49 AM
  • bool Is64bit = !string.IsNullOrEmpty( Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"));

    see below link.

    http://dotnet-magic.blogspot.in/2012/11/read-registry-value-from-32-and-64-bit.html

    Thursday, November 08, 2012 9:49 AM
  • does not work for my Win 7 64 bit, 

    your Is64Bit() returns false. compiled for .net 3.5 as "any cpu","x86"...

    the simple (IntPtr.Size == 8) works better...


    amateur .net developer

    Wednesday, November 21, 2012 5:27 PM
  •             bool y = Environment.Is64BitOperatingSystem;


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Wednesday, November 21, 2012 6:29 PM
  • It's pretty amazing how many people get this one wrong. There is a lot of misinformation on the subject. The following code works and will not crash on certain operating systems (because it checks for the existance of IsWow64Process which does not exist on earlier OSes).

    public static class Wow
    {
      public static bool Is64BitProcess
      {
        get { return IntPtr.Size == 8; }
      }
    
      public static bool Is64BitOperatingSystem
      {
        get
        {
          // Clearly if this is a 64-bit process we must be on a 64-bit OS.
          if (Is64BitProcess)
            return true;
          // Ok, so we are a 32-bit process, but is the OS 64-bit?
          // If we are running under Wow64 than the OS is 64-bit.
          bool isWow64;
          return ModuleContainsFunction("kernel32.dll", "IsWow64Process") && IsWow64Process(GetCurrentProcess(), out isWow64) && isWow64;
        }
      }
    
      static bool ModuleContainsFunction(string moduleName, string methodName)
      {
        IntPtr hModule = GetModuleHandle(moduleName);
        if (hModule != IntPtr.Zero)
          return GetProcAddress(hModule, methodName) != IntPtr.Zero;
        return false;
      }
    
      [DllImport("kernel32.dll", SetLastError=true)]
      [return:MarshalAs(UnmanagedType.Bool)]
      extern static bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] out bool isWow64);
      [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
      extern static IntPtr GetCurrentProcess();
      [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
      extern static IntPtr GetModuleHandle(string moduleName);
      [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError=true)]
      extern static IntPtr GetProcAddress(IntPtr hModule, string methodName);
    }
    
    

    Unfortunately, The code does not work properly if you have a 64-bit process and 64-bit application.

    //for 64-bit OS and 64-bit application.
    
    bool moduleContains = ModuleContainsFunction("kernel32.dll", "IsWow64Process");//true
                    
    bool isWow64 = false;
    bool returnValueOfIsWow64 = IsWow64Process(GetCurrentProcess(), out isWow64);//true(non zero)
    
    //currently isWow64 is false because my application and os have 64-bit architecture.
    return moduleContains && returnValueOfIsWow64 && isWow64;//it's wrong and it is a bug in .NET 4

    What is the correct code?

    if (IntPtr.Size == 8)
        return true;//current process is a 64-bit process; so, windows have a 64-bit architecture.
    
    if (!ModuleContainsFunction("kernel32.dll", "IsWow64Process"))
        return false;//current windows does not have 64-bit library files.
    
    bool isWow64 = false;
    IsWow64Process(GetCurrentProcess(), out isWow64);
    
    return isWow64;//if isWow64 is true then the windows have 64-bit architecture libary files.

    Thursday, February 21, 2013 10:05 PM
  • This only works on the .NET Frameworks 4 and 4.5. The property does not exists in earlier versions.
    Thursday, February 28, 2013 4:49 PM