locked
How to detect 32/64 bit OS?

    Question

  • In my program I need to check whether OS is 64 bit or 32 bit for win xp and vista.

    So basically i need to differentiate between xp 32, xp 64,vista32 and vista 64.

    Based on this i want to install specific system drivers.

    How can I do this ? Is registry entries can be used if yes which one?

    Any help is appreciated.
    Thanks
    Thursday, May 24, 2007 9:44 PM

Answers

  • 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
    }
    http://blogs.msdn.com/oldnewthing/archive/2005/02/01/364563.aspx
     
    Friday, May 25, 2007 1:53 PM

All replies

  • 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
    }
    http://blogs.msdn.com/oldnewthing/archive/2005/02/01/364563.aspx
     
    Friday, May 25, 2007 1:53 PM
  • IntPtr.Size == 4 --> 32bit Platform

    IntPtr.Size == 8 --> 64 Bit Platform


    it's simple no ?
    • Proposed as answer by CodedFreak Friday, April 02, 2010 2:38 PM
    Monday, July 23, 2007 5:50 PM
  • does it give the same when 32-bit code is executed on x64 platform?
    Tuesday, July 24, 2007 10:52 AM
  • I think yes,

    but i didn't verify, because i can't connect on my TSE machine this morning.I will make this test.

     

    http://www.gavarin.net

     

     

     

    Tuesday, July 24, 2007 10:59 AM
  • You may try this,

    Click on Start -> Run, and type dxdiag. If a Windows dialog box is displayed asking if you want to verify your drivers.
    Note the Operating System line, refers to Windows <some version> x64 or 64-bit edition. The last window specifies the processor Itanium, which is a 64-bit processor.

    http://www.stata.com/support/faqs/win/64bit.html
    Tuesday, July 31, 2007 3:45 AM
  • We can use GetSystemWow64Directory  function which retrives the path of the system (64-Bit OS) directory used by WOW64.But in 32 bit windows does not have.So we can easily distingush between 64 bit and 32 bit OS.

     

    Wednesday, August 22, 2007 9:52 AM
  • Hi, I don't think this is simple...... where do I find IntPtr Size??? I need step by step instructions for me to find out what OS bit platform my computer has

     

    Thursday, November 22, 2007 6:35 AM
  • IntPtr is probally defined/typedefed in some standard Microsoft include file, but I would do this using the standard C types instead of a typedef, but note that this defines is you compiled for 32 bit or 64 bit, it does not determine what the machine your are running on (that would be true of checking IntPtr too).

     

    if(sizeof(int *) == 4)

    {

    printf("32 bit compile\n");

    }

    else

    {

    printf("64 bit comple (size of int pointer == 8)\n";

    }

     

    The size of this int pointer is going to be determined at compile time not run time.  If someone wanted to switch between 32/64 programs at run time you would compile both versions write another small 32 program that would check something that would tell you are on a 32 or 64 bit machine and then start the real 32 or 64 bit program depending on this check look for the other suggestions for run time checks, because integer pointer size isn't going to do it.

     

    The suggestion to use GetSystemWow64Directory() looks good, if called on a Windows 32 machine it will return an error and the extended error is ERROR_CALL_NOT_IMPLEMENTED.  Look at the documentation on GetSystemWow64Directory().  But I don't get how you would ever compile a 32 bit program up with GetSystemWow64Directory(), because it will not be defined.  If you compile up a 64 bit program to be a program, well then that program will not run on a 32 bit OS.  You can run a 32 bit program on a 64 bit OS, but not the other way around.

     

    I'm sure there is a better way, but one way would be to start the 32 bit wrapper program, try to start the 64 bit program, and if it fails to start (most likely because you are on a 32 bit OS), then start the 32 bit version.

     

    P.S. If you want a compile time test for certain code use

    _WIN32 and _WIN64 in the if statement not sizeof(int *) since that is what they are for.

    P.S.S. Of course the if(sizeof(int *)) would work on other OSes like Unix and Linux, and the _WIN32 and _WIN64 wouldn't.

    Wednesday, November 28, 2007 4:56 AM
  • You can use LoadLibrary("") and GetProcAddress() to get the function pointer of the method "GetSystemWow64Directory". I have not tested but surely it will be null in 32 bit OS and if it not null then our app is running in 64 bit OS.
    Friday, March 12, 2010 4:19 PM
  • Why not just use System.Environment.Is64BitOperatingSystem

    ???

    Thursday, October 14, 2010 6:30 PM
  • This is available for .net 4 and not in earlier .net framework versions.
    Sunday, July 17, 2011 3:23 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:50 AM
  • There are many different answers, I'll try to summarize:

    • The answer by Pingala is the preferred mechanism specified by MS. Because .NET may run in 32-bit or 64-bit context depending on a number of variables
    • --> Of course, you'll need to ensure that _WIN64 is defined in your build environment. If you're compiling for AnyCPU, then you should investigate GetNativeSystemInfo. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms724340(v=vs.85).aspx. Then you can get the information at run-time.
    • The "IntPtr.Size == 4" or "IntPtr.Size == 8" only tells you if your process is running 32-bit or 64-bit. Thus, if you're process is running 32-bit on a 64-bit OS, you'll get IntPtr.Size == 4, which is not what you want.
    • Env Variables are dangerous, you can't really rely on them. There may be some corner cases where environment variables aren't set properly on a customers machine (making you wonder why the 0.1% of installations don't work).

    Regards,
    Jason.


    • Edited by Jason Curl Saturday, November 10, 2012 8:43 PM
    Saturday, November 10, 2012 8:40 PM