51

I want to detect whether the current Windows OS is 32-bit or 64-bit. How to achieve it using C++? I don't want processor type I want OS's bit type. This is because you can install 32-bit OS on 64-bit processor.

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
Rahul
  • 1,401
  • 4
  • 17
  • 33
  • probably duplicate of : http://stackoverflow.com/questions/1505582/determining-32-vs-64-bit-in-c –  Aug 10 '11 at 12:59
  • he is not asking whether code is compiled in 32 or 64 bit but trying to find installed os version – Hayri Uğur Koltuk Aug 10 '11 at 13:02
  • Duplicate of http://stackoverflow.com/questions/2140619/correct-way-to-check-if-windows-is-64-bit-or-not-on-runtime-c (which has a more complete answer). – Fabio Zadrozny Jan 10 '12 at 17:04

14 Answers14

48

The function to call is IsWow64Process or IsWow64Process2. It tells your 32-bit application if it is running on a 64 bit Windows.

If the program is compiled for 64 bits, it will already know.

oleksii
  • 35,458
  • 16
  • 93
  • 163
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • If the function exists, that's enough indication of the fact that it's a 64-bit Windows, right? You don't even need to call it. – GetFree Apr 19 '17 at 14:35
  • 4
    msdn says that the function exists do not mean it is 64-bit windows. "For compatibility with operating systems that do not support this function, call GetProcAddress to detect whether IsWow64Process is implemented in Kernel32.dll. If GetProcAddress succeeds, it is safe to call this function. Otherwise, WOW64 is not present. Note that this technique is not a reliable way to detect whether the operating system is a 64-bit version of Windows because the Kernel32.dll in current versions of 32-bit Windows also contains this function." – bronze man Oct 17 '17 at 08:00
  • 3
    @GetFree: "*If the function exists, that's enough indication of the fact that it's a 64-bit Windows, right?*" - No. `IsWow64Process()` exists in 32-bit versions of XP SP2 and higher. – Remy Lebeau Mar 15 '18 at 02:08
17

If your code is 64-bit and running, then Windows is 64-bit - nothing to check. If your process is 32-bit call IsWow64Process() - 32-bit processes run in WOW64 on 64-bit Windows and without WOW64 otherwise.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Does calling this function affect virtualization of my 32-bit process? – Wolf Jan 15 '15 at 16:07
  • @Wolf: Could you please provide more details? – sharptooth Jan 16 '15 at 08:35
  • Specifically, I was afraid that an application may lose the automatic translation for HKLM registry keys (or things like this) after calling `IsWow64Process()`. – Wolf Jan 19 '15 at 10:02
  • 2
    @Wolf: AFAIK this function doesn't change the program state - it just returns a boolean and that's all. MSDN never mentions that calling this function causes any program state changes. – sharptooth Jan 19 '15 at 10:05
11
bool getWindowsBit(bool & isWindows64bit)
{
#if _WIN64

    isWindows64bit =  true;
    return true;

#elif _WIN32

    BOOL isWow64 = FALSE;

    //IsWow64Process is not available on all supported versions of Windows.
    //Use GetModuleHandle to get a handle to the DLL that contains the function
    //and GetProcAddress to get a pointer to the function if available.

    LPFN_ISWOW64PROCESS fnIsWow64Process  = (LPFN_ISWOW64PROCESS) 
GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");

    if(fnIsWow64Process)
    {
        if (!fnIsWow64Process(GetCurrentProcess(), &isWow64))
            return false;

        if(isWow64)
            isWindows64bit =  true;
        else
            isWindows64bit =  false;

        return true;
    }
    else
        return false;

#else

    assert(0);
    return false;

#endif
}
Ian Fellows
  • 17,228
  • 10
  • 49
  • 63
Survarium
  • 190
  • 2
  • 6
8

you can use IsWow64Process if your app is a 32 bit app, if it's true you are running on an x64 OS, else it's 32bit

Wolf
  • 9,679
  • 7
  • 62
  • 108
Necrolis
  • 25,836
  • 3
  • 63
  • 101
6

Use GetNativeSystemInfo function. It gets a LPSYSTEM_INFO parameter to get what you want.

SYSTEM_INFO structure:

wProcessorArchitecture
The processor architecture of the installed operating system.

Alexey Ivanov
  • 11,541
  • 4
  • 39
  • 68
Hayri Uğur Koltuk
  • 2,970
  • 4
  • 31
  • 60
  • 3
    Microsoft's documentation is wrong, and in fact wProcessorArchitecture doesn't return the installed OS processor architecture, but rather returns the architecture the app was built for. – ThreeBit Jul 20 '12 at 01:54
  • 4
    @ThreeBit: This answer is fine. What you said is true of `GetSystemInfo`, this answer calls for `GetNativeSystemInfo` – Ben Voigt Aug 20 '14 at 20:45
  • Honestly I'm not sure of that fully. Running a Windows 11 ARM build on parallels seems to return AMD data. – DreTaX Jul 24 '23 at 11:03
6

You need to use GetNativeSystemInfo. Given that you expect this to work on a 32-bit operating system, you need to use LoadLibrary + GetProcAddress so that you can deal with this function not being available. So if that fails, you know it is a 32-bit operating system. If not, SYSTEM_INFO.wProcessorArchitecture gives you the real processor type instead of the emulated one.

Alexey Ivanov
  • 11,541
  • 4
  • 39
  • 68
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

Here is another way: GetSystemWow64Directory - "Retrieves the path of the system directory used by WOW64. This directory is not present on 32-bit Windows." and "On 32-bit Windows, the function always fails, and the extended error is set to ERROR_CALL_NOT_IMPLEMENTED."

I personally am not sure about the usage of IsWow64Process since in MSDN in the description of the IsWow64Process there is the text "Note that this technique is not a reliable way to detect whether the operating system is a 64-bit version of Windows because the Kernel32.dll in current versions of 32-bit Windows also contains this function."

ReggieB
  • 8,100
  • 3
  • 38
  • 46
1
bool IsX64win()
{
    UINT x64test = GetSystemWow64DirectoryA(NULL, 0);
    if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)  return FALSE;
    else return TRUE;
}
0

Answer for Newer Versions of Windows

While several people, and the Microsoft Docs, have suggested IsWow64Process2 for this, I have not seen any code examples in my research. That is why I wanted to contribute this answer to the community.

According to the running 32 bit applications documentation page, Microsoft recommends using the IsWow64Process2 for Windows 10 instead of IsWow64Process:

A 32-bit application can detect whether it is running under WOW64 by calling the IsWow64Process function (use IsWow64Process2 if targeting Windows 10).

This function works on Windows 10 version 1511 (client) and Windows Server 2016 and above.

This has two parameters via which information is returned: pProcessMachine and pNativeMachine. Both return Image File Machine Constants

pProcessMachine returns information about whether the target process is running under the WOW64 emulator or not, and if it is, what kind of process it is.

pNativeMachine returns information about the architecture of the Windows host.

Using both of these return values, one can determine if Windows is 32 bit or 64 bit (which is what the OP asked), and whether the process is running under WOW64 as well as whether the process is 32 or 64 bit.

Here is a function I wrote for these purposes:

BOOL getBits(BOOL& windowsIs32Bit, BOOL& isWOW64, BOOL& processIs32Bit)
{
  USHORT ProcessMachine;
  USHORT NativeMachine;

  if (!IsWow64Process2(GetCurrentProcess(), &ProcessMachine, &NativeMachine)) {
    std::cerr << "IsWOW64Process2 returned FALSE (failed). GetLastError returned: " << GetLastError() << std::endl;
    return FALSE;
  }

  if (ProcessMachine == IMAGE_FILE_MACHINE_UNKNOWN) {
    isWOW64 = FALSE;

    if (NativeMachine == IMAGE_FILE_MACHINE_IA64 || NativeMachine == IMAGE_FILE_MACHINE_AMD64 || NativeMachine == IMAGE_FILE_MACHINE_ARM64) {
      windowsIs32Bit = FALSE;
      processIs32Bit = FALSE;

      return TRUE;
    }

    if (NativeMachine == IMAGE_FILE_MACHINE_I386 || NativeMachine == IMAGE_FILE_MACHINE_ARM) {
      windowsIs32Bit = TRUE;
      processIs32Bit = TRUE;

      return TRUE;
    }

    std::cerr << "Unknown Windows Architecture." << std::endl;
    return FALSE;
  }

  windowsIs32Bit = FALSE;
  isWOW64 = TRUE;
  processIs32Bit = TRUE;

  return TRUE;
}

Here is an example usage of the above function:

