Principles and discussion of Windows service writing "3"

Source: Internet
Author: User
(iii) Under intensive discussion of services

Now we have one more function to discuss in detail, which is the Ctrlhandler function of the service.
When the RegisterServiceCtrlHandler function is invoked, the SCM obtains and saves the address of the callback function. An SCP called a Win32 function that tells SCM how to control the service, there are now 10 predefined control requests:

Control code

Meaning

Service_control_stop Requests the service to stop. Thehservice 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 it 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 and because the system is shutting down. For more information, 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. 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.

The newly added control code in 2000 is the winner of Windows 2000 in the table above. In addition to the code, the service can accept user-defined code that ranges from 128-255.
When the Ctrlhandler function receives a service_control_stop, Service_control_pause, service_control_continue control code, the SetServiceStatus will Must be called to confirm the code and specify the time that you think the service will take to handle this state change.
For example: Your service receives a stop request, first of all, set the Dwcurrentstate member of the SERVICE_STATUS structure to service_stop_pending, so that the SCM can determine that you have received the control code. When a service's pause or stop operation is executing, you must specify the time that you think the operation will take: this is because a service may not change its state immediately, it may have to wait for a network request to be completed or the data will be flushed to a drive. Method of specifying time as I said in the previous chapter, members Dwcheckpoint and dwwaithint are used to indicate the time it takes to complete state changes. If necessary, you can report progress periodically by increasing the value of the Dwcheckpoint member and setting the value of the Dwwaithint member to indicate how you expect the service to arrive next time.
Once the entire startup process is complete, call SetServiceStatus again. At this point will be the service_status structure of the Dwcurrentstate member set to service_stopped, when reporting the status code, must be the member Dwcheckpoint and Dwwaithint set to 0, Because the service has already completed its state changes. The same method is used when pausing or continuing the service.
When the Ctrlhandler function receives a Service_control_interrogate control code, the service will simply set the Dwcurrentstate member to the current state of service, while the member Dwcheckpoint and Dwwaithint set to 0, and then call SetServiceStatus on it.
  When the operating system shuts down, the Ctrlhandler function receives a Service_control_shutdown control code. The service simply does not need to respond to this code because the system is about to close. It will perform the minimum set of actions needed to save the data in order to make sure that the machine shuts down in time. By default, the system only gives a very small amount of time to turn off all services, and MSDN says it's about 20 seconds, but that's probably the Windows nt 4 setting, which is 10 seconds in my Windows2000 Server, and you can manually modify this number. It is recorded in the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control of the waittokillservicetimeout, in milliseconds.


When the Ctrlhandler function receives any user-defined code, it should perform the desired user-defined action. The SetServiceStatus function is not tuned unless the user-defined action forces the service to pause, resume, or stop. If a user-defined action forces a change in the state of the service, SetServiceStatus will be invoked to set up Dwcurrentstate, Dwcheckpoint, and Dwwaithint, with the exact control code as previously stated.
If your Ctrlhandler function takes a long time to perform an operation, be aware that SCM will return an error if the Ctrlhandler function does not return within 30 seconds, which is not what we expect. So if this happens, the best thing to do is to create a second thread that will continue to perform the operation so that the Ctrlhandler function can be returned quickly. For example, when a service_control_stop request is received, as stated above, the service may be waiting for a network request to be completed or the data will be flushed to a drive that you cannot estimate. Then it's time to create a new thread. The Stop command is executed after the operation completes, and the Ctrlhandler function still reports the service_stop_pending state before returning it, and when the new thread finishes the operation, it sets the state of the service to Service_ STOPPED. If the current operation time can be estimated to do not do so, still use the method described in the preceding process.
Ctrlhandler function I'm going to say this first, and here's how the service is installed. A service program can use the CreateService function to add information about a service to the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.