Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable a service via Delphi?

Tags:

service

delphi

I use a routine that can start and stop services via Delphi but I also need to be able to disable them, is it possible?

like image 836
Eder Gusatto Avatar asked Mar 08 '10 13:03

Eder Gusatto


2 Answers

Open the service with OpenService, and then disable it by passing Service_Disabled as the dwStartType parameter for ChangeServiceConfig. Specify a null pointer or Service_No_Change for the rest of the parameters since you're not interested in changing them.

like image 187
Rob Kennedy Avatar answered Sep 28 '22 10:09

Rob Kennedy


You can use file JclSvcCtrl.pas from JEDI Components Library (JCL). I have written a pseudo example that you could use. However, be aware that I didn't test it. But in this way it should work (error checks omitted):

M := TJclSCManager.Create;
M.Refresh(true);  //Not sure if true is needed or not (refresh all services)
For i := 0 to M.ServiceCount -1 do
begin
  S := M.Services[i]; //TJclNtService
  if CompareText(S.ServiceName, 'bla') then
  begin
    S.Stop;
    S.StartType := sstDisabled;   
    S.Commit;
    break;
  end;
end;
like image 34
ChristianWimmer Avatar answered Sep 28 '22 11:09

ChristianWimmer