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.

What can I try next?

2
  • 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. Commented Apr 19, 2018 at 13:20
  • That is perfectly fine, and normal for Delphi services Commented Apr 19, 2018 at 19:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I would use the service's AfterInstall event to update the ImagePath, using either ChangeServiceConfig(), or updating the ImagePath in the Registry directly with TRegistry.
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, add a manifest to your service application with requireAdministrator execution level
@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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.