0

为什么 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();

(是的,我偶然发现了这个问题,但我不需要所有枚举的东西。)

4

2 回答 2

1

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

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);
    }
}
于 2013-06-25T14:03:17.867 回答
1

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

于 2013-06-24T15:14:44.447 回答