4

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)

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user1580348
  • 5,721
  • 4
  • 43
  • 105

1 Answers1

8

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • The `FOLDERID_ProgramFilesX64` is not supported for 32-bit applications running on 64-bit operating systems. Attempting to use FOLDERID_ProgramFilesX64 in this situation results in an error. – user1580348 Oct 11 '17 at 15:38
  • Yes. That's documented in the topics I linked to. From a 64 bit process you can find the 32 bit folder. – David Heffernan Oct 11 '17 at 15:41
  • Typically only installers care about the location of these folders so that's not much of a limitation. – David Heffernan Oct 11 '17 at 15:43
  • I have a situation here where a ShellLink pointing to a program file in the folder for 64-bit programs resolves to the wrong path in the folder for 32-bit programs when executed in a 32-bit program on 64-bit Windows. – user1580348 Oct 11 '17 at 15:50
  • That sounds odd. I wonder how the link is being resolved. Anyway, that's a different topic. I think I answered the specific question here. – David Heffernan Oct 11 '17 at 15:51
  • @user1580348: "*Attempting to use `FOLDERID_ProgramFilesX64` in [a 32 bit app] results in an error*" - your app should detect when it is a 32bit process running on a 32bit or 64bit OS, or running as a 64bit process, and then use the appropriate `KNOWNFOLDERID` for its needs. When compiling for 32bit, use `IsWow64Process()` to know if you are running inside of Wow64 or not. – Remy Lebeau Oct 11 '17 at 17:00
  • For such cases, I use $IFDEFs. The compilr knows best what it is generating. – Rudy Velthuis Oct 12 '17 at 06:57
  • @rudy Compiler can't tell whether you are running on 32 or 64 bit system – David Heffernan Oct 12 '17 at 06:59
  • @David: that's true. But accessing FOLDERID_ProgramFilesX64 from a 32 bit app is not allowed. The compiler can help me with that. And one can also check the result of GetSpeicalFolderID (or whatever similar function). – Rudy Velthuis Oct 12 '17 at 07:15