Saturday, November 19, 2016

Windows Bitness Helper

Although it was just six years ago I wrote about the 32-bit and 64-bit issues on Windows, the issues still exist today. Most Windows system these days are 64-bit, but a good portion of programs are still just 32-bit. To get an idea, just open C:\Program Files (x86)\ on your PC – that’s all 32-bit programs you see.

Normally, you don’t need to take care what is 64- or 32-bit, except you need to write a batch file and know exactly where which files are. To give you an example, the environment variable %ProgramFiles% points to different folders, depending on the system and how it’s executed:

  • On a 32-bit Windows it points to: C:\Program Files\
  • On a 64-bit Windows, from a 64-bit Process, it points to: C:\Program Files\
  • On a 64-bit Windows, from a 32-bit Process, it points to: C:\Program Files (x86)\

This is somewhat logical, but where things go totally haywire is when you need to know the location of the Windows EXE files:

  • On a 32-bit Windows, the 32-bit files are in C:\windows\system32\
  • On a 64-bit Windows, the 32-bit files are in C:\windows\SysWoW64\
  • On a 64-bit Windows, from a 32-bit Process, the 32-bit files are in C:\windows\system32\
    * Because this will be redirected to \SysWoW64
  • On a 64-bit Windows, from a 32-bit Process, the 64-bit files are in C:\windows\sysnative
    * This is a special virtual redirection folder
  • On a 64-bit Windows, from a 64-bit Process, the 64-bit files are in C:\windows\system32
    For real this time, no redirection will occur.

The special case is here “32-bit process on 64-bit Windows” (aka Windows on Windows = WoW) where Windows will redirect any access to the \system32 folder to \SysWow64. This also means, you can’t access the “real” \system32 folder where the 64-bit files are located. Only in this case the virtual redirection folder \sysnative exists.

Because I was tired to write this detection logic repeatedly, I create a batch file that adds some _BIT_xxx environment variables that are always set to the correct values. For example, if I want to start PowerShell, I use the following lines:

REM This call will provide the _BIT_xxx variables
call "%~dp0BitnessHelper.cmd"

REM Use one of those variables – will work in 32-bit, 64-bit or when running as WOW
"%_BIT_SYSTEM_NATIVE%\WindowsPowerShell\v1.0\powershell.exe"

Here is a screen shot of the variables it creates. The left shows a WoW process, the right a 64-bit process:

BitnessHelperCapture

This batch should work Windows XP up to Windows 10 and always report correct values. You can get it from GitHub: https://github.com/texhex/BitnessHelper

No comments:

Post a Comment