1

Is it possible to get the path of the Windows service EXE

  1. at installation time, to modify the service display name, and
  2. at runtime, to use as a parameter?

GetCurrentDir etc. of course gives system32 folder.

The problem I would like to solve is that I want multiple instances of it running, with a parameter passed to it which will determine name and behaviour. My proposed solution was to use a folder name, e.g.:

C:\MyProgramDirectory\bin\1\MyService.exe
C:\MyProgramDirectory\bin\2\MyService.exe
C:\MyProgramDirectory\bin\3\MyService.exe

I want the service to

  • pick up this directory name at install, so its service/display name would be "MyServiceX":
    procedure TMyService.ServiceLoadInfo(Sender: TObject);
      begin
    
        name        := 'MyServiceX'; // Get this by extracting X from EXE directory
        DisplayName := 'MyServiceX'; // Get this by extracting X from EXE directory
      end;
    
    procedure TMyService.ServiceBeforeInstall(Sender: TService);
      begin
        ServiceLoadInfo(Sender);
        inherited;
      end;
    
    procedure TMyService.ServiceBeforeUninstall(Sender: TService);
      begin
        ServiceLoadInfo(Sender);
        inherited;
      end;
    
  • pick up this directory name at runtime to use as a parameter to determine behaviour
AmigoJack
  • 5,234
  • 1
  • 15
  • 31
Joao
  • 23
  • 4

1 Answers1

1

The service can get its own .exe file path by calling ParamStr(0).

However, a cleaner solution that doesn't require having multiple copies of the .exe is to pass extra command-line parameters when (un)installing and running the service. The service can look for those parameters to update its Name/DisplayName as needed. See this question for how to do that:

Is it possible to install multiple instances of the same delphi service application?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you for ParamStr(0), I thought it was just another call to GetCurrentDir. Regarding only one .exe - yes much cleaner, however this would mean that all instances of the service would have to be stopped for an update to the exe? I can imagine scenarios where I might need these services to be running a different version to each other – Joao Apr 28 '23 at 21:40