0

I need to get the 64 bit "Program Files" folder path (the one that defaults to C:\Program Files, not the one that default to C:\Program Files (x86)) from a 32 bit program and for this I planned to use SHGetKnownFolderPath.

Unfortunately it seems impossible to get that path from a 32 bit program running on a 64 bit Windows, at least this is what I conclude from the remarks section of the documentation.

Is there another method to do this?

I'm seriously thinking of stripping the " (x86)" from the path I'm getting with SHGetKnownFolderPath, or use some other ugly hack.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • mybe this will help http://www0.cs.ucl.ac.uk/staff/W.Langdon/getenv/ search for "ProgramFiles" – phoenixstudio Jul 16 '20 at 13:01
  • 3
    *GetEnvironmentVariableW(L"ProgramW6432", ...)* – RbMm Jul 16 '20 at 13:03
  • And why a 32-bit program will need path to 64-bit software? What it will do there? – i486 Jul 16 '20 at 13:35
  • Because it's an installation program that should run on both 32 bit and 64 bit Windows. – Jabberwocky Jul 16 '20 at 13:36
  • 2
    Did you try using `FOLDERID_ProgramFilesX64`? In the remarks section you linked, one mention of it says "not supported under 32-bit **operating systems**" and the other says "not supported for 32-bit **applications**". It wouldn't surprise me if the second comment was a mistake. Otherwise there really wouldn't be much point in having that folder ID in the first place. – Jonathan Potter Jul 16 '20 at 21:35

1 Answers1

1

Reading the registry is an option.

Provided that you've properly determined that the host OS is 64-bit, you can read ProgramFilesDir from HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion.

There should be no problem reading that key, but one nitpick is that the key is accurate only if it hasn't been changed since the last system startup. I doubt it's frequently changed on any reasonable system, though.


As suggested by @RbMM in comments, reading ProgramW6432 from the environment is another reliable option.

According to this answer, this environment variable is:

Host OS  Program  %ProgramW6432%
-------  -------  --------------
32-bit   32-bit   (not present)
64-bit   32-bit   C:\Program Files
64-bit   64-bit   C:\Program Files
iBug
  • 35,554
  • 7
  • 89
  • 134
  • Note that by default, a 32-bit application will transparentely read `HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion`. There the program path is again `C:\Program Files (x86)` (Note the `WOW6432Node`!). AFAIK it depends how you open the registry key – king_nak Jul 16 '20 at 13:07
  • Well, that's what I wanted to avoid, but apparently there are not many other options. – Jabberwocky Jul 16 '20 at 13:08
  • @Jabberwocky I got an update with the answer. Check it out. – iBug Jul 16 '20 at 13:13
  • @Jabberwocky see [32-bit and 64-bit Application Data in the Registry](https://learn.microsoft.com/en-us/windows/win32/sysinfo/32-bit-and-64-bit-application-data-in-the-registry) and [Accessing an Alternate Registry View](https://learn.microsoft.com/en-us/windows/win32/winprog64/accessing-an-alternate-registry-view). To access the 64-bit version of `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion` from a 32-bit app, open the key using the `KEY_WOW64_64KEY` flag. – Remy Lebeau Jul 17 '20 at 00:33