Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!

Developing Windows Services in Windows 11: Best Practices and Tools

Developing Windows Services in Windows 11 Best Practices and Tools

When you develop Windows software you come across the need for some kinds of applications need to be run for 24 hours or, in fact, continually while the computer is running. Usually, the computers in question are network servers or monitoring applications on desktop machines. In these cases, you might think about creating a console application which has a either a minimal amount of interaction or is completely silent, but there are better solutions than this.

Simple console or minimized regular GUI apps can face problems like Windows session termination, reboots and user rights. The way to solve this problem is to develop Windows services. In this tutorial, we will dive into building Windows services using Delphi. Following this, you can use it as a template for your Windows services.

Why would I need to create a Windows service?

There are numerous reasons for creating long-running services such as:

  • Processing CPU-intensive data.
  • Queuing work items in the background.
  • Performing a time-based operation on a schedule
  • Unobtrusive operation of ‘helper’ or utility app functions

Background service processing usually doesn’t involve a user interface (UI), but UIs can be built around them. 

What are the prerequisites for developing Windows services?

Before starting to dive into Service App development, I recommend you check out the latest version of Delphi IDE. With lots of enhancements and new features, the development process goes even smoother. Besides, you can go with the Delphi Community Edition for free and familiarize yourself with Delphi programming languages and its syntax.

How do Windows Service Applications work?

Service applications take requests from client applications, process those requests, and return information to the client applications. A Web, FTP or e-mail server is an example of a service application.

A Windows service application is a Windows application that can run without requiring a user to be logged on. Windows service applications rarely interact with the desktop at all. In this tutorial, we will be creating a Windows service application with Delphi.

When a service application runs it can be set up to use a default set of user rights either as a virtual ‘service user’ or as an actual regular user with rights to access the system. It’s important to understand that these user rights can affect the folders to which your service app has rights to access and also any ‘mapped’ network folders – where someone has created a mapping like a “Z:” drive which points to a network folder will most likely not be available. Where possible you should always use full UNC path names – or use Windows system calls or Delphi’s runtime TPath type functions to obtain the correct locations of special folders such as the location of the %APPDATA% folder and “My Documents” type virtual paths.

Windows service applications can provide a lot of functionality. Look at the list when you start the Services utility tool. These services can be the core of the main GUI applications that you use.

Developing Windows Services in Windows 11 Best Practices and Tools the Windows service manager display

When do you need to write Windows service?

Some time ago, I needed a system monitor utility to monitor the free disk space on our file server. I wrote a utility to check every minute and then write that information to a log file. However, it required a user to be logged on, and when the user logged out, my app just closed. The solution was to recreate the app as a Windows service which would then remain running all the time the computer was powered on – even if the user wasn’t logged in.

Although RAD Studio with Delphi or C++ Builder is optimized more for typical user-facing interactive applications it is more than capable of easily creating service applications.

How to create a Windows service project in Delphi?

To create a new project in Delphi, you will need to have the Delphi development environment installed on your computer. Once you have Delphi up and running, you can begin by creating a new project.

To create a new Windows service project in RAD Studio with Delphi, take the following steps:

  1. Click on the File menu and select New -> Other. This will bring up the New Items dialog box.
  2. From the left-hand side of the dialog box, select Delphi and then choose the Windows Service project type from the Windows category.
  3. Click OK to create the new project.
Developing Windows Services in Windows 11 Best Practices and Tools the dialog to create a Windows service project in RAD Studio

Note the process is similar for those of you using C++ Builder.

Once the new project is created, you will need to set up the project properties and options. To do this, right-click on the project name in the “Project Manager” window and select “Options.” This will bring up the “Project Options” dialog box. In this dialog box, you can set various options for your project, such as the target platform, output directory, and compiler options. Make sure to set these options according to your project requirements before proceeding with the development of your service.

How to implement a Windows service app using Delphi?

