Jabberwocky
Jabberwocky

Reputation: 50912

How to get the 64 bit "Program Files" folder path from a 32 bit program

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.

Upvotes: 0

Views: 1325

Answers (1)

iBug
iBug

Reputation: 37317

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

Upvotes: 1

Related Questions