DelphiFAQ Home Search:

How to write a Windows NT Service (Delphi 5)


comments6 comments. Current rating: 3 stars (2 votes). Leave comments and/ or rate it.


How can I turn a regular Delphi application into a service for a Windows NT4 server?
The meaning for this is that the program should run when the server is rebooted and even when the server is not logged on. So putting the program in startup folder is not enough.


With Delphi 5, this has become very simple.
Just go to 'File' -> 'New' -> 'Service Application'.
It will create a regular project source with one main 'form', which is derived from TService.

The TService class encapsulates a Windows NT service in an NT service application. A service is accessed via the Service Control Manager. It is usually started during boot time or manually in the control panel 'Services' applet.

The code will look as shown in the example below and consult the online help about TService. You may want to handle the OnExecute event.

  TService1 = class(TService)
    { private declarations }
    function GetServiceController: TServiceController; override;


2006-05-21, 03:27:40 from Egypt  
Can you please tell me what could be wrong if i try to debug the service and i get 'unable to create process : access denied' when i try to attach to the service executable.
2006-09-04, 06:00:51
anonymous from United Kingdom  
Question was not exactly answered. The writer wanted to know how to turn his current application into an NT Service, not how to create one from scratch which is what I believe this response is instructing.

I would also like to know how to do this. Can I just add the SERVICE as opposed to the SERVICE APPLICATION and write some code to convert my existing application to an nt service
2008-01-21, 08:57:37
hey baby its too easy
write this in cmd too create a Windows Services

Creating Services:
Adding Service (Note: Space between binpath= and 'C:\ has to be there.
<path>\sc.exe create 'Service Name' binPath= 'C:\Your Program.exe'
Removing Service
<path>\sc.exe delete 'Service Name'

Controlling Services:
Starting Service
<path>\sc.exe start 'Service Name'
Pausing Service
<path>\sc.exe pause 'Service Name'
Stoping Service
<path>\sc.exe stop 'Service Name
2008-07-15, 07:50:38
anonymous from India  
Didnot work for me...
2008-11-23, 02:32:34
Hi, I craete a service application using delphi7. It works fine and supported all my reqierment but I should handle OnStop or OnDestroy even of my service to do something. When service is stopped from list of services in control panel by user,it works fine, but when a user killed my service by using Task Manager Process by pushing End Process button, nor of these event are fire and I am unable to handle this event. Can aby body help me?.....
2009-09-13, 04:02:42
http://www.tolderlu..ervice.htm - tutorial for making delphi service application



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: