HOWTO: Detect Process Bitness
In an ideal world, everything runs as native bitness (64bit program on 64bit OS, 32bit program on 32bit OS) and life goes on. However, sometimes you need to run that legacy 32bit program on a 64bit OS and need to configure things a little differently as a result. How can you detect this WOW64 case (32bit program on 64bit OS) and act appropriately?
The general idea is to check the following environment variables:
- PROCESSOR_ARCHITECTURE - reports the native processor architecture EXCEPT for WOW64, where it reports x86.
- PROCESSOR_ARCHITEW6432 - not used EXCEPT for WOW64, where it reports the original native processor architecture.
Visually, it looks like:
|Environment Variable \ Program Bitness||32bit Native||64bit Native||WOW64|
- WOW64 = 32bit Program on 64bit OS
- Replace AMD64 with IA64 for Itaniums
The logic that I use from a program to detect whether the OS is 32bit or 64bit looks like this:
IF PROCESSOR_ARCHITECTURE == amd64 OR PROCESSOR_ARCHITEW6432 == amd64 THEN // OS is 64bit ELSE // OS is 32bit END IF
Another way to test for the same thing is:
IF PROCESSOR_ARCHITECTURE == x86 AND PROCESSOR_ARCHITEW6432 NOT DEFINED THEN // OS is 32bit ELSE // OS is 64bit END IF
While detection for whether a process is in WOW64 mode is to simply check for the presence of the PROCESSOR_ARCHITEW6432 environment variable.
Now, WHEN would one have to do this? Oh... how about when you are a 32bit application and need to:
- Turn off WOW64 FileSystem Redirection to reach into System32 directory to launch a 64bit process
- Turn off WOW64 Registry Redirection to read the real HKLM\Software or HKCU\Software values
Of course, you should realize that WOW64 is just a compatibility shim and NOT an architecture to build/rely upon, so I would keep these sorts of manipulations to a minimum. Sometimes, it is a good idea to not disturb the man behind the curtain...