1

I created a service in Delphi 10.2.3 and it runs well.

To debug it, I added this in the dpr file:

{$ifdef DEBUG}

    try
      // In debug mode the server acts as a console application.
      WriteLn('MyServiceApp DEBUG mode. Press enter to exit.');

      // Create the TService descendant manually.
      EseguiProcessiClient := TEseguiProcessiClient.Create(nil);

      // Simulate service start.
      EseguiProcessiClient.ServiceStart(EseguiProcessiClient, MyDummyBoolean);

      // Keep the console box running (ServerContainer1 code runs in the background)
      ReadLn;

      // On exit, destroy the service object.
      FreeAndNil(EseguiProcessiClient);
    except
      on E: Exception do
      begin
        Writeln(E.ClassName, ': ', E.Message);
        WriteLn('Press enter to exit.');
        ReadLn;
      end;
    end;
  {$else}
    if not Application.DelayInitialize or Application.Installing then
      Application.Initialize;
    Application.CreateForm(TEseguiProcessiClient, EseguiProcessiClient);
  Application.Run;
  {$endif}

This way, I can debug it without problems.

But now, I wanted to change the service name at runtime, so I followed this answer:

https://stackoverflow.com/a/612938/1032291

But now, when I run the service in Release mode, if I keep the original name (EseguiProcessiClient) I have no problems, but if I change the name to something else, the service does not start. It looks like it enters in the ServiceCreate event, but not in the ServiceStart event.

Could anybody give me some help?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
lorife
  • 371
  • 1
  • 12
  • it looks like the problem is that i am not sending any parameters. If i check the service in the registry, ImagePath is just the path of my exe, without any parameter. – lorife Apr 19 '18 at 13:20
  • That is perfectly fine, and normal for Delphi services – Remy Lebeau Apr 19 '18 at 19:08

1 Answers1

1

Ok, i solved it myself. As I said, the problem was the ImagePath. I had to create the service myself using sc create and adding the parameter manually.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
lorife
  • 371
  • 1
  • 12
  • 3
    I would use the service's `AfterInstall` event to update the `ImagePath`, using either [`ChangeServiceConfig()`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms681987.aspx), or updating the `ImagePath` in the Registry directly with `TRegistry`. – Remy Lebeau Apr 19 '18 at 19:09
  • Hello, thank you for your answer. I tried using tregistry but it looks like UAC is blocking the update, that is why i used "sc create". Do you have any advice? – lorife Apr 20 '18 at 07:33
  • 1
    lorife, add a manifest to your service application with requireAdministrator execution level – whosrdaddy Apr 20 '18 at 08:02
  • 1
    @lorife then you are likely not using `TRegistry` correctly. If your service EXE wasn't already being run with admin rights during installation, it couldn't install your service into the SCM to begin with. The `AfterInstall` event would be fired while the EXE is still running, so it would still have access to the service's Registry key. I use `AfterInstall` to make Registry changes in my services, it works fine. – Remy Lebeau Apr 20 '18 at 18:05
  • @whosrdaddy that would force the service to always run with admin rights, even after install. That may not be desirable, depending on what the service actually does when run by the SCM. Only the install process requires admin rights, at least, and that can be done from an elevated console, for instance. – Remy Lebeau Apr 20 '18 at 18:07
  • @RemyLebeau you are right I did not try on production but just when debugging. Thank you for your help – lorife Apr 23 '18 at 07:23