(三)对服务的深入讨论之下
现在我们还剩下一个函数可以在细节上讨论,那就是服务的CtrlHandler函数。
当调用RegisterServiceCtrlHandler函数时,SCM得到并保存这个回调函数的地址。一个SCP调一个告诉SCM如何去控制服务的Win32函数,现在已经有10个预定义的控制请求:
Control code
|
Meaning
|
SERVICE_CONTROL_STOP |
Requests the service to stop. The hService handle must have SERVICE_STOP access. |
SERVICE_CONTROL_PAUSE |
Requests the service to pause. The hService handle must have SERVICE_PAUSE_CONTINUE access. |
SERVICE_CONTROL_CONTINUE |
Requests the paused service to resume. The hService handle must have SERVICE_PAUSE_CONTINUE access. |
SERVICE_CONTROL_INTERROGATE |
Requests the service to update immediately its current status information to the service control manager. The hService handle must have SERVICE_INTERROGATE access. |
SERVICE_CONTROL_SHUTDOWN |
Requests the service to perform cleanup tasks, because the system is shutting down. For more information, see Remarks. |
SERVICE_CONTROL_PARAMCHANGE |
Windows 2000: Requests the service to reread its startup parameters. The hService handle must have SERVICE_PAUSE_CONTINUE access. |
SERVICE_CONTROL_NETBINDCHANGE |
Windows 2000: Requests the service to update its network binding. The hService handle must have SERVICE_PAUSE_CONTINUE access. |
SERVICE_CONTROL_NETBINDREMOVE |
Windows 2000: Notifies a network service that a component for binding has been removed. The service should reread its binding information and unbind from the removed component. |
SERVICE_CONTROL_NETBINDENABLE |
Windows 2000: Notifies a network service that a disabled binding has been enabled. The service should reread its binding information and add the new binding. |
SERVICE_CONTROL_NETBINDDISABLE |
Windows 2000: Notifies a network service that one of its bindings has been disabled. The service should reread its binding information and remove the binding. |
上表中标有Windows 2000字样的就是2000中新添加的控制代码。除了这些代码之外,服务也可以接受用户定义的,范围在128-255之间的代码。
当CtrlHandler函数收到一个SERVICE_CONTROL_STOP、SERVICE_CONTROL_PAUSE、 SERVICE_CONTROL_CONTINUE控制代码的时候,SetServiceStatus必须被调用去确认这个代码,并指定你认为服务处理这个状态变化所需要的时间。
例如:你的服务收到了停止请求,首先要把SERVICE_STATUS结构的dwCurrentState成员设置成SERVICE_STOP_PENDING,这样可以使SCM确定你已经收到了控制代码。当一个服务的暂停或停止操作正在执行的时候,必须指定你认为这种操作所需要的时间:这是因为一个服务也许不能立即改变它的状态,它可能必须等待一个网络请求被完成或 |