gpt4 book ai didi

c++ - 使用 IOpenServiceManager::InstallService 设置 IE 的默认搜索引擎提供程序

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:04:52 24 4
gpt4 key购买 nike

我想用 IOpenServiceManager::InstallService 设置 IE 的默认搜索引擎提供程序:

  1. 属于链接http://www.opensearch.org/Specifications/OpenSearch/1.1#OpenSearch_description_elements .我这样创建了 SearchProviderInfo.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
    <ShortName>Web Search</ShortName>
    <Description>Use Example.com to search the Web.</Description>
    <Tags>example web</Tags>
    <Contact>admin@example.com</Contact>
    <Url type="application/atom+xml"
    template="http://example.com/?q={searchTerms}&pw={startPage?}&format=atom"/>
    <Url type="application/rss+xml"
    template="http://example.com/?q={searchTerms}&pw={startPage?}&format=rss"/>
    <Url type="text/html"
    template="http://example.com/?q={searchTerms}&pw={startPage?}"/>
    <LongName>Example.com Web Search</LongName>
    <Image height="64" width="64" type="image/png">http://example.com/websearch.png</Image>
    <Image height="16" width="16" type="image/vnd.microsoft.icon">http://example.com/websearch.ico</Image>
    <Query role="example" searchTerms="cat" />
    <Developer>Example.com Development Team</Developer>
    <Attribution>
    Search data Copyright 2005, Example.com, Inc., All Rights Reserved
    </Attribution>
    <SyndicationRight>open</SyndicationRight>
    <AdultContent>false</AdultContent>
    <Language>en-us</Language>
    <OutputEncoding>UTF-8</OutputEncoding>
    <InputEncoding>UTF-8</InputEncoding>
    </OpenSearchDescription>

  2. Belong to the link http://msdn.microsoft.com/en-us/library/cc849088%28v=vs.85%29.aspx. I create the project "SetDefaultHelper" like this:

        #include <windows.h>    #include <atlbase.h>    #include <wininet.h>    #include <urlmon.h>    #include <string>    #include "openservice.h"        #pragma comment(lib, "urlmon.lib")void DisplayUsage(){    wprintf(L"\r\nSetDefaultHelper.exe -- Call SetDefault API on a search provider");    wprintf(L"\r\n");    wprintf(L"\r\nUSAGE: SetDefaultHelper.exe <option>");    wprintf(L"\r\n");    wprintf(L"\r\nOptions (these are mutually exclusive!):");    wprintf(L"\r\n");    wprintf(L"\r\n    /guid <guid>      GUID of an installed search provider");    wprintf(L"\r\n    /url <url>        URL of an OpenSearch Description file");    wprintf(L"\r\n");}int __cdecl wmain(__in int argc, __in_ecount(argc) WCHAR* argv[]){    HRESULT hr = E_FAIL;    BOOL fComInitialized = FALSE;        if (3 != argc)    {         DisplayUsage();    }    else if (SUCCEEDED(CoInitialize(NULL)))    {        fComInitialized = TRUE;        CComPtr<IOpenServiceManager> spManager;        hr = spManager.CoCreateInstance(CLSID_OpenServiceManager);            if (SUCCEEDED(hr))        {            CComPtr<IOpenService> spService;                if (0 == _wcsicmp(argv[1], L"/guid"))            {                // Get an IOpenService pointer from the GUID.                WCHAR szEscaped[INTERNET_MAX_URL_LENGTH] = L"";                DWORD cchEscaped = ARRAYSIZE(szEscaped);                hr = UrlEscape(argv[2], szEscaped, &cchEscaped, URL_ESCAPE_SEGMENT_ONLY);                    if (SUCCEEDED(hr))                {                                            std::wstring wsOsid(L"x-osid:1:search:");                    wsOsid += szEscaped;                    hr = spManager->GetServiceByID(wsOsid.c_str(), &spService);                }              }            else if (0 == _wcsicmp(argv[1], L"/url"))            {                // Install the provider to get an IOpenService pointer.                //CComPtr<IUri> spUri;                //hr = CreateUri(argv[2], 0, 0, &spUri);                //if (SUCCEEDED(hr))                //{                    hr = spManager->InstallService(argv[2], &spService);                //}            }            else            {                DisplayUsage();                hr = E_FAIL;            }
            if (SUCCEEDED(hr))
    {
    hr = spService-&#62;SetDefault(TRUE, NULL);
    }
    }
    }

    if (fComInitialized)
    {
    CoUninitialize();
    }

    return hr;
    }
  3. 我构建的项目没问题。文件 SetDefaultHelper.exe 和 SearchProviderInfo.xml 都是同一个文件夹。在项目设置中,设置 Configuration Properties > Debugging > Commands Arguments =/url absolutePaht/searchProvider.xml。然后运行调试 (F10),在“hr = CreateUri(argv[2], 0, 0, &spUri);”行,结果 hr 太奇怪了。我不知道为什么。你能帮帮我吗?

非常感谢。

[已解决]:
1.不需要CreateUri//commented
2. 使用绝对路径。

最佳答案

使用绝对路径和UrlCreateFromPath创建一个类似file:///的URL,将该URL传递给InstallService

之间,看来你的XML有错误。

            WCHAR szURL[MAX_PATH] = L"";
DWORD cchURL = ARRAYSIZE(szURL);
hr = ::UrlCreateFromPath(argv[2], szURL, &cchURL, 0);
if (SUCCEEDED(hr))
{
hr = spManager->InstallService(argv[2], &spService);

// Now we can set it as the default.
if (SUCCEEDED(hr))
{
hr = spService->SetDefault(TRUE, NULL);
if (hr == OS_E_CANCELLED)
{
hr = E_ACCESSDENIED; // by the user!
}
}
}

关于c++ - 使用 IOpenServiceManager::InstallService 设置 IE 的默认搜索引擎提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9528088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com