int main() {

  BOOL windowsIs32Bit;
  BOOL isWOW64;
  BOOL processIs32Bit;

  if (!getBits(windowsIs32Bit, isWOW64, processIs32Bit)) {
    return -1;
  }

  std::cout << (windowsIs32Bit ? "Windows is 32 bit" : "Windows is 64 bit") << std::endl;
  std::cout << (isWOW64 ? "This process *is* running under WOW64" : "This process is *not* running under WOW64") << std::endl;
  std::cout << (processIs32Bit ? "This process is 32 bit" : "This process is 64 bit") << std::endl;

  return 0;
}

I could only test two scenarios of the above code because I only have 64 bit Windows on a 64 bit machine. I do not have a 32 bit machine nor 32 bit Windows, nor do I have any ARM machines. If someone can test other scenarios, I would appreciate some feedback about whether the design I have done works for them.

I wrote an article that goes into greater depth explaining how the above code works.

Community
  • 1
  • 1
Xitalogy
  • 1,592
  • 1
  • 14
  • 17
0

Alternatively the easiest way is to check the size of integer pointer with sizeof(int *).

If 4 then its 32 bit
If 8 then its 64 bit

Frightera
  • 4,773
  • 2
  • 13
  • 28
Manoj
  • 11
  • 2
0

You can run the windows command systeminfo as a process in your program.

#include <stdlib.h>

system("systeminfo");

One of the returning categories is System Type.

Its output reads: System Type: x86-based PC, or System Type: x64-based PC

This may be a more complicated solution than the one provided by others but I thought I would add it as a possibility. (Maybe you are after additional info as well.)

Alexey Ivanov
  • 11,541
  • 4
  • 39
  • 68
sealz
  • 5,348
  • 5
  • 40
  • 70
  • I'd appreciate down votes that occur 2.5 years later come with an explanation. If this is no longer a valid option don't be afraid to let me know :) – sealz Mar 28 '14 at 12:52
  • Or the one that is 3 years later O-o – sealz Jan 05 '15 at 13:41
  • 2
    Well, it's a valid answer. Yet parsing the output of another process is not as simple as calling `IsWow64Process()` function specifically designed for this purpose. – Alexey Ivanov Nov 16 '15 at 15:43
  • 1
    it prints whole bunch of system information that is irrelevant to what OP asked for, use **IsWow64Process()** method. – Haseeb Mir Apr 24 '18 at 19:46
0

Here is an undocumented method ...

bool _Is64BitOS(void);


bool   _Is64BitOS(void) {
    unsigned int version = *((unsigned int)*)0x7FFE026C;
    unsigned int address = version == 10 ? 0x7FFE0308 : 0x7FFE0300;
//printf("Running %u-bit system",*((void*)*)address ? 32 : 64);

    return (*((void*)*)address ? false,true);
};
Lewis Miller
  • 105
  • 6
-1
 static bool is64bitOS()
   {
      SYSTEM_INFO si;
      GetSystemInfo(&si);

      if((si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_IA64)||(si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_AMD64)==64)
      {
         return true;
      }
      else
      {
         return false;
      }

   }
foobar
  • 2,887
  • 2
  • 30
  • 55
  • 1
    Besides the error '==64', it also doesn't work. For 32 bit application it doesn't detect a 64 bit operating system. You should use 'GetNativeSystemInfo' as described below. – gast128 Jul 30 '15 at 12:12
-2

A simple check is if the EXE does not run, then it is a 64-bit executable running on a 32-bit machine. A 64-bit machine will always run a 32-bit executable.

From Microsoft,

Most programs designed for the 32-bit version of Windows will work on the 64-bit version of Windows. Notable exceptions are many antivirus programs.

Device drivers designed for the 32-bit version of Windows don't work on computers running a 64-bit version of Windows. If you're trying to install a printer or other device that only has 32-bit drivers available, it won't work correctly on a 64-bit version of Windows.

In Windows, however, you can also check for the existence of the Program Files (x86) folder as another simple check. No need to get fancy.

  • 4
    Err, how does a program conclude anything when it cannot run? – Hans Passant Aug 10 '11 at 13:02
  • make a bunch of hello-world programs compiled for various architectures and bitness - then try to execute them from your application - what ever of the sub-programs will run indicates what "personalities" the host in question is able to execute. (you might be able to detect even more runability features like .NET framework support, e.g. .NET 3.0/4.0; having the right QT library or other common libs in reach might unveil as well when doing so.) – Alexander Stohr Dec 14 '18 at 15:30