views:

474

answers:

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

+4  A: 

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
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
@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
oh so i could just use the 32 bit registry functions anyways? and 64 bit windows can handle it properly?
Newbie
yes, it will work fine.
Alex Brown
+4  A: 

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
This is the correct solution IMO
BlueRaja - Danny Pflughoeft
Nah, don't use a API call when the compiler already knows.
Hans Passant
The compiler does not know. At least not if you want to find out whether you're running on 64Bit Windows.
Alexander Gessler
I think each answers a different side of the question: +1.
Alex Brown
The compiler does NOT know if the app is being run on a 64 bit machine.
John Dibling
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
If you're compiling for 64Bits, `sizeof(void*)==8` holds. In this case, there is no need to call `IsWow64Process`.
Alexander Gessler
@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
+3  A: 

Here's what Raymond Chen suggests in his blog at http://blogs.msdn.com/oldnewthing/archive/2005/02/01/364563.aspx:

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
}
Igor Korkhov
hmm that looks good, didnt understand the logic perfectly, isnt the BOOL f64 = FALSE; useless? shouldnt BOOL f64; be enough?
Newbie
That's trouble, IsWow64Process() is not exported by kernel32.dll on older versions of Windows. The program won't run. Use GetProcAddress().
Hans Passant
how old versions are you talking about? win95? win98?
Newbie
Igor Korkhov
@Newbie: "how old versions are you talking about?" It is exported since WinXP SP2
Igor Korkhov