| (Inglês) Creating a Windows Service in Delphi - Let’s Create a Service |
|
|
|
|
Our service is going to write the time and date to a text file every minute or so. So, start up Delphi and select File->New->Other. Then select "Service Application."
Add a timer, set it to 60000 (one minute), save the project and unit, and familiarize yourself with the code structure. As you’ve no doubt noticed, the application declaration is different from the standard application. Go to the object inspector and have a look at the various properties of the service application. Let’s go through what each of them mean: AllowPause and AllowStop properties are self explanatory. You need to set these to true. DisplayName specifies the name of the service in the Service Control Manager or Task Manager; enter any name you want in here. The Interactive property specifies whether the Service should be able to communicate with the desktop (for example, with ShowMessage). This will normally depend on what kind of service you are creating. For our service we will normally not need any interaction with the desktop, but for the purposes of this tutorial we will set it to true. The ServiceStartName and Password properties specify the account that the service should be operating as. The ServiceType is set to stWin32 for a Win32 Service, and the StartType property can be stAuto, stBoot, stDisabled, stManual, or stSystem (but for a Win32 service, only Auto, Manual, and System are relevant).
(Page 3 of 5 )
Once you’ve set up the properties, switch to the events tab. Let's go through the event options: Before Install, After Install and Uninstall allow you to create actions that will take place during these events. Start, Stop, Pause, and Continue are all key events that let you interact with the Service from the Service Control Manager or Task Manager. You can start, stop or pause the service in the Service Control Manager. OnExecute is an event that is called when the Service is started. This is where we will write our code skeleton. Now that we’ve covered the properties and events that enable you to create a Service application, let’s start to build our service. Double click onExecute and add the following code: procedure TService1.ServiceExecute(Sender: TService); begin Timer1.Enabled := True; while not Terminated do ServiceThread.ProcessRequests(True);// wait for termination Timer1.Enabled := False; end; All that this procedure does is start up a thread that enables or disables the timer. Next we are going to set the timer and add the code to carry out the simple task, so click on the timer, go to the events tab and double click on onTimer, and add the following code: procedure TService1.Timer1Timer(Sender: TObject); const FileName = 'c:\logdate.txt'; var F: TextFile; begin AssignFile(f,FileName); if FileExists(FileName) then Append(f) else Rewrite(f); writeln(f,DateTimeToStr(Now)); ShowMessage(DateTimeToStr(Now)); CloseFile(f); end; That’s it as far as the code is concerned. Remember to remove the ‘ShowMessage(DateTimeToStr(Now));’ line in the TService1.Timer1Timer procedure, to avoid getting a MessageBox every minute. Now, all we need to do is first, compile the code (to compile – press Ctrl +F9), then install the service and give it a test run!
To install the service: Click Start and select run, then type: PATHTOYOURSERVICE\yourservicename.exe /INSTALL then press OK. If your service is located in a directory called service on the c drive, then type c:\service\yourservicename.exe. The same thing applies when you uninstall. To uninstall: Click on Start and select run, then type: PATHTOYOURSERVICE\yourservicename.exe /UNINSTALL then press OK. To check whether your service is running, open up the TASK Manager and look for the service name. The service I created is called diskmonitor.exe. Here’s an example of my Task Manager showing the simple DiskMonitor.exe service: So, as you can see, Delphi makes it very easy to create a service. Although my example is trivial, you can create very complicated applications. Just remember that the bigger your service application is, the more resources it will consume.
(Page 5 of 5 ) unit diskmon; interface uses type var implementation {$R *.DFM} procedure ServiceController(CtrlCode: DWord); stdcall; function TService1.GetServiceController: TServiceController; procedure TService1.Timer1Timer(Sender: TObject); procedure TService1.ServiceExecute(Sender: TService);
FONTE : devarticle
|
|||






