简体   繁体   English

检测IShellItem引用库的正确方法是什么?

[英]What is the correct way to detect that an IShellItem refers to a Library?

I'm using the common file dialog with FOS_PICKFOLDERS to let the user pick a location to save files. 我正在将公用文件对话框与FOS_PICKFOLDERS配合使用,以使用户选择保存文件的位置。 If the user selects a library, eg Library\\Documents then my current code fails at the point where I call IShellItem::GetDisplayName to extract a file system name. 如果用户选择一个库,例如Library\\Documents则我当前的代码在我调用IShellItem::GetDisplayName提取文件系统名称时失败。 If the item were a file then this would succeed and the library's default save location would be used. 如果该项目是文件,则将成功,并且将使用库的默认保存位置。

What I would like to do is to detect that the shell item is a library, then obtain an IShellLibrary interface, and then query it to find the default save location. 我想做的是检测外壳项目是一个库,然后获取IShellLibrary接口,然后查询它以找到默认的保存位置。 Then I would save my files there. 然后,我将文件保存在那里。

What is the correct way to detect that an IShellItem refers to a Library? 检测IShellItem引用库的正确方法是什么?

Use SHLoadLibraryFromItem() to get an IShellLibrary from an IShellItem , eg: 使用SHLoadLibraryFromItem()IShellItem获取IShellLibrary ,例如:

IShellItem *pItem, *pSave;
IShellLibrary *pLibrary;
...
if (SUCCEEDED(SHLoadLibraryFromItem(pItem, STGM_READWRITE, IID_IShellLibrary, (void**)&pLibrary)))
{
    pLibrary->GetDefaultSaveFolder(DSFT_DETECT, IID_IShellItem, (void**)&pSave);
    pLibrary->Release();
}
else
{
    pSave = pItem;
    pSave->AddRef();
}
...
pSave->GetDisplayName(...);
pSave->Release();

The only way I found was to use IShellLibrary::LoadLibraryFromItem ( MSDN here ), to which you pass an IShellItem interface. 我发现的唯一方法是使用IShellLibrary::LoadLibraryFromItem此处MSDN ),并向其传递IShellItem接口。

If it fails (ie HRESULT != S_OK ), then the IShellItem is not a library. 如果失败(即HRESULT != S_OK ),则IShellItem 不是库。

So something like this: 所以像这样:

bool IsLibrary(IShellItem *pItem)
{
    bool bIsLibrary = false;

    IShellLibrary *plib = NULL;
    HRESULT hr = CoCreateInstance(CLSID_ShellLibrary, NULL, CLSCTX_INPROC_SERVER, 
        IID_PPV_ARGS(&plib));
    if (SUCCEEDED(hr))
    {
        hr = plib->LoadLibraryFromItem(pItem, STGM_READ);
        if (SUCCEEDED(hr)) bIsLibrary = true;
        plib->Release();
    }
    return bIsLibrary;
}

I have no idea if it's the "correct" way, but it may be useful anyway. 我不知道这是否是“正确”的方法,但是无论如何它可能还是有用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM