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 0
PWSTR path = NULL;

HRESULT hr = SHGetKnownFolderPath(FOLDERID_ProgramFiles, 0, NULL, &path);
if (SUCCEEDED(hr))

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


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

// Not handled in Linux

return sv;