HRESULT GetKnownFolderForPath(PCWSTR pszPath, KNOWNFOLDERID *pkfid, KNOWNFOLDER_DEFINITION *pkfd)
{
    IKnownFolderManager *pkfm;
    HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm));
    if (SUCCEEDED(hr))
    {
        IKnownFolder *pkf;
        hr = pkfm->FindFolderFromPath(pszPath, FFFP_EXACTMATCH, &pkf);
        if (S_OK == hr)
        {
            hr = pkf->GetId(pkfid);
            if (S_OK == hr)
            {
                hr = pkf->GetFolderDefinition(pkfd);
                if (S_OK != hr)
                {
                    wprintf(L"IKnownFolderManager::GetFolderDefinition return hr=0x%x\n", hr);
                }
            }
            else
            {
                wprintf(L"IKnownFolder::GetId return hr=0x%x\n", hr);
            }
            pkf->Release();
        }
        else
        {
            wprintf(L"IKnownFolderManager::FindFolderFromPath return hr=0x%x\n", hr);
        }
        pkfm->Release();
    }
    return hr;
}
void EnumAndDumpKnownFolders(DWORD *pdwKFCount, PCWSTR pszNameSrchStr, REFKNOWNFOLDERID kfidSearch)
{
    *pdwKFCount = 0;
    IKnownFolderManager *pkfm = NULL;
    HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm));
    if (SUCCEEDED(hr))
    {
        KNOWNFOLDERID *rgKFIDs = NULL;
        UINT cKFIDs = 0;
        hr = pkfm->GetFolderIds(&rgKFIDs, &cKFIDs);
        if (SUCCEEDED(hr))
        {
            WCHAR szKFIDGuid[GUID_SIZE];
            IKnownFolder *pkfCurrent = NULL;
            for (UINT i = 0; i < cKFIDs; ++i)
            {
                // if we are searching for a specific GUID, make sure we match before going
                // any further.  GUID_NULL means "show all."
                if (kfidSearch == GUID_NULL || kfidSearch == rgKFIDs[i])
                {
                    StringFromGUID2(rgKFIDs[i], szKFIDGuid, ARRAYSIZE(szKFIDGuid));
                    hr = pkfm->GetFolder(rgKFIDs[i], &pkfCurrent);
                    if (SUCCEEDED(hr))
                    {
                        KNOWNFOLDERID kfid;
                        hr = pkfCurrent->GetId(&kfid);
                        if (FAILED(hr))
                        {
                            wprintf(L"IKnownFolder::GetId() failed for %s!  hr=0x%x\n", szKFIDGuid, hr);
                        }

                        KNOWNFOLDER_DEFINITION kfd;
                        hr = pkfCurrent->GetFolderDefinition(&kfd);
                        if (FAILED(hr))
                        {
                            wprintf(L"IKnownFolderManager::GetFolderDefinition() failed hr=0x%x KNOWNFOLDERID=%s", hr, szKFIDGuid);
                        }
                        else
                        {
                            BOOL fDumpThisFolder = TRUE;
                            if (pszNameSrchStr)
                            {
                                if (NULL == wcsstr(kfd.pszName, pszNameSrchStr))
                                {
                                    fDumpThisFolder = FALSE;
                                }
                            }

                            if (fDumpThisFolder)
                            {
                                ++*pdwKFCount;
                                DumpKnownFolderDef(kfid, kfd);
                                DumpKnownFolderInfo(pkfCurrent);
                            }

                            FreeKnownFolderDefinitionFields(&kfd);
                        }

                        pkfCurrent->Release();
                    }
                    else
                    {
                        wprintf(L"IKnownFolderManager::GetFolder() failed for %s  hr=0x%x\n", szKFIDGuid, hr);
                    }
                }
            }
            CoTaskMemFree(rgKFIDs);
        }
        pkfm->Release();
    }
}