How to get Program Files folder path (not Program

2019-04-04 01:37发布

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".

6条回答
SAY GOODBYE
2楼-- · 2019-04-04 01:49

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.

查看更多
甜甜的少女心
3楼-- · 2019-04-04 01:49

The best and universal way to get path to "Program Files", is to query it from the registry:

64-Bit-Process can query: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir to get "C:\Program Files" HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\ProgramFilesDir to get "C:\Program Files (x86)"

32-Bit-Process (Wow64) can query: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir to get "C:\Program Files (x86)" HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir with KEY_WOW64_64KEY option! to get "C:\Program Files"

Pseudo-code:

OpenKey(hKey, HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion", KEY_READ | KEY_WOW64_64KEY);
QueryStringValue(hKey, L"ProgramFilesDir", sValue);
查看更多
时光不老,我们不散
4楼-- · 2019-04-04 01:50

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.

查看更多
叛逆
5楼-- · 2019-04-04 01:59

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);
查看更多
孤傲高冷的网名
6楼-- · 2019-04-04 02:00

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.

查看更多
Viruses.
7楼-- · 2019-04-04 02:03

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
查看更多
登录 后发表回答