Jump to content
microtronx

Best components for creating windows service apps

Recommended Posts

Hi,

what components, classes do you use to create extended service apps for windows i.e. with forms and timers etc or have one exe for service and gui? Only tService or any other commercial components?

Share this post


Link to post

I wrote all of my Windows service applications based on Delphi's TService and I did not find any showstoppers which made me want to change it. It's robust, small and gets the job done quite well.

A small tip though. Since service applications are hard to debug, I started to implement the whole business logic as a simple Class which is being created and destroyed based on what is happening with the service.

 

Change the .DPR like this:

 If Not FindCmdLineSwitch('console', True) Then Begin
                                                If Not Application.DelayInitialize Or Application.Installing Then Application.Initialize;
                                                Application.CreateForm(TService1, Service1);
                                                Application.Run;
                                                End
   Else StartWithConsole(TMyServiceClass);

...where StartWithConsole is just a small procedure which creates a console window with AllocConsole, creates the service class, redirects the logging output to StdOut and handles the console handlers (like Ctrl-C, etc).

This way there's only one executable, which can be started as a command line application for easy debugging and as a Windows Service as well.

 

P.s.: don't forget about creating a custom message pump, some components rely on Windows Messages to work properly!

Share this post


Link to post
3 hours ago, aehimself said:

I wrote all of my Windows service applications based on Delphi's TService and I did not find any showstoppers which made me want to change it

My experience too.

Share this post


Link to post

Thanks for your tips, will think about creating a class for all logic.

 

Has someone used SvCom in the past; are the components still supported?

Edited by microtronx

Share this post


Link to post

I've used SvCom from Aldyn Software for 15 years.  It is commercial software, still being updated for new Delphi versions, comes with source code. 

 

The major benefit is it allows applications to be created as dual purpose GUI and service applications, which means you can develop and test under the Delphi debugger with a minimal GUI window usually just a log window and start/stop buttons.  Some of my applications can be optionally installed as a service, but have a full GUI as well, and include their own code to install as a service.

 

Angus

 

Share this post


Link to post
1 minute ago, Angus Robertson said:

I've used SvCom from Aldyn Software for 15 years.  It is commercial software, still being updated for new Delphi versions, comes with source code. 

 

The major benefit is it allows applications to be created as dual purpose GUI and service applications, which means you can develop and test under the Delphi debugger with a minimal GUI window usually just a log window and start/stop buttons.  Some of my applications can be optionally installed as a service, but have a full GUI as well, and include their own code to install as a service.

 

Angus

 

 

That means it is still supported? I have send two request to their mails but have not received any answers so i thinked that they not "online" anymore ...

Share this post


Link to post

I've never need help from Aldyn support, the software just works although I've fixed a couple of minor things myself.  They have a version for Rio which means it's been updated in the last year or so, Developers do go on holiday, sometimes you have to be patient.

 

Angus

 

Share this post


Link to post
49 minutes ago, microtronx said:

Has someone used SvCom in the past; are the components still supported?

I personally never used SvCom but even if it's unsupported I would not worry about it. The way Windows handles services (service control messages) did not and could not change as it would break backwards compatibility (you wouldn't be able to install / start a service on Windows 2003, only on 2019 - never seen that, unless it was an API dependency in the business logic). A component to create a service application mostly contains a code to handle these SCMs, which are the same since Windows 2000.

Share this post


Link to post

Although the Windows service APIs rarely change, there are new ones periodically to improve interaction with GUIs and taskbar notifications, although I don't need any of that stuff.  Not looked for a long time, but TService used to use old APIs and was never updated. 

 

Main problem with long term support is RTL changes in new versions of Delphi, not often, but breaks new compilers. Usually a simple change provided you have the source code.

 

Angus

Share this post


Link to post
1 minute ago, Angus Robertson said:

Not looked for a long time, but TService used to use old APIs and was never updated. 

 

Main problem with long term support is RTL changes in new versions of Delphi, not often, but breaks new compilers.

Agreed, this is a possibility in most cases; I just don't see a reasonable chance when it comes to Windows Services.

Even if Embarcadero would decide to re-write the logic, they'd probably keep the event handlers and the class name so it stays backwards compatible... I hope 🙂 if not, most of us will be in a big trouble 🙂

Share this post


Link to post

We used SvCom for years but threw it away as we did not need any of those fancy features - using a simple TService with some additional code to let the service run as normal application that simply shows a small form with a start and stop button (useful for debugging)

Share this post


Link to post
On 9/23/2019 at 1:58 AM, microtronx said:

what components, classes do you use to create extended service apps for windows i.e. with forms and timers etc or have one exe for service and gui? Only tService or any other commercial components?

Most of my services are written using the standard TService, with separate apps for their UIs.  But I do have one service that runs in both service and app mode, and I use SvCom for that project.

Share this post


Link to post
8 hours ago, Angus Robertson said:

Not looked for a long time, but TService used to use old APIs and was never updated. 

