The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,402 other followers

Windows batch file to set ProgramFilesX86 directory for 32-bit program files on x86 and x64 systems (via: Stack Overflow)

Posted by jpluimers on 2014/05/27

Every once in a while you need to execute a binaryfrom a batch file that will always be available 32-bits.
Examples include development tools (that usually are x86 hosts talking or encapsulating managed environments, and x64 debuggers) or office versions for which certain plugins are not available in x64 versions.

That’s why I wrote the below batch file to fill the ProgramFilesX86 environment variable to point to the 32-bit Program Files directory on any system (x86, x64, or one that does not make a distinction).

Determining processor architecture

The batch file makes use of the flags mentioned in HOWTO: Detect Process Bitness – David Wang – Site Home – MSDN Blogs:

Notes:

  • There are 3 distinct cases and two outcomes
  • We default to x86 just in case we run on a system that has none of the flags defined (for instance on legacy systems).
Environment Variable \ Program Bitness 32bit Native 64bit Native WOW64
PROCESSOR_ARCHITECTURE x86 AMD64 x86
PROCESSOR_ARCHITEW6432 undefined undefined AMD64

Source of 32-bit program files

The place of the 32-bit program files directory is determined by the environment variables mentioned in WOW64 Implementation Details (Windows):

Process Environment variables
64-bit process PROCESSOR_ARCHITECTURE=AMD64 or PROCESSOR_ARCHITECTURE=IA64
ProgramFiles=%ProgramFiles%
ProgramW6432=%ProgramFiles%
CommonProgramFiles=%CommonProgramFiles%
CommonProgramW6432=%CommonProgramFiles%
32-bit process PROCESSOR_ARCHITECTURE=x86
PROCESSOR_ARCHITEW6432=%PROCESSOR_ARCHITECTURE%
ProgramFiles=%ProgramFiles(x86)%
ProgramW6432=%ProgramFiles%
CommonProgramFiles=%CommonProgramFiles(x86)%
CommonProgramW6432=%CommonProgramFiles%
Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP: The ProgramW6432 and CommonProgramW6432 environment variables were added starting with Windows 7 and Windows Server 2008 R2.

Boolean logic and case insensitive compare in batch files

Many things in batch files can be tricky including these two:

The batch file

Finally: the batch file:

:: Gets the right link for x86 (32-bit) program files
IF /I %PROCESSOR_ARCHITECTURE% == amd64 goto :x64
IF /I %PROCESSOR_ARCHITEW6432% == amd64 goto :x64
goto :x86
:x64
   :: OS is 64bit
   set ProgramFilesX86=%ProgramFiles(x86)%
  goto :eof
:x86
   :: OS is 32bit
  set ProgramFilesX86=%ProgramFiles%
  goto :eof

IF PROCESSOR_ARCHITECTURE == amd64 OR
   PROCESSOR_ARCHITEW6432 == amd64 THEN
   // OS is 64bit
ELSE
   // OS is 32bit
END IF

–jeroen

via:

4 Responses to “Windows batch file to set ProgramFilesX86 directory for 32-bit program files on x86 and x64 systems (via: Stack Overflow)”

  1. […] The base registry path depends if you are running on an x64 or x86 system I used the PROCESSOR_ARCHITECTURE  and PROCESSOR_ARCHITEW6432 environment variables to detect which bitness was running as described in HOWTO: Detect Process Bitness – David Wang – MSDN Blogs (I blogged about that before: Windows batch file to set ProgramFilesX86 directory for 32-bit program files on x86 and x64 systems …). […]

  2. […] jpluimers on Windows batch file to set Prog… […]

  3. Is there supposed to be an “:eof” label in the batch file?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: