Erro ao configurar Failure Service (Opção de Recuperação do Serviço) - com TService

Delphi

19/09/2014

Tenho um TService, que funciona corretamente, mas ao instalar preciso configurar automaticamente as opções de Recuperação do Serviço para reiniciar em caso de erro após 1 minuto.

Procurando na internet cheguei neste código. Mas não sei o que estou fazendo de errado que está dando erro.

procedure SimpleChangeServiceConfig(AService: string);
var
  SCMHandle: Longword;
  ServiceHandle: Longword;
  actions : LPSC_ACTION;
  sfActions : array of SERVICE_FAILURE_ACTIONSW;
begin
  SetLength(sfActions, 1);
  try
    SCMHandle := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
    if SCMHandle = 0 then
      exit;
    try
      ServiceHandle := OpenService(SCMHandle, PWideChar(AService), SERVICE_ALL_ACCESS);
      if ServiceHandle = 0 then
        RaiseLastOSError;
      try

        actions.&Type := SC_ACTION_RESTART;
        actions.Delay := 6000;


        sfActions[0].dwResetPeriod := INFINITE;           // in seconds, MMC displayes in days
        sfActions[0].cActions := 1;                    // first, second and subsequent failures
        sfActions[0].lpsaActions  := actions;

        sfActions[1].dwResetPeriod := INFINITE;           // in seconds, MMC displayes in days
        sfActions[1].cActions := 2;                    // first, second and subsequent failures
        sfActions[1].lpsaActions  := actions;

        sfActions[2].dwResetPeriod := INFINITE;           // in seconds, MMC displayes in days
        sfActions[2].cActions := 3;                   // first, second and subsequent failures
        sfActions[2].lpsaActions  := actions;

        if not ChangeServiceConfig2(
           ServiceHandle,             // handle to service
           SERVICE_CONFIG_FAILURE_ACTIONS, // change: description
           @sfActions)       // new description
        then
          RaiseLastOSError;
      finally
      if ServiceHandle <> 0 then
        CloseServiceHandle(ServiceHandle);
      end;
    finally
      if SCMHandle <> 0 then
        CloseServiceHandle(SCMHandle);
    end;
    except
      on e: Exception do
      begin
        ShowMessage('Código Erro: '+IntToStr(GetLastError)+' .Error: ' + e.Message);
        exit;
      end;
  end;
end;




Ao utilizar o código recebo o seguinte erro:

Código Erro: 0. Error: Access violation at address 00B905F2 in module '<programa>'. Write of address 00000002.


Debugando consegui verificar que da erro nestas duas linhas:

actions.&Type := SC_ACTION_RESTART;
actions.Delay := 6000;


Gostaria da ajuda de vocês para poder solucionar este erro.
Fm9

Fm9

Curtidas 0
POSTAR