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".
Let me quote Raymond Chen's excellent blogpost on the issue:
If you still want to do this, I recommend reading the comments on this blogpost as they contain some good hints.
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:
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.
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)
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.
I needed it to get the x64 Program Folder from a Logonscript and used: