Windows service does not start automatically

Asked

Viewed 858 times

4

I just implemented a Windows service I just can’t get it to turn automatically when the windows is started. I don’t know if it was some thing in the programming or some configuration of the windows. I also found nothing on this subject made with Delphi here.

Code in service

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  SvcAutoPub.Controller(CtrlCode);
end;

function TSvcAutoPub.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TSvcAutoPub.ServiceExecute(Sender: TService);
begin
  Timer.Enabled := True;
  while not Terminated do
    ServiceThread.ProcessRequests(True); // fica em looping até alguém fechar o serviço

  Timer.Enabled := False;
end;

procedure TSvcAutoPub.ServiceStart(Sender: TService; var Started: Boolean);
begin
  if Connection.Connected = False then
  begin
    try
      Connection.Connected := True;
    finally
      tbParametos.Open;
      tbProcessos.Open;
      tbPublicacoes.Open;
      tbPonteiros.Open;
      tbAndamentos.Open;
      tbClientes.Open;
    end;
  end;
end;

The Properties of the Service: inserir a descrição da imagem aqui If it is some windows configuration there I no longer where goes.

  • 1

    You registered your service in the OS? Installutil.exe

  • 1

    yes it works good only no auto start

  • 2

    It appears in the service list and the settings shows the option to start automatically?

  • Sure if I had to install the service

  • I used this command to install the service "PATH_COMPLETO_DO_SERVION /INSTALL".

  • I haven’t forgotten about you, I’m reading the documentation. But I’m finding difficulties and finding quality material for Delphi.

  • 1

    I already have 2 weeks looking and nothing...

  • I found nothing about it at the moment. I’ll keep looking if I find anything I leave an answer.

  • Blz, thank you very much!

  • It starts manually if Voce is going to start it on the list if services?

  • Yes. When I open the list of services it is already there only stopped I have to press to start.

  • As soon as possible I will be bringing an answer that will possibly solve your problem. At the moment I am far from my computer.

  • @Mendonça the solution that I presented does not solve? Here it is perfectly running automatically.

  • I don’t know what it is... I did the installation as @Jefferson Rudolf spoke and did as you said it runs when windows opens only it starts with the status stopped

  • But you started it a first time? In my answer I put: de um start nele faça seus testes se o serviço esta executando

  • it performs yes only it comes with statos stopped. are you understanding? sabe la na services appears initializing, stopping and restarting mine as when windows start it starts the service stopped even there in Delphi i setting for automatic. I have to go into service and press start for it to work.

Show 11 more comments

3 answers

1

Add to the event OnCreate the instruction: Self.StartType := stBoot;

In this way the service will be installed and set as "Automatic", from a start in it make its tests if the service is running. I restarted Windows and make sure everything goes as planned.

Remembering that this property can be set in Object Inspector, its default is stAuto but does not initialize the service automatically.

1

You can include your service on automatic Windows startup as follows:

Enter the code in the event AfterInstall

procedure TService.ServiceAfterInstall(Sender: TService);
var
  oRegEdit : TRegistry;
begin
  oRegEdit := TRegistry.Create(KEY_READ or KEY_WRITE);
  try
    oRegEdit.RootKey := HKEY_LOCAL_MACHINE;

    if oRegEdit.OpenKey('\SYSTEM\CurrentControlSet\Services\[ServiceName]',False) then
    begin
      oRegEdit.WriteString('Description','Serviço de sincronização do aplicativo');
      oRegEdit.CloseKey;
    end;
  finally
    FreeAndNil(oRegEdit);
  end;
end;

Replace the [ServiceName] by the name of your service so that Windows can boot it. So after the service is installed, it will be included on automatic startup.

Note: Leave the property StartType as stAuto

1

I see you already use a command to install, but try to use the following command by running a minimized . bat.

SC CREATE NOME_DO_SERVICO binpath= "NOME_DO_EXECUTAVEL.exe" start= auto displayname= "NOME_DE_EXIBICAO"

on the part of start=auto is where it will stand as auto boot.

  • I will test and put the results.

  • @Edumendonça worked on the service?

  • in parts. It the way you told me it starts with Windows only it is coming stopped.

  • 1

    @Edumendonça On your Tservice object, make sure the property is as Starttype=stAuto

  • The property is set to stAuto.

  • You have to send a print of the properties that are set in its object

  • In the Interactive property Arrow to True

  • Service startup type is as automatic?

  • It is even I have already tested and at the moment it starts with Windows only starts with Status Stopped

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.