Código Delphi
[-]
uses WinSvc;
procedure StopService(Nombre: String);
var
ServiceControlManager: SC_HANDLE;
Service: SC_HANDLE;
ServiceStatus: SERVICE_STATUS;
begin
ServiceControlManager:= OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
if ServiceControlManager <> 0 then
begin
Service:= OpenService(ServiceControlManager,PChar(Nombre),SERVICE_ALL_ACCESS);
if Service <> 0 then
begin
if QueryServiceStatus(Service, ServiceStatus) then
begin
if ServiceStatus.dwCurrentState <> SERVICE_STOPPED then
ControlService(Service, SERVICE_CONTROL_STOP, ServiceStatus);
end;
CloseServiceHandle(Service);
end;
CloseServiceHandle(ServiceControlManager);
end;
end;
procedure StartSrv(Nombre: String);
var
ServiceControlManager: SC_HANDLE;
Service: SC_HANDLE;
ServiceStatus: SERVICE_STATUS;
Argv: PChar;
begin
ServiceControlManager:= OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
if ServiceControlManager <> 0 then
begin
Service:= OpenService(ServiceControlManager,PChar(Nombre),SERVICE_ALL_ACCESS);
if Service <> 0 then
begin
if QueryServiceStatus(Service, ServiceStatus) then
begin
if ServiceStatus.dwCurrentState <> SERVICE_RUNNING then
begin
Argv:= nil;
StartService(Service,0,Argv);
end;
end;
CloseServiceHandle(Service);
end;
CloseServiceHandle(ServiceControlManager);
end;
end;
Ejemplo de uso:
Código Delphi
[-]
StartSrv('NombreDelServicio');
StopService('NombreDelServicio');