Jeffrey&Lynny

一个温馨小家庭的.Net生活

导航

Windows Service

1. After installing the service, the SCP only writes the service exe information in the SCM database, so any modification to the exe will be valid, since the exe path does not change. The exe file will not be locked by the SCM.
2. StartServiceCtrlDispatcher will return only after ServiceMain invoked SetServiceStatus to set SERVICE_STOPPED.
3. The service.msc/SCM will recognize the response of the service with SetServiceStatus API. So the several key points to SCM interoperation are:
    @. Calling SetServiceStatus after RegisterServiceCtrlHandlerEx in ServiceMain SERVICE_RUNNING, so that the service can start successfully. 
    @. Calling SetServiceStatus in HandlerEx with Pending, then in ServiceMain with corresponding status finish, so that corresponding controlling request from SCM will succeed.

Below is the basic programming model:

1. Main thread #1
void main()
{
[following func must be called in 30m, or the SCM will kill process]
StartServiceCtrlDispatcher(ServiceMain...);
[return after all ServiceMain return, the service is allowed to run 30m or kill]
}

HandlerEx()
{
}

2. Thread #2
ServiceMain(){

[SERVICE_RUNNING must be called in 80m, or SCM assumes it as failed]
SetServiceStatus(SERVICE_RUNNING);
RegisterServiceCtrlHandlerEx(HandlerEx...);

Wait in a loop for:
1. client request (IPC)
2. HandlerEx control (ITC)

}

posted on 2006-06-11 21:57  比尔盖房  阅读(336)  评论(0编辑  收藏  举报