I'm struggling with a problem of how to determine the location of the 64-bit Program Files directory on 64-bit Windows Vista from a 32-bit application.
Calls to SHGetKnownFolderPath(FOLDERID_ProgramFilesX64)
do not return anything. The MSDN article KNOWNFOLDERID also states that this particular call with FOLDERID_ProgramFilesX64
is not supported for a 32-bit application.
I would like to avoid as much as possible hardcoding the path to "C:\Program Files".
Doing something like GetWindowsDirectory()
, extracting the drive from the return value and adding "\Program Files" to it is not appealing either.
How can a 32-bit application properly get the location of the folder from 64-bit Windows Vista?
Our application has a service component which is supposed to launch other processes based on requests from user-session-specific component. The applications launched can be 32-bit or 64-bit. We do this is via CreateProcessAsUser()
by passing in a token from initiating user-session process. For call to CreateProcessAsUser
, we create an environment block via the CreateEnvironmentBlock()
API. The problem is that CreateEnvironmentBlock()
, using the token of the user-session application, creates a block with ProgramW6432="C:\Program Files (x86)", which is a problem for 64-bit applications. We need to override it with the proper value.
As you mentioned, using SHGetKnownFolderPath from a 32-bit application will not work on a 64-bit operating system. This is because Wow64 emulation is in effect.
You can however use RegOpenKeyEx passing in the flag KEY_WOW64_64KEY
and then read the program files directory from registry.
The location in registry:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion
You are interested in the string value:
ProgramFilesDir