Why does FOLDERID_ProgramFiles return "C:Program Files (x86)"?

  c++, windows
#include <Shlobj.h>
#include <iostream>
#include <string>

std::string fun(REFKNOWNFOLDERID  val) {
    ITEMIDLIST* pIDList;
    if (S_OK == SHGetKnownFolderIDList (val, 0, NULL, & pIDList))
    {
        char cpath[MAX_PATH] = {0};
        if (TRUE == SHGetPathFromIDList(pIDList, cpath))
            return cpath;
    }
    return "failed";
}

int main() {
    std::cout << "--> " << fun(FOLDERID_ProgramFiles) << std::endl;
    std::cout << "--> " << fun(FOLDERID_ProgramFilesX86) << std::endl;
    std::cout << "--> " << fun(FOLDERID_ProgramFilesX64) << std::endl;
}

The output is:

--> C:Program Files (x86)
--> C:Program Files (x86)
--> failed

I compile it using cl.exe test.cpp shell32.lib.

I was expecting "C:Program Files" for FOLDERID_ProgramFiles. My system is x64 based. Why am I getting "Program Files (x86)"?

Source: Windows Questions

LEAVE A COMMENT