How to detect on C++ is windows 32 or 64 bit?
Solution 1
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
}
Solution 2
Retrieves the path of the system directory used by WOW64. This directory is not present on 32-bit Windows.
Ted
Updated on June 06, 2022Comments
-
Ted almost 2 years
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
-
vz0 about 10 yearsWhy the last "&& f64"? Looks like the compiler can optimize the whole line to "return false" since "A && false" is false.
-
David Heffernan about 10 years@vz0 No, the call to
IsWow64Process()
modifiesf64
-
Remy Lebeau about 10 years@vz0:
IsWow64Process()
returns TRUE/FALSE to indicate whether theBOOL
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));
-
vz0 almost 10 yearsLooks like undefined behaviour to me. Is it? My C++-std-fu is wasted
-
David Heffernan almost 10 years@vz0 No. C++ logical operators evaluate left to right with short circuit
-
david.pfx almost 10 years@DavidHeffernan: And they are properly sequenced (which you knew).
-
phuclv about 4 yearsthat's absolutely not reliable. That folder may have other names in other languages, and the path to that can also be easily changed
-
metamorphosis about 4 years@phuclv Good point about other languages, but the path cannot be changed easily. If you think it can, please provide a reference.
-
Alex Yu almost 3 yearsHm. Interesting answer. Will that work on 32bit Windows or return an exception?
-
ZoomIn over 2 yearsBut 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 - docs.microsoft.com/en-us/windows/win32/api/wow64apiset/…. Why would the API return false for 32 bit application running on 64 bit Windows 10 on ARM?
-
saulius2 about 2 yearsIt should return ERROR_CALL_NOT_IMPLEMENTED on 32-bit Windows (according to MSDN).