winapiclsid

Winapi: programatically obtain path of special folder given its CLSID as string


Does Windows API offer any way to obtain a special folder path (i.e. My Documents), given its CLSID as string (i.e. ::{450d8fba-ad25-11d0-98a8-0800361b1103})? Can this be done in any way? Also, it should be done with functions supported under Windows XP.

Thank you in advance.


Solution

  • The fundamental API you need for this is SHParseDisplayName. That will take the ::{GUID} format path and convert it to a PIDL.

    If the PIDL then has a string form (as your example does, since it resolves to the Documents folder), you can use SHGetPathFromIDList to convert it.

    LPITEMIDLIST pidl;
    if (SUCCEEDED(SHParseDisplayName(L"::{450d8fba-ad25-11d0-98a8-0800361b1103}", nullptr, &pidl, 0, nullptr)))
    {
        wchar_t wchPath[MAX_PATH];
        if (SUCCEEDED(SHGetPathFromIDList(pidl, wchPath)))
        {
            // string form of path is now in wchPath
        }
        ILFree(pidl);
    }