Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can 32-bit application find the location of 64-bit Program Files directory on Windows Vista 64-bit?

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?

Background

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.

like image 931
Karen Gabrielyan Avatar asked Dec 24 '09 16:12

Karen Gabrielyan


2 Answers

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

like image 109
Brian R. Bondy Avatar answered Nov 02 '22 07:11

Brian R. Bondy


If you read that page carefully you will see that FOLDERID_ProgramFilesX64 is supported for 32 bits applications on a 64-bit OS. It's NOT supported on a 32-bit OS, which makes complete sense.

like image 30
Jeff Paquette Avatar answered Nov 02 '22 07:11

Jeff Paquette