Set the Default Search Engine Provider of IE with

2019-03-30 07:07发布

I would like to set the Default Search Engine Provider of IE with IOpenServiceManager::InstallService:

  1. Belong to the link http://www.opensearch.org/Specifications/OpenSearch/1.1#OpenSearch_description_elements. I created the SearchProviderInfo.xml like this:

    <?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. I build the project ok. Both file SetDefaultHelper.exe and SearchProviderInfo.xml are same folder. In the project setting, set Configuration Properties > Debugging > Commands Arguments = /url absolutePaht/searchProvider.xml. Then run debug (F10), at line "hr = CreateUri(argv[2], 0, 0, &spUri);", the rusult hr is so stranger. I don't know why. Can you help me?

Thank you very much.

[Resolved]:
1. Don't need CreateUri //commented
2. Use a absolutely path.

1条回答
Lonely孤独者°
2楼-- · 2019-03-30 08:02

Use absolute path and UrlCreateFromPath to create a file:/// like URL, pass that URL to InstallService.

Between, it seems that your XML has error.

            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!
                    }
                }
            }
查看更多
登录 后发表回答