Ver Mensaje Individual
  #1  
Antiguo 08-06-2006
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Reputación: 24
seoane Va por buen camino
Iniciar y parar un servicio

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');
Responder Con Cita