0

我使用的平台是windows 7。我需要为windows 7上的虚拟文件夹创建快捷方式。我使用windows 7 SDK示例在Computer下创建一个虚拟文件夹:

在此处输入图像描述

示例项目名称为ExplorerDataProvider,它定义了 IShellFolder 类的 CLSID:

// add classes supported by this module here
const CLASS_OBJECT_INIT c_rgClassObjectInit[] =
{
{ &CLSID_FolderViewImpl,            CFolderViewImplFolder_CreateInstance },
{ &CLSID_FolderViewImplContextMenu,CFolderViewImplContextMenu_CreateInstance },
};

的定义CFolderViewImplFolder_CreateInstance是:

HRESULT CFolderViewImplFolder_CreateInstance(REFIID riid, void **ppv)
{
*ppv = NULL;
CFolderViewImplFolder* pFolderViewImplShellFolder = new (std::nothrow) CFolderViewImplFolder(0);
HRESULT hr = pFolderViewImplShellFolder ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{
    hr = pFolderViewImplShellFolder->QueryInterface(riid, ppv);
    pFolderViewImplShellFolder->Release();
}
return hr;
}

CFolderViewImplFolder实施IShellFolder2amd IPersistFolder2。我在这里找到了用于为打印机创建快捷方式的类似代码: https ://www.codeproject.com/Articles/596642/Creating-a-shortcut-programmatically-in-Cplusplus 和 https://msdn.microsoft。 com/en-us/library/aa969393.aspx#Shellink_Item_Identifiers

一旦获得 IShellFolder 的类标识符,就可以调用 CoCreateInstance 函数来检索接口的地址。然后您可以调用该接口来枚举文件夹中的对象并检索您正在搜索的对象的项目标识符的地址。最后,您可以在调用 IShellLink::SetIDList 成员函数时使用该地址来创建对象的快捷方式。

我修改了

hr = SHGetMalloc(&pMalloc);
hr = SHGetDesktopFolder( &pDesktopFolder );
hr = SHGetSpecialFolderLocation( NULL, CSIDL_PRINTERS, &netItemIdLst );
hr = pDesktopFolder->BindToObject( netItemIdLst, NULL, IID_IShellFolder, (void **)&pPrinterFolder );

// testFolder is the CLSID for the virtual folder implementation
hr = CoCreateInstance(testFolder, NULL, CLSCTX_INPROC_SERVER, IID_IShellFolder, (LPVOID*)&pVirtualFolder);

或者

hr = CoCreateInstance(testFolder, NULL, CLSCTX_INPROC_SERVER, IID_IShellFolder2, (LPVOID*)&pVirtualFolder);

但是 pVirtualFolder 仍然是 NULL,并且打印出“找不到对应的接口”。

我用的时候有什么问题CoCreateInstance吗?或者我不应该使用这个解决方案?有任何示例代码吗?

4

1 回答 1

1

要创建快捷方式,可以使用官方文档这是一个示例代码,它为“这台电脑”(又名: ComputerFolder)的孩子创建快捷方式

int main()
{
    CoInitialize(NULL);
    // I've used my Apple's iCloud as an example (name is in french)
    // it's a virtual folder, a shell namespace extension
    HRESULT hr = CreateComputerChildShortCut(L"Photos iCloud", L"c:\\temp\\my icloud");
    printf("hr:0x%08X\n", hr);
    CoUninitialize();
    return 0;
}

HRESULT CreateComputerChildShortCut(LPWSTR childDisplayName, LPWSTR path)
{
    // get My Computer folder's ShellItem (there are other ways for this...)
    CComPtr<IShellItem> folder;
    HRESULT hr = SHCreateItemInKnownFolder(FOLDERID_ComputerFolder, 0, NULL, IID_PPV_ARGS(&folder));
    if (FAILED(hr)) return hr;

    // enumerate children
    CComPtr<IEnumShellItems> items;
    hr = folder->BindToHandler(NULL, BHID_EnumItems, IID_PPV_ARGS(&items));
    if (FAILED(hr)) return hr;

    for (CComPtr<IShellItem> item; items->Next(1, &item, NULL) == S_OK; item.Release())
    {
        // get parsing path (if's often useful)
        CComHeapPtr<wchar_t> parsingPath;
        item->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &parsingPath);
        wprintf(L"Path: %s\n", parsingPath);

        // get display name
        CComHeapPtr<wchar_t> displayName;
        item->GetDisplayName(SIGDN_NORMALDISPLAY, &displayName);
        wprintf(L" Name: %s\n", displayName);

        if (!lstrcmpi(childDisplayName, displayName))
        {
            // get pidl
            // it's the unambiguous way of referencing a shell thing
            CComHeapPtr<ITEMIDLIST> pidl;
            hr = SHGetIDListFromObject(item, &pidl);
            if (FAILED(hr)) return hr;

            // create an instance of the standard Shell's folder shortcut creator
            CComPtr<IShellLink> link;
            hr = link.CoCreateInstance(CLSID_FolderShortcut);
            if (FAILED(hr)) return hr;

            // just use the pidl
            hr = link->SetIDList(pidl);
            if (FAILED(hr)) return hr;

            CComPtr<IPersistFile> file;
            hr = link->QueryInterface(&file);
            if (FAILED(hr)) return hr;

            // save the shortcut (we could also change other IShellLink parameters)
            hr = file->Save(path, FALSE);
            if (FAILED(hr)) return hr;
            break;
        }
    }
    return S_OK;
}

当然,如果您知道绝对解析路径或绝对pidl,则不必枚举任何内容,这只是为了演示目的。

于 2017-10-27T10:43:28.530 回答