void createShellLink()

in skiko/src/awtMain/cpp/windows/JumpList.cc [92:136]


void createShellLink(
    JNIEnv* env,
    const std::wstring& title,
    const std::wstring& arguments,
    const std::optional<std::wstring>& description,
    const std::optional<std::pair<std::wstring, int>>& icon,
    IShellLinkW **ppsl)
{
    ComPtr<IShellLinkW> psl;
    THROW_IF_FAILED(
        CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl)),
        "Failed to create a shell link.",
    );

    // Use current executable for the shell link. Can contain a verbatim (\\?\) path so MAX_PATH might not be sufficient.
    WCHAR szAppPath[1024];
    if (0 == GetModuleFileNameW(NULL, szAppPath, ARRAYSIZE(szAppPath))) {
        THROW_IF_FAILED(HRESULT_FROM_WIN32(GetLastError()), "Failed to get current module's path.",);
    }

    THROW_IF_FAILED(psl->SetPath(szAppPath), "Failed to set shell link's path.",);
    THROW_IF_FAILED(psl->SetArguments(arguments.c_str()), "Failed to set shell link's arguments.",);

    if (description.has_value()) {
        THROW_IF_FAILED(psl->SetDescription(description.value().c_str()), "Failed to set shell link's description.",);
    }

    if (icon.has_value()) {
        const auto& [iconPath, iconNum] = icon.value();
        THROW_IF_FAILED(psl->SetIconLocation(iconPath.c_str(), iconNum), "Failed to set shell link's icon.",);
    }

    // The title property is required on Jump List items provided as an IShellLink instance.
    // This value is used as the display name in the Jump List.
    ComPtr<IPropertyStore> pps;
    THROW_IF_FAILED(psl->QueryInterface(IID_PPV_ARGS(&pps)), "Failed to cast shell link to IPropertyStore.",);

    PropVariantWrapper propvar(title.c_str());
    THROW_IF_FAILED(propvar, "Failed to create a PropVariant.",);

    THROW_IF_FAILED(pps->SetValue(PKEY_Title, propvar), "Failed to set shell link's title.",);
    THROW_IF_FAILED(pps->Commit(), "Failed to commit shell link's title.",);

    THROW_IF_FAILED(psl->QueryInterface(IID_PPV_ARGS(ppsl)), "Failed to return the shell link.",);
}