Information Systems



How to get the Program Files dir in Windows c++

Here's some psuedocode. Hopefully the external references are self-explanatory. One thing that's important to understand is that when running a 32 bit app, FOLDERID_ProgramFilesX86 and FOLDERID_ProgramFiles will both return the x86 version of the program files folder. I'm not sure what the behavior is for a 64 bit app, but the following code will work regardless.

StringVector GXAppState::getProgramFilesDir()
{
StringVector sv;
#if _WINDOWS
#if 0
PWSTR path = NULL;

HRESULT hr = SHGetKnownFolderPath(FOLDERID_ProgramFiles, 0, NULL, &path);
if (SUCCEEDED(hr))
sv.push_back(ParseHelper::wToStd(path));
#endif

std::string s = ParseHelper::expandEnvString("%ProgramFiles%");

sv.push_back(s);

if (ParseHelper::endsWith(s, " (x86)"))
{
sv.push_back(s.substr(0, s.size()-6));
}
else
{
sv.push_back(s + " (x86)");
}



#else
// Not handled in Linux
#endif

return sv;

}