Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

registry

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 ?

like image 352
Newbie Avatar asked Jan 26 '10 16:01

Newbie


People also ask

How do I know if my windows is X64?

Click Start, type system in the search box, and then click System Information in the Programs list. When System Summary is selected in the navigation pane, the operating system is displayed as follows: For a 64-bit version operating system: X64-based PC appears for the System Type under Item.

How can I tell if a process is running 64-bit?

Right-click on it or press and hold and then select Properties. Then go to the Compatibility tab. Here, check the "Run this program in compatibility mode for" box, and open the drop-down list. If the list starts with Windows Vista, then the application you selected is a 64-bit application.


2 Answers

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
}
like image 189
Igor Korkhov Avatar answered Sep 21 '22 02:09

Igor Korkhov


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.

like image 22
Alex Brown Avatar answered Sep 20 '22 02:09

Alex Brown