SERVICE_STATUS
SERVICE_STATUS_HANDLE
SetServiceStatus
RegisterServiceCtrlHandler
StartServiceCtrlDispatcher
这几个东西是建服务程序时用到的,我虽然看了MSDN的说明,但还是比较糊涂。请各位高手给解释解释,最重要的是他们怎么用?最好有代码并带注释!谢谢!谢谢!

解决方案 »

  1.   

    一个很好的例子:
    http://expert.csdn.net/Expert/topic/1682/1682207.xml?temp=1.104373E-02
      

  2.   

    typedef struct _SERVICE_STATUS { // ss 
        DWORD dwServiceType; 
        DWORD dwCurrentState; 
        DWORD dwControlsAccepted; 
        DWORD dwWin32ExitCode; 
        DWORD dwServiceSpecificExitCode; 
        DWORD dwCheckPoint; 
        DWORD dwWaitHint; 
    } SERVICE_STATUS, *LPSERVICE_STATUS; 
     
    Members
    dwServiceType 
    The value returned includes one of the following service type flags to indicate the type of service. In addition, for a SERVICE_WIN32 service, the SERVICE_INTERACTIVE_PROCESS flag might be set, indicating that the service process can interact with the desktop. Value Meaning 
    SERVICE_WIN32_OWN_PROCESS A service type flag that indicates a Win32 service that runs in its own process. 
    SERVICE_WIN32_SHARE_PROCESS A service type flag that indicates a Win32 service that shares a process with other services. 
    SERVICE_KERNEL_DRIVER A service type flag that indicates a device driver. 
    SERVICE_FILE_SYSTEM_DRIVER A service type flag that indicates a file system driver. 
    SERVICE_INTERACTIVE_PROCESS  A flag that indicates a Win32 service process that can interact with the desktop. 
    dwCurrentState 
    Indicates the current state of the service. One of the following values is specified: Value Meaning 
    SERVICE_STOPPED The service is not running. 
    SERVICE_START_PENDING The service is starting. 
    SERVICE_STOP_PENDING The service is stopping. 
    SERVICE_RUNNING The service is running. 
    SERVICE_CONTINUE_PENDING The service continue is pending. 
    SERVICE_PAUSE_PENDING The service pause is pending. 
    SERVICE_PAUSED The service is paused. 
    dwControlsAccepted 
    Specifies the control codes that the service will accept and process. A user interface process can control a service by specifying a control command in the ControlService function. By default, all services accept the SERVICE_CONTROL_INTERROGATE value. Any or all of the following flags can be specified to enable the other control codes. Value Meaning 
    SERVICE_ACCEPT_STOP 
     The service can be stopped. This enables the SERVICE_CONTROL_STOP value. 
    SERVICE_ACCEPT_PAUSE_CONTINUE 
     The service can be paused and continued. This enables the SERVICE_CONTROL_PAUSE and SERVICE_CONTROL_CONTINUE values. 
    SERVICE_ACCEPT_SHUTDOWN 
     The service is notified when system shutdown occurs. This enables the system to send a SERVICE_CONTROL_SHUTDOWN value to the service. The ControlService function cannot send this control code. 
    dwWin32ExitCode 
    Specifies an Win32 error code that the service uses to report an error that occurs when it is starting or stopping. To return an error code specific to the service, the service must set this value to ERROR_SERVICE_SPECIFIC_ERROR to indicate that the dwServiceSpecificExitCode member contains the error code. The service should set this value to NO_ERROR when it is running and on normal termination. 
    dwServiceSpecificExitCode 
    Specifies a service specific error code that the service returns when an error occurs while the service is starting or stopping. This value is ignored unless the dwWin32ExitCode member is set to ERROR_SERVICE_SPECIFIC_ERROR. 
    dwCheckPoint 
    Specifies a value that the service increments periodically to report its progress during a lengthy start, stop, pause, or continue operation. For example, the service should increment this value as it completes each step of its initialization when it is starting up. The user interface program that invoked the operation on the service uses this value to track the progress of the service during a lengthy operation. This value is not valid and should be zero when the service does not have a start, stop, pause, or continue operation pending. 
    dwWaitHint 
    Specifies an estimate of the amount of time, in milliseconds, that the service expects a pending start, stop, pause, or continue operation to take before the service makes its next call to the SetServiceStatus function with either an incremented dwCheckPoint value or a change in dwCurrentState. If the amount of time specified by dwWaitHint passes, and dwCheckPoint has not been incremented, or dwCurrentState has not changed, the service control manager or service control program can assume that an error has occurred. 
      

  3.   

    我说过MSDN上的说明,我不明白(英语不太好).请各位帮帮忙忙啊!
      

  4.   

    SERVICE_STATUS
    说的是服务状态,有几种状态,他们之间是互相转换的
    SERVICE_STOPPED 服务停止SERVICE_START_PENDING The service is starting. 
    SERVICE_STOP_PENDING 服务停止中
    SERVICE_CONTINUE_PENDING 服务状态未定,执行正在的操作
    SERVICE_PAUSE_PENDING 停止正在执行的操作
    SERVICE_PAUSED 暂停服务SetServiceStatus
    该函数是设置服务状态的
    RegisterServiceCtrlHandler
    服务是通过该函数注册到系统服务中去的
    StartServiceCtrlDispatcher
    The StartServiceCtrlDispatcher function connects the main thread of a service process to the service control manager, which causes the thread to be the service control dispatcher thread for the calling process.
    连接到制定服务进程《win32系统编程》一书中有详尽的介绍
      

  5.   

    《WIN32系统编程》是哪个出版社的?
      

  6.   

    给你一个例子吧
    SERVICE_STATUS          MyServiceStatus; 
    SERVICE_STATUS_HANDLE   MyServiceStatusHandle; void MyServiceStart (DWORD argc, LPTSTR *argv) 

        DWORD status; 
        DWORD specificError; 
     
        MyServiceStatus.dwServiceType        = SERVICE_WIN32; 
        MyServiceStatus.dwCurrentState       = SERVICE_START_PENDING; 
        MyServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP | 
            SERVICE_ACCEPT_PAUSE_CONTINUE; 
        MyServiceStatus.dwWin32ExitCode      = 0; 
        MyServiceStatus.dwServiceSpecificExitCode = 0; 
        MyServiceStatus.dwCheckPoint         = 0; 
        MyServiceStatus.dwWaitHint           = 0; 
     
        MyServiceStatusHandle = RegisterServiceCtrlHandler( 
            "MyService", 
            MyServiceCtrlHandler); 
     
        if (MyServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) 
        { 
            SvcDebugOut(" [MY_SERVICE] RegisterServiceCtrlHandler 
                failed %d\n", GetLastError()); 
            return; 
        } 
     
        // Initialization code goes here. 
        status = MyServiceInitialization(argc,argv, &specificError); 
     
        // Handle error condition 
        if (status != NO_ERROR) 
        { 
            MyServiceStatus.dwCurrentState       = SERVICE_STOPPED; 
            MyServiceStatus.dwCheckPoint         = 0; 
            MyServiceStatus.dwWaitHint           = 0; 
            MyServiceStatus.dwWin32ExitCode      = status; 
            MyServiceStatus.dwServiceSpecificExitCode = specificError; 
     
            SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus); 
            return; 
        } 
     
        // Initialization complete - report running status. 
        MyServiceStatus.dwCurrentState       = SERVICE_RUNNING; 
        MyServiceStatus.dwCheckPoint         = 0; 
        MyServiceStatus.dwWaitHint           = 0; 
     
        if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus)) 
        { 
            status = GetLastError(); 
            SvcDebugOut(" [MY_SERVICE] SetServiceStatus error
                %ld\n",status); 
        } 
     
        // This is where the service does its work. 
        SvcDebugOut(" [MY_SERVICE] Returning the Main Thread \n",0); 
     
        return; 

     
    // Stub initialization function. 
    DWORD MyServiceInitialization(DWORD   argc, LPTSTR  *argv, 
        DWORD *specificError) 

        argv; 
        argc; 
        specificError; 
        return(0);