Utilizamos cookies propias y de terceros. [Más información sobre las cookies].
Política de cookies
Proyecto AjpdSoft

· Inicio
· Buscar
· Contactar
· Cookies
· Descargas
· Foros
· Historia
· Nosotros
· Temas
· Top 10
· Trucos
· Tutoriales
· Wiki
Inciar/Detener servicios de Windows NT XP 2000 - Delphi
Lenguaje de programación Borland Delphi


Para ello, en el USES del formulario o unidad declararemos la unidad WinSvc, Windows, Types:

Función para Iniciar un servicio:

function iniciarServicio (sMachine, sService: String) : Boolean;
var
  schm,
  schs: SC_Handle;
  ss: TServiceStatus;
  psTemp: PChar;
  dwChkP: DWord;
begin
  ss.dwCurrentState := -1;
  schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
  if (schm>0) then
  begin
    schs := OpenService(schm, PChar(sService), SERVICE_START or
      SERVICE_QUERY_STATUS);
    if (schs>0) then
    begin
      psTemp := nil;
      if (StartService(schs, 0, psTemp)) then
        if (QueryServiceStatus(schs, ss)) then
          while (SERVICE_RUNNING<>ss.dwCurrentState) do
          begin
            dwChkP := ss.dwCheckPoint;
            Sleep(ss.dwWaitHint);
            if (not QueryServiceStatus(schs, ss)) then
              Break;
            if (ss.dwCheckPoint < dwChkP) then
              Break;
          end;
      CloseServiceHandle(schs);
    end;
    CloseServiceHandle(schm);
  end;
  Result := SERVICE_RUNNING=ss.dwCurrentState;
end;
Función para detener un servicio:
function detenerServicio(sMachine, sService: String) : Boolean;
var
  schm,
  schs: SC_Handle;
  ss: TServiceStatus;
  dwChkP: DWord;
begin
  schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
  if (schm>0) then
  begin
    schs := OpenService(schm, PChar(sService), SERVICE_STOP or
      SERVICE_QUERY_STATUS);
    if (schs>0) then
    begin
      if (ControlService(schs, SERVICE_CONTROL_STOP, ss)) then
        if (QueryServiceStatus(schs, ss)) then
          while (SERVICE_STOPPED<>ss.dwCurrentState) do
          begin
            dwChkP := ss.dwCheckPoint;
            Sleep(ss.dwWaitHint);
            if (not QueryServiceStatus(schs, ss)) then
              Break;
            if (ss.dwCheckPoint < dwChkP) then
              Break;
          end;
      CloseServiceHandle(schs);
    end;
    CloseServiceHandle(schm);
  end;
  Result := SERVICE_STOPPED=ss.dwCurrentState;
end;
Utilización: colocamos un botón en un formulario y dos cuadros de texto (TEdit), el primero lo llamaremos txtNombrePC y el segundo txtNombreServicio:
procedure TForm1.Button1Click(Sender: TObject);
begin
begin
  if opIniciar.Checked then
  begin
    if (inciarServicio('\\' + txtNombrePC.text,  txtNombreServicio.text)) then
      ShowMessage('El servicio se ha iniciado correctamente.') 
    else
      ShowMessage('El servicio no se ha iniciado.');
  end
  else
  begin
    if (detenerServicio('\\' + txtNombrePC.text,  txtNombreServicio.text)) then
      ShowMessage('El servicio se ha detenido correctamente.') 
    else
      ShowMessage('El servicio no se ha detenido.');
  end;
end;




Publicado el: 2003-09-18

Visita nuestro nuevo sitio web con programas y contenidos actualizados: Proyecto A