Windows98NT service

xiaoxiao2021-03-06  18

In the control panel, you will find an icon with a service, open it, you will see the list of all Win32 services. There you can start, stop, suspend, and continue a service. After you press the start button, a dialog box will appear, you can modify the startup operation and the default account used by the service. A service can automatically run when the system is started, or it can be completely disabled. Or set to manually execute. When manual, the user can set the start-up parameters. To modify the items in the service, you need to log in as an administrator or superuser. Windows NT comes with some pre-installed tasks to handle operations such as network messenger services or use the "At" command timing execution, and distributed RPC naming. When you create your own service, you have to perform a separate installation step to insert the information of the service into the list of service management tools, including the name of the new service, the name of the execution file, and the type of startup, etc. Both will be written to the registry, so when the machine is started next time, the SCM will receive information about the new service.

Creating a new service execution service is also an EXE file, but it must meet certain specific specifications so that you can configure correctly with the SCM. Microsoft is well designed in the process of function calls, you must follow these processes, otherwise your service can't work. Specific provisions are listed below. You can find the following functions in the Win32 programmer's reference guide, these information in the SDK Win32 online help or Visual C :. The service code must have a general main or winmain function. This function should immediately call the StartServiceCrtLDispatcher function. By calling this function, you can get the SCM to get a pointer to the servicemain function, you can call it when SCM is to start the service. When the SCM is to start the service, the serviceMain function will be called. For example, if the administrator presses the button in the Service Manager, SCM will execute the servicemain function in a separate thread. ServiceMain should call the RegisterServiceCtrlHandler function so that you can register a Handler function so that SCM controls the service. The name of the Handler function can be arbitrary, but it will be listed in the document under Handler. The RegisterServiceCtrlHandler function returns a handle that can be performed by this handle when the service needs to send statly information to the SCM. The .serviceMain function must also start the thread that actually works actually in this service. The servicemain function should not be returned before the service is stopped. When it returns, the service has stopped. The .Handler function contains a Switch statement to analyze the request from SCM. By default, SCM can send any of the following control constants: SERVICE_CONTROL_STOP - To service stops SERVICE_CONTROL_PAUSE - to serve the suspended SERVICE_CONTROL_CONTINUE - To service continues SERVICE_CONTROL_INTERROGATE - to be served immediately report its status SERVICE_CONTROL_SHUTDOWN - told the service will shut down can also create custom The constant (value between 128 and 255) and sent to the service via SCM. If you created EXE includes the above, servicemain, and handler functions, as well as thread functions that perform service own tasks, then your service program is complete. The following graph summarizes these different functions and SCM interactions: ************** Figure 1 ************** ****** In this article, there are several programs in this article, where lists show us a simple service. This service only issues a "beep" ring. In the default, it rang every two seconds. You can modify the interval of the vocal by starting parameters. This service is quite complete, it can properly respond to each control signal from the SCM. Therefore, this program can serve as a good template you create yourself. The main function registers the ServiceMain function by calling StartServiceCtrldispatcher. The registered operation uses an array of service_table_entry structures. In this example, the program only contains a service, so there will be only one item in the table. However, for an EXE file, you can create several tasks so that there will be several items in the table to identify different servicemain functions.

Before calling StartServiceCtrldispatcher, you can put the initialized code in the main function, but these code must be completed within 30 seconds, otherwise the SCM will think that certain places have error and terminate service. The servicemain function will be called when the service is automatically or manually started. The servicemain function will contain the following steps: 1. It immediately calls the registerServiceCtrlHandler to register the Handler function, which is the Handler function 2 of the service as the SCM. Then it will call the sendstatustoscm function to report the current process to the SCM. The fourth parameter is a "Click Count" value, and its value increases when the program is updated each time. SCMs and other programs can know the process of initialization according to the value of Click Count. The final parameter is "Wait Hint", which is used to tell SCM before the next update of Click Count, it needs to wait for the time (in milliseconds). 3. ServiceMain then creates a thing, the event is used at the bottom of the function, allowing it to run until the SCM issues a STOP request. 4. Next, ServiceMain checks the start-up parameters. The parameters can be transferred through the startup parameters in the service management tool when the user is manually started. These parameters enters the servicemain function in an argument in the form of an Argv. 5. If your service needs to handle other initialization tasks, they should be placed in this step and before invoing INITSERVICE. 6. The servicemain function then calls the INITSERVICE function, which will start the thread and do the truly work of the service. If the call is successful, SverviceMain will notify the SCM service has been successfully started. 7. ServiceMain will call WaitForsingleObject to wait for the TerminateEvent event object to be set. This object is set through the Handler function, once it is set, ServiceMain will call the termination function to do the clear work, then return and stop the service. You can see from above, there is not much flexible place in this function. In addition to step 5, you must press the task mentioned above, otherwise the service will not start correctly. The termination function clears all open handles and sends a state information to the SCM, telling it that the service is now stopped. When SCM is paused, continuing, queries, or stopping the service, it will call the Handler function. To stop the service, Handler sets TerminateEvent, which will cause servicemain to terminate and return in a stand-alone thread. Once ServiceMain returns, the service stops. The SendStatustoscm function is responsible for sending the current state of the service to the SCM. When you need to start the service thread, ServiceMain calls the INITSERVICE function. This function calls CreateTHRead to create a new thread for the service. The ServiceThread function contains the work that the service really wants. In this example, the thread contains an unlimited loop to emit a sound in a predefined time interval. When you create your own service, you can put any code in this thread, call the Win32 function or your own function. Installing and Removing Services In order to use the sound service mentioned above, you must install it. Installation allows SCM to know this service and let SCM add it to the list of service in the control panel. The code for forms two shows how to install a service.

转载请注明原文地址:https://www.9cbs.com/read-41774.html

New Post(0)