How to get Program Files folder path (not Program Files (x86)) from 32bit WOW process?

26,373

Solution 1

I appreciate all the help and, especially, the warnings in this thread. However, I really do need this path and this is how I got it in the end:

(error checking removed for clarity, use at your own risk, etc)

WCHAR szNativeProgramFilesFolder[MAX_PATH];
ExpandEnvironmentStrings(L"%ProgramW6432%", 
                       szNativeProgramFilesFolder, 
                       ARRAYSIZE(szNativeProgramFilesFolder);

Solution 2

Let me quote Raymond Chen's excellent blogpost on the issue:

On 64-bit Windows, 32-bit programs run in an emulation layer. This emulation layer simulates the x86 architecture, virtualizing the CPU, the file system, the registry, the environment variables, the system information functions, all that stuff. If a 32-bit program tries to look at the system, it will see a 32-bit system. For example, if the program calls the GetSystemInfo function to see what processor is running, it will be told that it's running on a 32-bit processor, with a 32-bit address space, in a world with a 32-bit sky and 32-bit birds in the 32-bit trees.

And that's the point of the emulation: To keep the 32-bit program happy by simulating a 32-bit execution environment.

...

The question is "What is the way of finding the x64 Program Files directory from a 32-bit application?"

The answer is "It is better to work with the system than against it." If you're a 32-bit program, then you're going to be fighting against the emulator each time you try to interact with the outside world. Instead, just recompile your installer as a 64-bit program. Have the 32-bit installer detect that it's running on a 64-bit system and launch the 64-bit installer instead. The 64-bit installer will not run in the 32-bit emulation layer, so when it tries to copy a file or update a registry key, it will see the real 64-bit file system and the real 64-bit registry.

If you still want to do this, I recommend reading the comments on this blogpost as they contain some good hints.

Solution 3

You're on the right path - Use the KNOWNFOLDERID of FOLDERID_ProgramFilesX64

The SHGetKnownFolderPath function can be used to retrieve the full path of a given KnownFolder.

Solution 4

This is almost certainly a bad idea, according to a recent-ish post by the infamous Raymond Chen. See here for details. Bottom line, I think it can be done, but it's a lot of hard work and there's almost certainly an easier way.

Microsoft built the WOW emulation layer to make your life easier. Don't waste all their time and effort by fighting it :-).

Perhaps if you told us why you need the non-WOW Program Files directory, we could assist further.

Solution 5

I needed it to get the x64 Program Folder from a Logonscript and used:

Dim oWshShell : Set oWshShell = CreateObject("WScript.Shell")
Dim sProgramDirPath : sProgramDirPath = 
    oWshShell.ExpandEnvironmentStrings("%ProgramW6432%")

WScript.Echo sProgramDirPath
Share:
26,373
Shanu Azeez
Author by

Shanu Azeez

I write code for a medium sized corporation.

Updated on July 25, 2022

Comments

  • Shanu Azeez
    Shanu Azeez over 1 year

    I need to get the path to the native (rather than the WOW) program files directory from a 32bit WOW process.

    When I pass CSIDL_PROGRAM_FILES (or CSIDL_PROGRAM_FILESX86) into SHGetSpecialFolderPath it returns the WOW (Program Files (x86)) folder path.

    I'd prefer to avoid using an environment variable if possible.

    I want to compare some values I read from the registry, if the values point to the path of either the WOW or native version of my app then my code does something, if not it does something else. To figure out where the native and WOW versions of my app are expected to be I need to get the paths to "Program Files (x86)" and "Program Files".