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
)
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
.