<b>Windows服务编写原理及探究(3)</b>[VC/C++编程]
本文“<b>Windows服务编写原理及探究(3)</b>[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
(三)对服务的深化谈论之下
目前我们还剩下一个函数可以在细节上谈论,那就是服务的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肯定你已经收到了掌握代码.当一个服务的暂停或终止操作正在履行的时刻,必须指定你认为这种操作所需求的时间:这是因为一个服务大概不能当即改变它的状况,它大概必须等候一个网络恳求被完成大概数据被革新到一个驱动器上.指按时间的办法就像我上一章说的那样,用成员dwCheckPoint和dwWaitHint来指明它完成状况改变所需求的时间.假如需求,可以用增添dwCheckPoint成员的值和设置dwWaitHint成员的值去指明你等待的服务到达下一步的时间的方法周期性的报告进展情形.
当整个启动的历程完成之后,要再一次调用SetServiceStatus.这时就要把SERVICE_STATUS构造的dwCurrentState成员设置成SERVICE_STOPPED,当报告状况代码的同时,一定要把成员dwCheckPoint和dwWaitHint设置为0,因为服务已经完成了它的状况改变.暂停或持续服务的时刻办法也一样.
当CtrlHandler函数收到一个SERVICE_CONTROL_INTERROGATE掌握代码的时刻,服务将简单的将dwCurrentState成员设置成服务当前的状况,同时,把成员dwCheckPoint和dwWaitHint设置为0,然后再调用SetServiceStatus便可以了.
在操作系统关闭的时刻,CtrlHandler函数收到一个SERVICE_CONTROL_SHUTDOWN掌握代码.服务根本无须回应这个代码,因为系统行将关闭.它将履行保存数据所需求的最小行动集,这是为了肯定机械能及时关闭.缺省时系统只给很少的时间去关闭全部的服务,MSDN里面说大约是20秒的时间,不过那大概是Windows NT 4的设置,在我的Windows 2000 Server里这个时间是10秒,你可以手动的改正这个数值,它被记录在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control子键里面的WaitToKillServiceTimeout,单位是毫秒.
以上是“<b>Windows服务编写原理及探究(3)</b>[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |