Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve PROGRAMFILES variable from 32bit app in Win64 OS?

As explained in MSDN's WOW64 Implementation Details, the variable %PROGRAMFILES%,

  • in a 32-bit-process on a 64-bit-Windows OS, resolves to C:\Program Files (x86)

  • in a 64-bit-process on a 64-bit-Windows OS, resolves to C:\Program Files

You can verify this e.g. with a Delphi 10.1 program, compiled both with the 32-bit Windows Target Platform and with the 64-bit Windows Target Platform:

MyShellExecute('%PROGRAMFILES%');

So, from a 32-bit Delphi Application executed in a Windows-64bit-OS, how can I get BOTH:

  • the ProgramFiles directory for 32-bit-Programs (C:\Program Files (x86))

  • the ProgramFiles directory for 64-bit-Programs (C:\Program Files)

like image 664
user1580348 Avatar asked Oct 27 '25 18:10

user1580348


1 Answers

Use the following environment variables:

  • ProgramW6432 to obtain the 64 bit program files directory.
  • ProgramFiles(x86) to obtain the 32 bit program files directory.

These return the same values in both 32 and 64 bit processes.

Of course, relying on environment variables is always a little brittle. It's always possible for your parent process to have modified these variables before creating your process.

To make your program more robust you should use known folder IDs instead. Use FOLDERID_ProgramFilesX64 and FOLDERID_ProgramFilesX86.

like image 88
David Heffernan Avatar answered Oct 29 '25 08:10

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!