Querying a Service's Configuration

Querying a Service's Configuration

In the following example, a service configuration program uses the OpenService function to get a handle with SERVICE_QUERY_CONFIG access to an installed service object. Then the program uses the service object handle in the QueryServiceConfig function to retrieve the current configuration of the service.

#include <windows.h>
#include <stdio.h>

BOOL GetSampleServiceConfig() 
{ 
    LPQUERY_SERVICE_CONFIG lpqscBuf; 
    LPSERVICE_DESCRIPTION lpqscBuf2;
    DWORD dwBytesNeeded; 
    BOOL bSuccess=TRUE;
 
    // Open a handle to the service. 
 
    schService = OpenService( 
        schSCManager,           // SCManager database 
        TEXT("Sample_Srv"),     // name of service 
        SERVICE_QUERY_CONFIG);  // need QUERY access 
    if (schService == NULL)
    { 
        printf("OpenService failed (%d)", GetLastError());
        return FALSE;
    }
 
    // Allocate a buffer for the configuration information.
 
    lpqscBuf = (LPQUERY_SERVICE_CONFIG) LocalAlloc( 
        LPTR, 4096); 
    if (lpqscBuf == NULL) 
    {
        return FALSE;
    }
 
    lpqscBuf2 = (LPSERVICE_DESCRIPTION) LocalAlloc( 
        LPTR, 4096); 
    if (lpqscBuf2 == NULL) 
    {
        return FALSE;
    }
 
    // Get the configuration information. 
 
    if (! QueryServiceConfig( 
        schService, 
        lpqscBuf, 
        4096, 
        &dwBytesNeeded) ) 
    {
        printf("QueryServiceConfig failed (%d)", GetLastError());
        bSuccess = FALSE; 
    }
 
    if (! QueryServiceConfig2( 
        schService, 
        SERVICE_CONFIG_DESCRIPTION,
        lpqscBuf2, 
        4096, 
        &dwBytesNeeded) ) 
    {
        printf("QueryServiceConfig2 failed (%d)", GetLastError());
        bSuccess = FALSE;
    }
 
    // Print the configuration information.
 
    printf("/nSample_Srv configuration: /n");
    printf(" Type: 0x%x/n", lpqscBuf->dwServiceType);
    printf(" Start Type: 0x%x/n", lpqscBuf->dwStartType);
    printf(" Error Control: 0x%x/n", lpqscBuf->dwErrorControl);
    printf(" Binary path: %s/n", lpqscBuf->lpBinaryPathName);

    if (lpqscBuf->lpLoadOrderGroup != NULL)
        printf(" Load order group: %s/n", lpqscBuf->lpLoadOrderGroup);
    if (lpqscBuf->dwTagId != 0)
        printf(" Tag ID: %d/n", lpqscBuf->dwTagId);
    if (lpqscBuf->lpDependencies != NULL)
        printf(" Dependencies: %s/n", lpqscBuf->lpDependencies);
    if (lpqscBuf->lpServiceStartName != NULL)
        printf(" Start Name: %s/n", lpqscBuf->lpServiceStartName);
    if (lpqscBuf2->lpDescription != NULL)
        printf(" Description: %s/n", lpqscBuf2->lpDescription);
 
    LocalFree(lpqscBuf); 
    LocalFree(lpqscBuf2); 

    return bSuccess;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值