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

时间:2012-03-02 04:40:59

标签: c++ internet-explorer internet-explorer-8 internet-explorer-9 search-engine

我想用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. 属于http://msdn.microsoft.com/en-us/library/cc849088%28v=vs.85%29.aspx链接。我像这样创建项目“SetDefaultHelper”:

        #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都是相同的文件夹。在项目设置中,设置配置属性&gt;调试&gt;命令Arguments = / url absolutePaht / searchProvider.xml 。然后运行调试(F10),在行“hr = CreateUri(argv [2],0,0,&amp; spUri);”,rusult hr是如此陌生。我不知道为什么。你能帮帮我吗?

  4. 非常感谢。

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

1 个答案:

答案 0 :(得分:3)

使用绝对路径和UrlCreateFromPath创建类似file:///的网址,将该网址传递给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!
                    }
                }
            }