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. 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 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, SetServiceStatus 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 the service, and Set the member Dwcheckpoint and Dwwaithint to 0, and then call SetServiceStatus.

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 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 Windows Server, and you can manually modify this number, which is recorded in hkey _local_machine\system\currentcontrolset\control the waittokillservicetimeout in the child key, the unit is milliseconds.

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.