Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1804337
  • 博文数量: 290
  • 博客积分: 10653
  • 博客等级: 上将
  • 技术积分: 3178
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-24 23:08
文章存档

2013年(6)

2012年(15)

2011年(25)

2010年(86)

2009年(52)

2008年(66)

2007年(40)

分类: C/C++

2009-07-02 19:58:44

Sending Control Requests to a Service

The following example uses the function to send a control value to a running service. Different control values require different levels of access to the service object. For example, a service object handle must have SERVICE_STOP access to send the SERVICE_CONTROL_STOP control code. When ControlService returns, a structure contains the latest status information for the service.

 

BOOL ControlSampleService(DWORD fdwControl)
{
    SERVICE_STATUS ssStatus;
    DWORD fdwAccess;
    DWORD dwStartTickCount, dwWaitTime;
 
    // The required service object access depends on the control.

 
    switch (fdwControl)
    {
        case SERVICE_CONTROL_STOP:
            fdwAccess = SERVICE_STOP;
            break;
 
        case SERVICE_CONTROL_PAUSE:
        case SERVICE_CONTROL_CONTINUE:
            fdwAccess = SERVICE_PAUSE_CONTINUE;
            break;
 
        case SERVICE_CONTROL_INTERROGATE:
            fdwAccess = SERVICE_INTERROGATE;
            break;
 
        default:
            fdwAccess = SERVICE_INTERROGATE;
    }
 
    // Open a handle to the service.

 
    schService = OpenService(
        schSCManager, // SCManager database

        TEXT("Sample_Srv"), // name of service

        fdwAccess); // specify access

    if (schService == NULL)
    {
        printf("OpenService failed (%d)\n", GetLastError());
        return FALSE;
    }
 
    // Send a control value to the service.

 
    if (! ControlService(
            schService, // handle to service

            fdwControl, // control value to send

            &ssStatus) ) // address of status info

    {
        printf("ControlService failed (%d)\n", GetLastError());
        return FALSE;
    }
 
    // Print the service status.

 
    printf("\nStatus of Sample_Srv: \n");
    printf(" Service Type: 0x%x\n", ssStatus.dwServiceType);
    printf(" Current State: 0x%x\n", ssStatus.dwCurrentState);
    printf(" Controls Accepted: 0x%x\n",
        ssStatus.dwControlsAccepted);
    printf(" Exit Code: %d\n", ssStatus.dwWin32ExitCode);
    printf(" Service Specific Exit Code: %d\n",
        ssStatus.dwServiceSpecificExitCode);
    printf(" Check Point: %d\n", ssStatus.dwCheckPoint);
    printf(" Wait Hint: %d\n", ssStatus.dwWaitHint);
 
    return TRUE;
}

阅读(694) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~