Detect 32-bit or 64-bit of Windows

C++Windows32bit 64bit

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.

Solution 3 - C++

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
}

Solution 4 - C++

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

Solution 5 - C++

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.

Solution 6 - C++

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.

Solution 7 - C++

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."

Solution 8 - C++

bool IsX64win()
{
	UINT x64test = GetSystemWow64DirectoryA(NULL, 0);
	if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)  return FALSE;
	else return TRUE;
}

Solution 9 - C++

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.)

Solution 10 - C++

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.

Solution 11 - C++

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

Solution 12 - C++

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);
};

Solution 13 - C++

 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;
      }

   }

Solution 14 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahulView Question on Stackoverflow
Solution 1 - C++Bo PerssonView Answer on Stackoverflow
Solution 2 - C++sharptoothView Answer on Stackoverflow
Solution 3 - C++SurvariumView Answer on Stackoverflow
Solution 4 - C++NecrolisView Answer on Stackoverflow
Solution 5 - C++Hans PassantView Answer on Stackoverflow
Solution 6 - C++Hayri Uğur KoltukView Answer on Stackoverflow
Solution 7 - C++user3759036View Answer on Stackoverflow
Solution 8 - C++TheMaster MohView Answer on Stackoverflow
Solution 9 - C++sealzView Answer on Stackoverflow
Solution 10 - C++XitalogyView Answer on Stackoverflow
Solution 11 - C++ManojView Answer on Stackoverflow
Solution 12 - C++Lewis MillerView Answer on Stackoverflow
Solution 13 - C++foobarView Answer on Stackoverflow
Solution 14 - C++user195488View Answer on Stackoverflow