Windows Services Programming 14

 

 

 

 

 

Querying a Service's Configuration Example

 

A service configuration program uses the OpenService() function to get a handle with SERVICE_QUERY_CONFIG access to an installed service object. The program can then use the service object handle in the QueryServiceConfig() and QueryServiceConfig2() functions to retrieve the current configuration of the service.

In the following code fragment example, the DoQuerySvc() function uses QueryServiceConfig() and QueryServiceConfig2() to retrieve configuration information, then writes selected information to the console. The szSvcName variable is a global variable that contains the name of the service.

 

// Purpose: Retrieves and displays the current service configuration.

// Parameters: None

// Return value: None

void __stdcall DoQuerySvc(void)

{

    SC_HANDLE schSCManager;

    SC_HANDLE schService;

    LPQUERY_SERVICE_CONFIG lpsc;

    LPSERVICE_DESCRIPTION lpsd;

    DWORD dwBytesNeeded, cbBufSize, dwError;

 

    // Get a handle to the SCM database

    schSCManager = OpenSCManager(

        NULL,                    // local computer

        NULL,                    // ServicesActive database

        SC_MANAGER_ALL_ACCESS);  // full access rights

 

    if (schSCManager == NULL)

    {

        wprintf(L"OpenSCManager() failed, error %u\n", GetLastError());

        return;

    }

      else

            wprintf(L"OpenSCManager() is working!\n");

 

    // Get a handle to the service

    schService = OpenService(

        schSCManager,          // SCM database

        szSvcName,             // name of service

        SERVICE_QUERY_CONFIG); // need query config access

 

    if (schService == NULL)

    {

        wprintf(L"OpenService() failed, error %u\n", GetLastError());

        CloseServiceHandle(schSCManager);

        return;

    }

      else

            wprintf(L"OpenService() is OK!\n");

 

    // Get the configuration information

    if(!QueryServiceConfig(schService, NULL, 0, &dwBytesNeeded))

    {

        dwError = GetLastError();

        if(ERROR_INSUFFICIENT_BUFFER == dwError)

        {

            cbBufSize = dwBytesNeeded;

            lpsc = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LMEM_FIXED, cbBufSize);

        }

        else

        {

            wprintf(L"QueryServiceConfig() failed, error %u\n", dwError);

            goto cleanup;

        }

    }

 

    if( !QueryServiceConfig(schService,lpsc,cbBufSize,&dwBytesNeeded))

    {

        wprintf(L"QueryServiceConfig() failed, error %u\n", GetLastError());

        goto cleanup;

    }

 

    if( !QueryServiceConfig2(schService,SERVICE_CONFIG_DESCRIPTION,NULL,0,&dwBytesNeeded))

    {

        dwError = GetLastError();

        if(ERROR_INSUFFICIENT_BUFFER == dwError)

        {

            cbBufSize = dwBytesNeeded;

            lpsd = (LPSERVICE_DESCRIPTION)LocalAlloc(LMEM_FIXED, cbBufSize);

        }

        else

        {

            wprintf(L"QueryServiceConfig2() failed, error %u\n", dwError);

            goto cleanup;

        }

    }

 

    if (!QueryServiceConfig2(schService,SERVICE_CONFIG_DESCRIPTION,(LPBYTE) lpsd,cbBufSize,&dwBytesNeeded))

    {

        wprintf(L"QueryServiceConfig2() failed, error %u\n", GetLastError());

        goto cleanup;

    }

 

    // Print the configuration information

    wprintf(L"%s configuration: \n", szSvcName);

    wprintf(L"  Type: 0x%x\n", lpsc->dwServiceType);

    wprintf(L"  Start Type: 0x%x\n", lpsc->dwStartType);

    wprintf(L"  Error Control: 0x%x\n", lpsc->dwErrorControl);

    wprintf(L"  Binary path: %s\n", lpsc->lpBinaryPathName);

    wprintf(L"  Account: %s\n", lpsc->lpServiceStartName);

 

    if (lpsd->lpDescription != NULL && lstrcmp(lpsd->lpDescription, L"") != 0)

        wprintf(L"  Description: %s\n", lpsd->lpDescription);

    if (lpsc->lpLoadOrderGroup != NULL && lstrcmp(lpsc->lpLoadOrderGroup, L"") != 0)

        wprintf(L"  Load order group: %s\n", lpsc->lpLoadOrderGroup);

    if (lpsc->dwTagId != 0)

        wprintf(L"  Tag ID: %d\n", lpsc->dwTagId);

    if (lpsc->lpDependencies != NULL && lstrcmp(lpsc->lpDependencies, L"") != 0)

        wprintf(L"  Dependencies: %s\n", lpsc->lpDependencies);

 

    LocalFree(lpsc);

    LocalFree(lpsd);

 

cleanup:

    CloseServiceHandle(schService);

    CloseServiceHandle(schSCManager);

}

 

 

 

 

< Windows Services 13 | Win32 Programming | Windows Services Win32 Programming | Windows Services 15 >