Export (0) Print
Expand All

IOpenService::SetDefault Method

Sets the default Accelerator or search provider.

Syntax


HRESULT SetDefault(
    BOOL fDefault,
    HWND hwnd
);

Parameters

fDefault
[in] TRUE to set the provider as the default for its category; FALSE to remove the default.
hwnd
[in] The window handle to use for the confirmation and error dialogs, or NULL to specify no window handle.

Return Value

Returns S_OK if successful, or an error value otherwise.

Remarks

The SetDefault method can be used to set both a default Accelerator for a category or the default search provider.

Setting a default provider for a category will displace the current default in that category. If you set the fDefault parameter to FALSE to remove the default Accelerator, the category will have no default.

Setting a new default search provider causes Windows Internet Explorer to display a confirmation dialog box. If a user clicks the Cancel button on the dialog, the method returns OS_E_CANCELLED. Note that you cannot remove the default search provider, because it is required by the Search Box. This method returns OS_E_NOTSUPPORTED if you attempt to do so.

Example

The following sample code implements a command-line utility that can be used to install or set the default search provider. If a /guid argument is specified, the application instantiates the IOpenService object by using IOpenServiceManager::GetServiceByID. If a valid /url argument is provided, the application installs the OpenSearch provider with IOpenServiceManager::InstallService. Finally, the application uses IOpenService::SetDefault to set the search provider as the default.


#include <windows.h>
#include <atlbase.h>
#include <wininet.h>
#include <string>
#include "openservice.h"

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->SetDefault(TRUE, NULL);
            }
        }
    }

    if (fComInitialized)
    {
        CoUninitialize();
    }

    return hr;
}

See Also

IOpenServiceActivity::SetEnabled

Community Additions

ADD
Show:
© 2014 Microsoft