How to detect process bitness

Microsoft Scripting Windows

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?

Detection Matrix

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 Bitness32bit Native64bit NativeWOW64
PROCESSOR_ARCHITECTUREx86AMD64x86
PROCESSOR_ARCHITEW6432undefinedundefinedAMD64
  • WOW64 = 32bit Program on 64bit OS
  • Replace AMD64 with IA64 for Itaniums

Detection Logic

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.

In Closing…

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…

Source article: HOWTO: Detect Process Bitness | Microsoft Docs

Leave a Reply

Your email address will not be published. Required fields are marked *