7
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 ?

Newbie
  • 1,593
  • 10
  • 35
  • 50
  • 2
    No real need for the "?1:0" part, if you're sticking it in a bool. (Don't know about the rest.) – Chris Burt-Brown Jan 26 '10 at 16:09
  • 1
    Avoid bypassing registry virtualization, it is rarely ever needed. – Hans Passant Jan 26 '10 at 16:19
  • what do you mean? i want to be sure my app can use the registry in 64 bit windows too, thats why i need to know which version is running my program, so i can choose the correct registry functions (they use different functions for 32 and 64 bits) – Newbie Jan 26 '10 at 16:25
  • Please see my comment regarding registry redirection below – Igor Korkhov Jan 26 '10 at 16:38
  • 2
    "bool Win64bit = (sizeof(int*) == 8) ? 1 : 0;" tells you if the executing program is compiled to 64-bit or not. It doesn't tell you the bitness of the Windows OS it runs on. – ytw Feb 13 '13 at 22:55

4 Answers4

16

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
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
Igor Korkhov
  • 8,283
  • 1
  • 26
  • 31
  • hmm that looks good, didnt understand the logic perfectly, isnt the BOOL f64 = FALSE; useless? shouldnt BOOL f64; be enough? – Newbie Jan 26 '10 at 16:46
  • 4
    That's trouble, IsWow64Process() is not exported by kernel32.dll on older versions of Windows. The program won't run. Use GetProcAddress(). – Hans Passant Jan 26 '10 at 16:46
  • how old versions are you talking about? win95? win98? – Newbie Jan 26 '10 at 17:13
  • @Newbie: nobugz was rigth, to make the code above robust you should use something like: "fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress( GetModuleHandle(TEXT("kernel32")),"IsWow64Process"); if (NULL != fnIsWow64Process) { if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64)) { // handle error } }" – Igor Korkhov Jan 26 '10 at 17:30
  • @Newbie: "how old versions are you talking about?" It is exported since WinXP SP2 – Igor Korkhov Jan 26 '10 at 17:33
7

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.

Alex Brown
  • 41,819
  • 10
  • 94
  • 108
  • i just need to know which one of the functions to use RegDeleteKeyEx() or RegDeleteKey() thats why i need this check. Also even if i just needed one function, the function still needs value depending on which OS it is: KEY_WOW64_32KEY or KEY_WOW64_64KEY ... so im stuck if i dont know how to get the windows version. – Newbie Jan 26 '10 at 16:29
  • 1
    @Newbie: are you sure you need to know that? I mean, do you really need to delete particular WOW64_64_KEY? Because otherwise it is worth mentioning that WoW64 makes use of a registry redirector that "isolates 32-bit and 64-bit applications by providing separate logical views of key portions of the registry on WOW64" – Igor Korkhov Jan 26 '10 at 16:34
  • oh so i could just use the 32 bit registry functions anyways? and 64 bit windows can handle it properly? – Newbie Jan 26 '10 at 16:41
4

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.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • 4
    The compiler does not know. At least not if you want to find out whether you're running on 64Bit Windows. – Alexander Gessler Jan 26 '10 at 16:19
  • I think each answers a different side of the question: +1. – Alex Brown Jan 26 '10 at 16:24
  • 3
    The compiler does NOT know if the app is being run on a 64 bit machine. – John Dibling Jan 26 '10 at 16:30
  • at docs: "If the process is a 64-bit application running under 64-bit Windows, the value is also set to FALSE." doesnt this mean my program will think its 32 bit windows now if i compile it on 64 bit machine and run it on 64 bit machine? i dont get it whats the point returning false in that case... btw i dont want the compiler to know, i need to check on runtime. – Newbie Jan 26 '10 at 16:37
  • If you're compiling for 64Bits, `sizeof(void*)==8` holds. In this case, there is no need to call `IsWow64Process`. – Alexander Gessler Jan 26 '10 at 16:41
  • 1
    @John - yes it knows, it is generating 64-bit code. A program containing 64-bit machine code is guaranteed not to run on a 32-bit operating system. – Hans Passant Jan 26 '10 at 16:43
0

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."

  • 1
    I think you misunderstood. They were referring to the check whether `IsWow64Process` is present or not as a way to detect the bitness. You also have to call it. – Andreas Haferburg Aug 27 '15 at 09:08