Home > Windows Development > How to determine programmatically whether your application and OS are 32 or 64-bit

How to determine programmatically whether your application and OS are 32 or 64-bit

September 22, 2012 Leave a comment Go to comments

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.

Advertisements
Categories: Windows Development Tags: , ,
  1. August 1, 2014 at 11:49

    Excellent post. I was checking continuously this blog and I am impressed!
    Extremely useful info specifically the last part šŸ™‚ I care for such info
    a lot. I was seeking this certain info for a very long time.
    Thank you and best of luck.

  1. August 16, 2013 at 14:02

Share your thoughts! Note: to post source code, enclose it in [code lang=...] [/code] tags. Valid values for 'lang' are cpp, csharp, xml, javascript, php etc. To post compiler errors or other text that is best read monospaced, use 'text' as the value for lang.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: