Delphi Windows Service
Posted : admin On 5/31/2019Delphi 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.
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. Windows service is an application that runs on background. It is usually used to check system updates continuously, to work on system resources. Service applications take requests from client applications, process those requests, and return information to the client applications.
[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.
TURN AROUND Every now and then I get a little bit terrified but then I see the look in your eyes. TURN AROUND, BRIGHT EYES Every now and then I fall apart. And I need you now tonight and I need you more than ever and if you only hold me tight we´ll be holding on forever. TURN AROUND, BRIGHT EYES Every now and then I fall apart. Musica romantica 70 80 90.
- My Windows Service (which I will call the UPDATESERVICE) will run as a Windows Service and create a TCP server to listen for requests.
- The tray application (which I will call TRAYAPP) will use TCP Client to configure/manage the UPDATESERVICE.
- 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.
- Should the TCP Service be created on it's own thread?
- 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?
- 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.
- 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?
- 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.
Delphi Windows Service
1 Answer
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):
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:
Not the answer you're looking for? Browse other questions tagged windowsdelphiservicetcp or ask your own question.
I want a small executable to install my service with the usual
I've tried running a simple shellexecute in a program with elevated rights,
but the service doesn't install. I am not exactly a wiz with ShellEx so I suspect I have gotten it wrong; kindly show me the errors of my ways and possibly supply the correct shellexecute line
1 Answer
The proper way to install a service from code is to call the Service Control Manager (SCM) API. The /install parameter supplied by Delphi service does a 'standard' call to the SCM that usually is not the best one for most services. Today, very few services, for example, should run with LocalSystem privileges because it's too powerful. Fifa 19 commentators.