Newbie
Newbie

Reputation: 1613

Correct way to check if Windows is 64 bit or not, on runtime? (C++)

bool Win64bit = (sizeof(int*) == 8) ? 1 : 0;

I need this so my app can use Windows registry functions properly (or do i need?).

So am i doing it right ?

Upvotes: 7

Views: 15647

Answers (4)

Igor Korkhov
Igor Korkhov

Reputation: 8558

Here's what Raymond Chen suggests in his blog at https://devblogs.microsoft.com/oldnewthing/20050201-00/?p=36553:

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
}

Upvotes: 16

user3759036
user3759036

Reputation: 11

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." About IsWow64Process I personally am not sure about the usage of 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."

Upvotes: 0

Alex Brown
Alex Brown

Reputation: 42932

No, this cannot work as a run-time check, since sizeof(int*) is fixed at compile time at the point where you choose to compile your program as either 32-bit or 64-bit, and once compiled, the check will have the same fixed result regardless of which platform you are running it on.

However, since a 64-bit program cannot run on a 32-bit platform, you will find that your check works correctly without modification as a compile-time check:

If you compile your program as 64-bit, your program will use the 64-bit API because of your code above, and will work correctly on a 64-bit version of windows. It will fail to run on 32-bit windows at all, so there will be no chance you accidentally use the 64-bit API on a 32-bit version of windows.

v++ platform == 64-bit => sizeof(int*) == 8 => use 64-bit API
AND
( windows platform == 64-bit => 64-bit API works
  OR
  windows platform == 32-bit => program does not run )

If you compile your program in 32-bit mode, your program will correctly use the 32-bit APIs, which will work on a 64-bit windows platform in 32-bit compatibility mode, and will obviously work on a 32-bit platform.

v++ platform == 32-bit => sizeof(int*) == 4 => use 32-bit API
AND
( windows platform == 64-bit => 32-bit API works using compatibility mode
  OR
  windows platform == 32-bit => 32-bit API works )

If you really want to access 64-bit APIs from a 32-bit program I daresay there are APIs to do it, but I'm not sure that you would want to.

Upvotes: 7

Alexander Gessler
Alexander Gessler

Reputation: 46657

In addition, one can use IsWow64Process to check whether you're a 32Bit process (sizeof(void*)==4) running under the WoW64 emulation on a 64bit Windows machine.

Upvotes: 4

Related Questions