delphidelphi-10.1-berlinsystem-variable

Resolve PROGRAMFILES variable from 32bit app in Win64 OS?


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

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:


Solution

  • Use the following environment variables:

    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.