Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7928965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:54:37+00:00 2026-06-03T19:54:37+00:00

Delphi Windows Service Design I’ve never created a windows service but have been reading

  • 0

Delphi Windows Service Design

I’ve never created a windows service but have been reading everything I’ve found. All the articles or examples I’ve run across are very basic in implementation and limited in their scope. Haven’t seen anything that goes beyond this or that address specific scenarios. So, I have all the theory I’m probably going to find, and now I’m ready to dive into this project. I like to layout my ideas and get some feedback on what people think. I’ll describe what I need from the application and how I intend to build it. I’d appreciate comments from anyone who has experience building windows services and any advice they would care to share.

[SCENARIO]
Right now I have an application (I’ll call this UPDATEAPPLICATION) that provides updates to all our other applications. In order to run any of our applications you first have to run this UPDATEAPPLICATION program and pass it a parameter of the desired application. The UPDATEAPPLICATION calls a WebService that returns XML information as to whether the desired application has any updates.

If there is an update, the UPDATEAPPLICATION downloads the update in EXE or ZIP format, and replaces the appropriate files to update the targeted application. Afterwards the UPDATEAPPLICATION does a ShellExecute to start the desired application and then the UPDATEAPPLICATION closes.

It’s a fairly basic process that has worked well over the years. The UPDATEAPPLICATION program is a Delphi application, our other applications are mixed: Delphi, VB6, MS Access, .NET.

[THE PROBLEM]
With the move to Vista and Windows 7, the security has changed dramatically. Because of the nature of the UPDATEAPPLICATION UAC won’t allow the application to run under without Admin acces or UAC completely turned off. We are in the process of upgrading many of our applications to .NET and during this process I’d like the applications as well as the UPDATEAPPLICATION be UAC compliant. From what I’ve researched the only way to do this is by creating the UPDATEAPPLICATION as Windows Service. So, essentially, I need to duplicate the functionality of the UPDATEAPPLICATION into a Windows Service architecture.

[MY DESIGN]
I’m using DelphiXE2. My design will consist of 3 parts to form a single solution: a Windows Service, a small tray Application to interact with the Windows Service, and my redesigned applications that will send messages to the Windows Service.

  1. My Windows Service (which I will call the UPDATESERVICE) will run as a Windows Service and create a TCP server to listen for requests.
  2. The tray application (which I will call TRAYAPP) will use TCP Client to configure/manage the UPDATESERVICE.
  3. My USERAPPLICATION, when started, will send a TCP message to UPDATESERVICE that’s says “THIS APPLICATION” has started.

[UPDATESERVICE]
Will listen for messages. If it receives a message that a USERAPPLICATION has started will it will call the web service to see if there are updates. If there are, the user will be notified to close the application and allow the UPDATESERVICE to update the application. The UPDATESERVICE will download the appropriate files and update the application.

Now that I’ve explained the basics of what I’m trying to do, I can ask my specific questions I need answered. These all have to do with how I should build my Windows Service. I also plan on using OmniThread for my thread management.

When my service starts, I need to create the TCP Server.

  1. Should the TCP Service be created on it’s own thread?
  2. If the TCP Service is it’s own thread, how do I keep the thread alive? Otherwise, I can start the TCP Service but I’m not sure what code I would use within the TCP Service unit to keep the thread running?
  3. What Windows Services event should create the TCP Service? OnExecute? OnStart? OnCreate? After all I’ve read it’s unclear what event should be used.
  4. When the TCP Service receives a message to do something, should the work be executed within TCP Service thread or a new thread spawned off the main UPDATESERVICE? For example:
    • if the TCP Service gets a message to check for an update using HTTP should the TCP Service thread spawn a new thread to do this work
    • Or, should the TCP Service thread send a message to the UPDATESERVICE to spawn a new thread to do this work
    • Does it even matter?
  5. Is it possible to Start/Stop/Register/Unregister a windows service in Delphi Code?

This is all my questions. There probably isn’t a right/wrong answer for this but simply a preference based on experience. If you’ve built services with Delphi you probably have some input that I would find useful. If you have a project that is more robust then a basic “start a service and sleep” and are willing to share it – even if I doesn’t run or just psuedo code – I’m sure this would be invaluable. Thanks for reading my long-winded question. If you can think of a better way to go about this please share your thoughts. I’ll add that several of our applications can be downloaded and run by the general public, so I don’t have complete control over the expected environments. Any advice/comments/help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:54:40+00:00Added an answer on June 3, 2026 at 7:54 pm

    fast answers:

    1&3) Yes. As a rule of thumb do not implement the OnExecute service event. Spawn your own thread from the OnStart service event. The thread can be terminated when you receive the OnStop service event.

    2) you keep your thread alive like this (execute method):

    while not Terminated do
    begin
      // do something
    end;
    

    4) normally each client connection will live on it’s own thread. (ie the TCP server spawns a new thread for each client). Use a well known stack like Indy or ICS. Concerning the HTTP update, you can do this in the spawned client connection thread.

    5) yes, be aware that you need elevated rights to do this.

    I have made quite a few services in my career and I always use the same skeleton for the service application up till now:

    unit u_svc_main;
    
    interface
    
    uses
      // Own units
      u_globals, u_eventlog, u_MyThread, 
      // Third party units
      // Delphi units
      Windows, Messages, Registry, SysUtils, Classes, SvcMgr;
    
    type
      TMyService = class(TService)
        procedure ServiceCreate(Sender: TObject);
        procedure ServiceAfterUninstall(Sender: TService);
        procedure ServiceAfterInstall(Sender: TService);
        procedure ServiceShutdown(Sender: TService);
        procedure ServiceStop(Sender: TService; var Stopped: Boolean);
        procedure ServiceStart(Sender: TService; var Started: Boolean);
      private
        { Private declarations }
        MyThread : TMyThread;
      public
        { Public declarations }
        function GetServiceController: TServiceController; override;
      end;
    
    var MyService : TMyService;
    
    implementation
    
    {$R *.DFM}
    
    procedure ServiceController(CtrlCode: DWord); stdcall;
    begin
      MyService.Controller(CtrlCode);
    end;
    
    function TMyService.GetServiceController: TServiceController;
    begin
      Result := ServiceController;
    end;
    
    procedure TMyService.ServiceCreate(Sender: TObject);
    begin
      DisplayName := 'myservice';
    end;
    
    procedure TMyService.ServiceAfterInstall(Sender: TService);
    var
      Reg        : TRegistry;
      ImagePath  : string;
    begin
      // create needed registry entries after service installation
      Reg := TRegistry.Create;
      try
        Reg.RootKey := HKEY_LOCAL_MACHINE;
        // set service description
        if Reg.OpenKey(STR_REGKEY_SVC,False) then
        begin
          ImagePath := Reg.ReadString(STR_REGVAL_IMAGEPATH);
          Reg.WriteString(STR_REGVAL_DESCRIPTION, STR_INFO_SVC_DESC);
          Reg.CloseKey;
        end;
        // set message resource for eventlog
        if Reg.OpenKey(STR_REGKEY_EVENTMSG, True) then
        begin
          Reg.WriteString(STR_REGVAL_EVENTMESSAGEFILE, ImagePath);
          Reg.WriteInteger(STR_REGVAL_TYPESSUPPORTED, 7);
          Reg.CloseKey;
        end;
        // set installdir
        if ImagePath <> '' then
          if Reg.OpenKey(STR_REGKEY_FULL,True) then
          begin
            Reg.WriteString(STR_REGVAL_INSTALLDIR, ExtractFilePath(ImagePath));
            Reg.CloseKey;
          end;
      finally
        FreeAndNil(Reg);
      end;
    end;
    
    procedure TMyService.ServiceAfterUninstall(Sender: TService);
    var
      Reg : TRegistry;
    begin
      Reg := TRegistry.Create;
      try
        // delete self created registry keys
        Reg.RootKey := HKEY_LOCAL_MACHINE;
        Reg.DeleteKey(STR_REGKEY_EVENTMSG);
      finally
        FreeAndNil(Reg);
      end;
    end;
    
    procedure TMyService.ServiceShutdown(Sender: TService);
    var
      Stopped : boolean;
    begin
      // is called when windows shuts down
      ServiceStop(Self, Stopped);
    end;
    
    procedure TMyService.ServiceStart(Sender: TService; var Started: Boolean);
    begin
      Started := False;
      try
        MyThread := TMyThread.Create;
        MyThread.Resume;
        NTEventLog.Add(Eventlog_Success, STR_INFO_SVC_STARTED);
        Started := True;
      except
        on E : Exception do
        begin
          // add event in eventlog with reason why the service couldn't start
          NTEventLog.Add(Eventlog_Error_Type, Format(STR_INFO_SVC_STARTFAIL, [E.Message]));
        end;
      end;
    end;
    
    procedure TMyService.ServiceStop(Sender: TService; var Stopped: Boolean);
    begin
      try
        Stopped := True; // always stop service, even if we had exceptions, this is to prevent "stuck" service (must reboot then)
        MyThread.Terminate;
        // give MyThread 60 seconds to terminate
        if WaitForSingleObject(MyThread.ThreadEvent, 60000) = WAIT_OBJECT_0 then
        begin
          FreeAndNil(MyThread);
          NTEventLog.Add(Eventlog_Success,STR_INFO_SVC_STOPPED);
        end;
      except
        on E : Exception do
        begin
          // add event in eventlog with reason why the service couldn't stop
          NTEventLog.Add(Eventlog_Error_Type, Format(STR_INFO_SVC_STOPFAIL, [E.Message]));
        end;
      end;
    end;
    
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Windows Service written in Delphi which runs a number of programs.
I have the service name for a windows service in delphi, and I know
I have tested all my systems developed in Delphi (in Windows XP) on Wine
I have a service application created with Delphi, and managed to install it from
I have a Windows service written in Delphi. One of the third-party resources it
I want to develop windows service in Delphi that communicates with an Oracle server
I have a Deplhi based Windows Service that, on installation, parses some command line
Delphi 2010 Windows 7 - 64 bit. I have an app which is reasonably
I'm looking for Delphi sample code to develope a Win32 Windows service which can
I have an existing DataSnap server (Windows service) which is running fine on HTTP,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.