从 UWP 应用程序中提取图标

Extract icon from UWP application

本文关键字:提取 图标 应用程序 UWP      更新时间:2023-10-16

在尝试实现"打开方式"功能时,我遇到了从UWP应用程序中提取图标的问题。因此,在收到在SHAssocEnumHandlers的帮助下打开特定文件的推荐应用程序列表后,我正在尝试在IAssocHandler::GetIconLocation和经典ExtractIcon()的帮助下为每个应用程序提取图标。例如,对于像Paint这样的程序,一切都可以正常工作。我有 Paint 二进制文件的完整路径,可以从中提取图标。但是使用"3D构建器","照片"和其他UWP应用程序等应用程序获得的图标位置看起来像@{Microsoft.Windows.Photos_16.511.8630.0_x64__8wekyb3d8bbwe?ms-resource://Microsoft.Windows.Photos/Files/Assets/PhotosAppList.png}。我尝试了几个不同的 API 来提取图标,每次都收到FILE_NOT_FOUND错误。那么,在这种情况下,谁能给我一个提示,哪个功能可以用来提取图标?

更新添加了源代码的某些部分以澄清情况:

// m_handlers is a member of type std::vector<CComPtr<IAssocHandler>>
HRESULT FileManager::GetAssocHandlers(const std::wstring& strFileExtension, ASSOC_FILTER filter)
{
    HRESULT hr = S_OK;
    CComPtr<IEnumAssocHandlers> enumerator;
    m_handlers.clear();
    hr = SHAssocEnumHandlers(strFileExtension.c_str(), filter, &enumerator);
    if (SUCCEEDED(hr))
    {
        for (CComPtr<IAssocHandler> handler;
            enumerator->Next(1, &handler, nullptr) == S_OK;
            handler.Release())
        {
            m_handlers.push_back(handler);
        }
    }
    return hr;
}
HRESULT FileManager::GetAssociatedPrograms(BSTR bstrFileName, BSTR* bstrRet)
{
    ...
    hr = GetAssocHandlers(strFileExtension, ASSOC_FILTER_RECOMMENDED);
    if (SUCCEEDED(hr))
    {
        ...
        for (auto& handler : m_handlers)
        {
            ...
            if (SUCCEEDED(handler->GetIconLocation(&tmpStr, &resourceIndex)))
            {
                // And this is where I get classic full file path to regular
                // applications like "MS Paint" or this weird path mentioned
                // above for "Photos" UWP application for example which can't
                // be used in regular ExtractIcon functions.
            }
        }
    }
}

看起来我已经找到了解决方案。根据 MSDN,为 UWP 应用程序提供的图标位置路径称为"间接字符串"。我们可以将此间接字符串传递给 SHLoadIndirectString 函数,并将接收图标 PNG 文件的常规完整路径。在我的情况下,在将@{Microsoft.Windows.Photos_16.511.8630.0_x64__8wekyb3d8bbwe?ms-resource://Microsoft.Windows.Photos/Files/Assets/PhotosAppList.png}传递给 SHLoadIndirectString() 后,我收到了这样的路径:C:Program FilesWindowsAppsMicrosoft.Windows.Photos_16.511.8630.0_neutral_split.scale-125_8wekyb3d8bbweAssetsPhotosAppList.scale-125.png之后,我可以使用它来显示图标本身,没有任何问题。