How can I disable a service via Delphi?

后端 未结 5 1992
离开以前
离开以前 2021-01-01 21:43

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?

5条回答
  •  死守一世寂寞
    2021-01-01 22:22

    This is what i use

    It's just a little wrapper around some Windows API Functions we found useful to handle NT-Services. It allows you to query, start, stop, pause and enable/disable NT-Services on the local or a remote system.

    http://blog.marcduerst.com/post/How-to-use-TServiceManager-to-manage-Windows-services.aspx

    Which lets you write 'nice' delphi code ;)

    procedure DisableService(ServiceName: PChar);
    var SM: TServiceManager;
    begin
      SM:=TServiceManager.Create;
       try
         SM.Connect;
         SM.OpenServiceConnection(ServiceName);
    
       //not working with TServiceManager as is
       //but its easy to fix, see below        
        SM.DisableService;
    
    
       finally
        SM.Free;
       end;
    end;
    

    the DisableService section hasnt been written, but all that is needed is

     procedure TServiceManager.DisableService;
     begin
       ChangeServiceConfig(ServiceHandle, SERVICE_NO_CHANGE,SERVICE_DISABLED,SERVICE_NO_CHANGE, nil, nil, nil, nil, nil, nil, nil);
     end;
    

提交回复
热议问题