That is still the case.  And that is my only complaint I have about TService - lack of access to newer SCM features introduced since Win2K (and I have complained about it for years, and posted several QC tickets about it, which are now lost and I don't care to repost them).  Most things I can just work around in my own code by calling various APIs directly.  But TService's use of an old Handler() callback instead of a newer HandlerEx() callback is still an issue for some features to work correctly.

  • Like 1

Share this post


Link to post

The late Arno Garrels wrote an updated component, or rather he patches TService with updates to avoid copyright issues. 

Quote

DDService is an enhanced Windows NT service application framework for Delphi and C++ Builder based on the original VCL service framework. In addition to it it also encapsulates new Windows NT service APIs introduced since Windows 2000. DDService is freeware with source and currently supports Delphi 5, 7 and 2006-XE5 as well as C++ Builder 2006-XE5.

I've used it in a couple of applications but not managed to make a dual GUI/Service application work properly (it was not designed for that) so still use SvCom.  Keep meaning to update it for newer Delphi versions.

 

Angus

Share this post


Link to post
1 hour ago, Remy Lebeau said:

[...] lack of access to newer SCM features introduced since Win2K [...]

I never had to do anything fancy - reply to stop, start, pause, resume and shutdown events. Just out of my own curiosity and education - what are these features?

Share this post


Link to post
3 hours ago, aehimself said:

Just out of my own curiosity and education - what are these features?

A Handler callback (what TService uses) can receive only the following control codes from the SCM:

 

SERVICE_CONTROL_CONTINUE
SERVICE_CONTROL_INTERROGATE
SERVICE_CONTROL_NETBINDADD
SERVICE_CONTROL_NETBINDDISABLE
SERVICE_CONTROL_NETBINDENABLE
SERVICE_CONTROL_NETBINDREMOVE
SERVICE_CONTROL_PARAMCHANGE
SERVICE_CONTROL_PAUSE
SERVICE_CONTROL_PRESHUTDOWN
SERVICE_CONTROL_SHUTDOWN
SERVICE_CONTROL_STOP

User-defined codes

 

A HandlerEx callback can receive all of the above controls codes, as well as the following additional control codes:

 

SERVICE_CONTROL_DEVICEEVENT
SERVICE_CONTROL_HARDWAREPROFILECHANGE
SERVICE_CONTROL_POWEREVENT
SERVICE_CONTROL_SESSIONCHANGE
SERVICE_CONTROL_TIMECHANGE
SERVICE_CONTROL_TRIGGEREVENT
SERVICE_CONTROL_USERMODEREBOOT
 

In addition, when a service registers its HandlerEx callback, a user-defined context value can be passed to the HandlerEx whenever it is called by the SCM.  That option is not available when registering a Handler callback.

 

Also, there are newer APIs for apps to interact with the SCM, like ChangeServiceConfig2(), ControlServiceEx(), EnumServicesStatusEx(), QueryServiceConfig2(), QueryServiceStatusEx(), etc.

  • Like 2
  • Thanks 2

Share this post


Link to post

POWEREVENT, TIMECHANGE 🙂 I already had to make workarounds for these. Good to know, thank you very much!

Share this post


Link to post

We do something similar to what Stefan does.

 

We have a number of services, all implemented around a server core class which in itself is unaware of if it is running as a service or as an application.

We wrap TService using a small "helper" class to set up the appropriate server core descendant and hook the svc events that we care about.

The test application simply instantiates the same core object and hooks in a few handlers that f.x. will show certain config info and log outputs in a list view.

 

Using that approach, writing and testing service code has pretty much become trivial.

 

We did run into one snag the other day, when we moved a file share URL from a server drive share to a DFS share.  Turns out that Local System accounts cannot access DFS file shares, so you either have to give the machine access to the DFS through AD, or run the service under an AD user account that can be given access. We chose the latter.

Share this post


Link to post

That said, based on Remy's post - TService could do with a sibling: TServiceEx that handles the new control codes.

Is there a QP I can vote for?

Share this post


Link to post

DDService adds the following according to the readme:

 

Quote

 - Win 2000 FailureOptions and FailureActions,
   Service Description.
   HandlerEx extended service controls:
   ParamChange, NetBindChange.
   Optional device events as well as power events.
   Assigning one of these events creates a hidden  window in the context of the service thread.
   Creation of the service window can be enforced by  setting option eoForceServiceThreadWindow.
 - Win XP SessionChange service control
 - Win Vista PreShutdown service control and  NonCrashFailures flag, Service SID Info, Required Privileges and StartType AutoDelayed.
 - Optional console control handler
 - Property ServiceName.
 - Includes fixes of QC #68050 and QC #37706. 

I'll put it in my SVN repository next week, Arno would be pleased, he put a lot of effort into DDService.

 

I handle power, time and device events in my services using the normal Windows messages, wrote a hardware component that does it all. 

 

Angus

 

  • Like 3

Share this post


Link to post

I am using DDService successfully in Delphi 10.3; my patched files have a 2013 date and do all that is currently needed in our service applications.  I look forward to checking them against your versions in due course.  I have also used SVcom in the past but found DDService easier to implement.

Share this post


Link to post

 

2 hours ago, timfrost said:

I am using DDService successfully in Delphi 10.3;

The latest files in my DDService version are 20 April 2014, mostly XE6 units.  I'm not sure how the DIFF files that patch a copy of the installed SvcMgr.pas file are created, or whether the zip includes the tools to create new DIFF files.  Suspect the patched XE6 file could be distributed and used in more recent versions without a problem, which might be what you doing.

 

Angus

Share this post


Link to post

Yes, I no longer remember when I applied the patches; for each compiler change I just update the compiler defines, build and test. Occasionally I may have had to tweak something.

Share this post


Link to post
12 hours ago, Lars Fosdal said:

That said, based on Remy's post - TService could do with a sibling: TServiceEx that handles the new control codes.

Is there a QP I can vote for?

There used to be several in Quality Central, but they were lost during the migration to Quality Portal and the shutdown of Quality Central.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×