Install multiple instances of the same Delphi application service

In this article you will find an example on How to install multiple instances of the same Delphi application service under Windows. This topic was raised a few years ago in StackOverflow and I have decided to give more details about how to use multiple instances of the same service.
The service needs to be unique in name and the best way to sort this out is using a parameter to describe the service in a unique way. The service itself will use this parameter to compose its name even though it is using the same  executable. In this example I will identify the services when installing the service using the command line with the following instruction: "myService /install Param1". Param1 will be used to compose the internal service name as myServiceParam1. If we want to use another instance, just install a second service with a different param name as: myService /install Param2.

To achieve this is pretty simple, just create the service using your Delphi XE and then create the methods ServiceBeforeUninstall and ServiceCreate:


Notice the System.ParamStr(2) that will use the second parameter which was input from the command line. The first parameter is the /install command needed to install the service.

After the execution of the following commands:

myService /install Param1
myService /install Param2

You will see two installed services using the same executable:


To uninstall them, use the opposite command:

myService /uninstall Param1
myService /uninstall Param2

But this is not ending here. Now the services are installed, but neither the service name is set nor the description. To achieve this, we need to use the ChangeServiceConfig functions from WinSvc - Service Control Manager unit.

Have a look at the complete source code:

This solution is using the example provided by koochangmin on his Delphi blog. If you have a closer look at the ServiceAfterInstall method, you will find the composition of the name and description which will be used to populate the values in the service:


That is the way the service needs to be created in order to let the application use the parameters in runtime.

If you have any problem uninstalling the services, just use the SC command to delete the service:

sc delete ServiceExampleParam1

Related links:

Comments

  1. Hello,

    I created the project. I got do install the service, but when i try to start, it's not works. Do you know why ?

    ReplyDelete
    Replies
    1. What's the error you get? Did you install the service?
      Add a loop in your main code and then add a breakpoint there. Start the service and attach your service to your delphi IDE and debug through it.

      Delete
    2. The problem is in ParamStr(2). While installing service additional parameter is second parameter, but when running it's first one.
      if FindCmdLineSwitch('install', True) or FindCmdLineSwitch('uninstall', True) then
      FDescription := ParamStr(2)
      else
      FDescription := ParamStr(1);

      Delete
  2. This really helped me out. Thanks for posting.

    ReplyDelete

Post a Comment

Popular Posts