获取Windows控制面板的虚拟文件夹

时间:2013-06-24 15:02:41

标签: winapi com

为什么GetPath在查询FOLDERID_ControlPanelFolder时总是返回E_FAIL?其他FOLDERID实际上可以工作:

HRESULT hr = S_OK;
*path = '\0';
LPWSTR pwcPath = NULL;

CoInitialize(NULL);

IKnownFolderManager *pFolderManager = NULL;
if ((hr = CoCreateInstance(__uuidof(KnownFolderManager), NULL, CLSCTX_INPROC_SERVER, __uuidof(IKnownFolderManager), (LPVOID *)&pFolderManager)) == S_OK)
{
    IKnownFolder *pControlPanelFolder = NULL;
    if ((hr = pFolderManager->GetFolder(FOLDERID_ControlPanelFolder, &pControlPanelFolder)) == S_OK)
    {
        hr = pControlPanelFolder->GetPath(0, &pwcPath);

        if (hr == S_OK && pwcPath)
        {
            int nSize = wcslen(pwcPath);
            WideCharToMultiByte(CP_ACP, 0, pwcPath, nSize, path, nSize+2, NULL, NULL);
            path[nSize] = '\0';
            CoTaskMemFree(pwcPath);
        }

        pControlPanelFolder->Release();
        pControlPanelFolder = NULL;
    }

    pFolderManager->Release();
    pFolderManager = NULL;
}

CoUninitialize();

(是的,我偶然发现了this question,但我并不需要所有枚举内容。)

2 个答案:

答案 0 :(得分:1)

控制面板没有目录路径,因为它不存在于光盘上。你可以得到它的PIDL,甚至是桌面绝对解析“显示名称”(通过GetShellItem和GetDisplayName),但不是目录路径。

答案 1 :(得分:1)

我需要路径的原因是我想用ShellExecute“open”打开controp面板。我现在直接执行控制面板程序,其好处是能够立即选择所需的applet(在本例中为“Sound”)。我希望我发布这个作为答案并不太自命:

char controlpanelpath[2000];
UINT controlpanelpathbuffersize = sizeof(controlpanelpath);
int actualcontrolpanelpathsize;
if (actualcontrolpanelpathsize = GetSystemDirectory(controlpanelpath, controlpanelpathbuffersize))
{
    char *parameters = "\\control.exe mmsys.cpl,,0";
    if (actualcontrolpanelpathsize + strlen(parameters) < controlpanelpathbuffersize)
    {
        strcat(controlpanelpath, parameters);
        WinExec(controlpanelpath, SW_NORMAL);
    }
}