How to determine programmatically whether your application and OS are 32 or 64-bit
First of all, we must clear up a misconception. Some people want to programmatically determine whether the underlying processor in use is 32 or 64 bit. This is a misnomer, as modern Intel and AMD processors can and do run in both 32 and 64 bit modes, in addition there are considerations of cache width, bus width and so on. There is no “true, pure” 64-bit processor, therefore trying to determine this information is non-sensical.
You can, however, determine if you are running a 32 or 64 bit Windows environment, and whether your application is built as a 32-bit or 64-bit executable. Obviously, you should know the latter at compile time, but it can occasionally be able to know at run time too. Here is the code:
#include <Windows.h> ... #if defined(_WIN64) int app64 = true; int os64 = true; #else int app64 = false; BOOL f64 = FALSE; int os64 = IsWow64Process(GetCurrentProcess(), &f64) && f64; #endif std::cout << "Application build: " << (app64? "64" : "32") << "-bit" << std::endl; std::cout << "Underlying OS: " << (os64? "64" : "32") << "-bit" << std::endl;
NOTE: There is a bug in Visual Studio 2010 that causes the text within the _WIN64
test section to be greyed out even if _WIN64
is defined. Ignore this when writing your application.
The logic goes as follows: _WIN64
is only defined if you are building your application as a 64-bit executable. Therefore if it is defined, your build is 64-bit, and by consequence, your Windows OS must be 64-bit as well since 64-bit applications can’t run on 32-bit versions of Windows.
If your build is 32-bit (_WIN64
is not defined), it is still possible that you are running a 64-bit version of Windows. To test this, we check if the process is running in 64-bit emulation mode as all 32-bit processes do in 64-bit versions of Windows. We use the Windows function IsWow64Process
to do this. If set, then the OS must be a 64-bit version.
-
August 16, 2013 at 14:02C++11 / Boost: Multi-threading – The Parallel Aggregation Pattern | Katy's Code