Write Win2000 Server with Delphi

xiaoxiao2021-03-06  184

First, Win2000 Service Introduction

Service Application is a background program running in WinNT, which may contain several services (Service applications), each service is one of the threads (the service can also create multiple children) Thread). With services, applications can get special privileges, and will not be directly ended by the user through the Win2000 task manager, so service is often used to implement some special goals.

With the service management tool in the Win2000 Control Panel, we can set the characteristics of the service:

(1) Service name; (2) Display name; (3) Description; (4) Start type; (5) dependency;

Where the service name is identified to the service.

With Win2000's C: /Winnt/System32/Services.exe program as an example, the EXE file corresponds to a service application, which is a visible entity of the service program; service, such as Alerter, DHCP (DHCP) Client, Messenger, etc. When we end a service, other services in the service application in the service are not terminated.

In Delphi, Borland's engineers provide us with TServiceApplication, TService, TServiceThread, etc., encapsulated a lot of details, simplifying the development of the service.

?

Second, TSERVICEAPPLICATION

?????? In Delphi, class TServiceApplication corresponds to the above serviceApplication. With Delphi's development environment, we create a service application project, and create a class inherited from TSERVICE. Application object in the project file is a TServiceApplication instance. Each TServiceApplication contains several TSERVICE objects, just corresponds to the quantity relationship between the above-mentioned service programs and services.

By reading the definition of TServiceApplication and TService classes, you can know that tServiceApplication is inherited from the Tcomponent class, and TService comes from class TDataModule, and Application object is responsible for the Create and Destroy of each TService object. Track the following code

Application.createform (TService1, Service1);

You can find that Owner of the created TService object is an Application object; Owner in VCL Framework is always responsible for DESTROY Each Component object (VCL's Tcomponent class uses Composite mode), so TServiceApplication also will DESTROY TSERVICE object.

The following is followed by the code of TServiceApplication.Run, and you can find that TServiceApplication first parses the running parameters and implements the service's install and uninstall. Then, initialize a ServiceStartTable array, which contains the service name and running port of each service object; finally create a TServiceStartTHRead object, the object is a thread object, calling the API: StartServiceCtrldispatcher to start the Several services specified in ServiceStAble; The ServiceApplication main thread will continue to loop, process messages, such as receiving requests to stop / suspend a service. ?

Third, TSERVICE

TSERVICE class inherits self-class TDataModule, which means we can join a large number of VCL controls to achieve rich features. In addition, we can also handle ONSTART, ONPAUSE, ONSTOP, ONCONTINUE, OnCREATE, ONSHUTDOWN, etc. It will be described in that: OnStop indicates that the service is stopped; but OnShutDown indicates that the serviceApplication stops running, which means that other services are also terminated; the meaning of both is different. As mentioned earlier, ServiceApplication starts all services by calling StartServiceCtrldispatcher. StartServiceCtrldispatcher launches the TService entry, which is TService.Main. TSERVICE.MAIN First registered the service and then calls TService.dostart. TSERVICE.DOSTART Create an internal TServiceThread member object, this is a thread object; TSERVICETHREAD.EXECUTE can beware that when we deal with TService1. Onexecute, TService will delegate all requests to the TServiceThread member object processing, the object A default manner processes all requests. The TService. ServiceExecute is the main content of TService. One service is to be running normally, in addition to the target you want to pay attention to (such as listening to a port, executing a task, etc.), it is necessary to respond to external commands / requests: such as termination, suspend, restore the service. Therefore, you can consider creating a dedicated thread to complete the task, and processing the outside command / request in ServiceExecute. So the code is as follows: ?????? While Not Terminated Do Begin

???????????? servicethread.processRequests (FALSE);

?????? end;

Of course, some tasks can also be handled in Onexecute, such as listening to a port, but this often causes the service to respond promptly responding to STOP / PAUSE. When Onexecute is executed, the service actually completed the task to end (TERMINATE). • Reference: MSDN, Delphi VCL source code, Delphi Help document

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

New Post(0)