After creating a new Windows service project in Delphi and setting up the project properties and options, the next step is to implement the service itself. This involves adding code to the main service unit, which is typically named “Unit1.pas” by default.

To begin, double-click on the “Unit1.pas” file in the “Project Manager” window to open it in the code editor. This will display the unit’s code file, which contains the skeleton code for a Delphi service application.

To add your own code to the unit, you will need to define the service’s behavior by handling various service events. These events include the start, stop, and pause events, which are triggered when the service is started, stopped, or paused, respectively.

To handle these events, you can use Delphi’s built-in service component, the “TService” class. This component provides various methods and properties that you can use to control the service’s behavior, such as the “Start” and “Stop” methods.

For example, you can use the “OnStart” event handler to define the code that should be executed when the service is started. To do this, you can add the following code to the “Unit1.pas” file:

Similarly, you can use the “OnStop” event handler to define the code that should be executed when the service is stopped. To do this, you can add the following code to the “Unit1.pas” file:

In addition to the start and stop events, you may also need to handle the pause event, which is triggered when the service is paused. To do this, you can use the “OnPause” event handler, which is similar to the “OnStart” and “OnStop” event handlers.

For example, you can add the following code to the “Unit1.pas” file to handle the pause event:

In each of these event handlers, you can add your own code to define the service’s behavior when the respective event is triggered. For example, you could use the “Start” and “Stop” methods of the “TService” component to start or stop a timer or a thread, or you could use the “Pause” method to pause the execution of a task.

Once you have added code to the main service unit and handled the service’s events, you can proceed with debugging and testing the service using the Delphi debugger and the Windows Services Manager.

How do we implement Windows service application functionality?

Add Vcl.SvcMgr unit to the Uses clause of the unit.

Change the ancestor of the main form to TService by modifying the declaration of the TForm class as follows:

and here goes the full source code:

The background thread unit here follows:

Now you can build the service. In the Projects window, you can open the context menu and build the service like this:

Developing Windows Services in Windows 11 Best Practices and Tools the build dialog
Build Windows Service Delphi

Testing the Service using the Windows Services Manager

You can test the service using Windows Services Manager. This utility allows you to start, stop, pause and resume a service and view its status and any errors it may have encountered.

To install the service, you should follow these steps:

  • Go to the folder and open in terminal (the executable file should be configured to run as an administrator privileges)
  • Type the service name with “/install” command
Developing Windows Services in Windows 11 Best Practices and Tools installing a Windows service
Go to the folder and open in terminal
Developing Windows Services in Windows 11 Best Practices and Tools the default verbose service installation message
Install Windows Service using Terminal

Once the service is installed, you can start, stop, pause, or resume the service using the Windows Services Manager. To do this, open the Services app in the Administrative Tools folder of the Windows Control Panel. Find the service in the list of services, right-click on it, and select the desired action from the context menu.

Developing Windows Services in Windows 11 Best Practices and Tools a screenshot of the test service app running in the service manager window
Here you can start stop and pause the service

After a few seconds, stop the service from the service manager and check the Logs file.

Developing Windows Services in Windows 11 Best Practices and Tools the service app logging
Logslog file

Get the full project source code here on this repository. Moreover, do not forget to check out other tutorials where you can learn how to create multi-platform native applications.


How to Debug Windows Services?

How to Delete a Windows Service?

Open PowerShell as an administrator and type this command sc delete ServiceName

How do I silently install a Windows service app?

You can avoid the message box appearing by adding “/silent” to the end of the command line like so: myserviceapp.exe /install /silent

Can 32bit services run on 64bit Windows?

Yes, a 32bit service can run normally on a 64bit version of Windows. Note, however, that some network system administrators may have a Windows group policy option set up which will prevent anything other than 64bit services from running. Most don’t do this, but it is possible.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Software Developer | CS(CyberSec) Undergrad at APU Malaysia | Delphi/C++ Builder Enthusiast | Microsoft Learn Student Ambassador | Microsoft Azure Certified


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.