Delphi ChangeServiceConfig2 SERVICE_CONFIG_FAILURE_ACTIONS

Example of how to call ChangeServiceConfig2  and set a service to automatically restart on failure.

This example is used within a service, and set the values on installation.

uses
Winapi.WinSvc;

procedure TNTService.ServiceAfterInstall(Sender: TService);
var
schm, schs: SC_Handle;
sfa: SERVICE_FAILURE_ACTIONS;
actions: array [0 .. 2] of SC_ACTION;
begin
schm := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if (schm > 0) then
begin
schs := OpenService(schm, PChar(Name), SERVICE_ALL_ACCESS);
if (schs > 0) then
begin
try
try
sfa.dwResetPeriod := 0;
sfa.lpCommand := Nil;
sfa.lpRebootMsg := Nil;
sfa.cActions := 3;
actions[0].&Type := SC_ACTION_RESTART;
actions[0].Delay := 60000;
actions[1].&Type := SC_ACTION_RESTART;
actions[1].Delay := 120000;
actions[2].&Type := SC_ACTION_RESTART;
actions[2].Delay := 240000;
sfa.lpsaActions := @actions;

ChangeServiceConfig2(schs, SERVICE_CONFIG_FAILURE_ACTIONS, @sfa);
except
on E: Exception do
begin
LogMessage(E.Message, integer(esNormal));
end;
end;
finally
CloseServiceHandle(schs);
end;
end;
CloseServiceHandle(schm);
end;
end;

[whohit]Delphi-ChangeServiceConfig2[/whohit]

Leave a Reply

Your email address will not be published. Required fields are marked *