10

How to detect on C++ is windows 32 or 64 bit? I see a lot of examples in .Net but I need C++. Also IsWow64Process() dosen't works for me, becouse "If the process is running under 32-bit Windows, the value is set to FALSE. If the process is a 64-bit application running under 64-bit Windows, the value is also set to FALSE"

if I have 32 bit proc under 32 bit OS I have FALSE if I have 64 bit proc under 64 bit OS I have FALSE

BUT I dont care about process bit I need OS bit

Ted
  • 1,682
  • 3
  • 25
  • 52

3 Answers3

31

The Win32 API function to detect information about the underlying system is GetNativeSystemInfo. Call the function and read the wProcessorArchitecture member of the SYSTEM_INFO struct that the function populates.

Although it is actually possible to use IsWow64Process to detect this. If you call IsWow64Process and TRUE is returned, then you know that you are running on a 64 bit system. Otherwise, FALSE is returned. And then you just need to test the size of a pointer, for instance. A 32 bit pointer indicates a 32 bit system, and a 64 bit pointer indicates a 64 bit system. In fact, you can probably get the information from a conditional supplied by the compiler, depending on which compiler you use, since the size of the pointer is known at compile time.

Raymond Chen described this approach in a blog article. He helpfully included code which I reproduce here:

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
}
Paul
  • 6,061
  • 6
  • 39
  • 70
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Why the last "&& f64"? Looks like the compiler can optimize the whole line to "return false" since "A && false" is false. – vz0 May 16 '14 at 13:56
  • 1
    @vz0 No, the call to `IsWow64Process()` modifies `f64` – David Heffernan May 16 '14 at 13:58
  • 4
    @vz0: `IsWow64Process()` returns TRUE/FALSE to indicate whether the `BOOL` variable was updated or not. If TRUE, the variable then indicates whether the requested process is a WOW64 process (32bit running on 64bit) or not. That is why the extra check is needed: `return ((query was successful) && (query result is true));` – Remy Lebeau May 16 '14 at 14:58
  • Looks like undefined behaviour to me. Is it? My C++-std-fu is wasted – vz0 May 17 '14 at 16:00
  • 1
    @vz0 No. C++ logical operators evaluate left to right with short circuit – David Heffernan May 17 '14 at 16:09
  • @DavidHeffernan: And they are properly sequenced (which you knew). – david.pfx May 18 '14 at 10:28
  • But the following is line is interesting - "If the process is a 32-bit application running under 64-bit Windows 10 on ARM, the value is set to FALSE. " in MSDN page - https://learn.microsoft.com/en-us/windows/win32/api/wow64apiset/nf-wow64apiset-iswow64process. Why would the API return false for 32 bit application running on 64 bit Windows 10 on ARM? – ZoomIn Sep 15 '21 at 13:34
3

GetSystemWow64DirectoryW

Retrieves the path of the system directory used by WOW64. This directory is not present on 32-bit Windows.

Jaber
  • 311
  • 2
  • 5
-1

An alternative method is to check for the existence of the "Program Files (x86)" root folder on the system partition. It will be present on x64 windows installations, and not on x86 installations.

metamorphosis
  • 1,972
  • 16
  • 25
  • 2
    that's absolutely not reliable. That folder may have other names in other languages, and the path to that can also be easily changed – phuclv May 11 '20 at 01:33
  • @phuclv Good point about other languages, but the path cannot be changed easily. If you think it can, please provide a reference. – metamorphosis May 13 '20 at 04:35