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.
[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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With