Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros temas > Trucos
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Los mejores trucos

 
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 08-03-2014
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Poder: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
Iniciar y Detener un Servicio en Windows 7 x32 y x64

Club Delphi,

Revisen este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, WinSvc;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
   Machine = '\\MachineName'; // Machine = '\\127.0.0.1';  
   Service = 'Active@ Disk Monitor';

// Inicia un Servicio en Windows 7
function ServiceStart(Machine, Service: String) : Boolean;
var
   schm,
   schs: SC_Handle;
   ss: TServiceStatus;
   psTemp: PChar;
   dwChkP: DWord;

begin

   ss.dwCurrentState := 1;
   schm := OpenSCManager(PChar(Machine), SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT);

   if (schm>0) then
   begin

      schs := OpenService(schm, PChar(Service), 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;

// Detiene un Servicio en Windows 7
function ServiceStop(Machine, Service: String) : Boolean;
var
   schm,
   schs: SC_Handle;
   ss: TServiceStatus;
   dwChkP: DWord;

begin

   schm := OpenSCManager(PChar(Machine), nil, SC_MANAGER_CONNECT);

   if (schm>0) then
   begin

      schs := OpenService(schm, PChar(Service), 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;

procedure TForm1.Button1Click(Sender: TObject);
var
   Msg : String;

begin

   if (ServiceStart(Machine, Service)) then
   begin
      Msg := 'Servicio Iniciado Satisfactoriamente';
      MessageDlg(Msg,mtInformation,[mbOK],0);
   end
   else
   begin
      Msg := 'Servicio No Iniciado Satisfactoriamente';
      MessageDlg(Msg,mtError,[mbOK],0);
   end

end;

procedure TForm1.Button2Click(Sender: TObject);
var
   Msg : String;

begin

   if (ServiceStop(Machine, Service)) then
   begin
      Msg := 'Servicio Finalizado Satisfactoriamente';
      MessageDlg(Msg,mtInformation,[mbOK],0);
   end
   else
   begin
      Msg := 'Servicio No Finalizado Satisfactoriamente';
      MessageDlg(Msg,mtError,[mbOK],0);
   end

end;

end.
El código anterior permite iniciar o detener un Servicio en Windows por medio de las Service Functions APIs. El código fue realizado en Delphi 7 y Delphi 2010 y funciono correctamente en Windows 7 Professional x32 y x64.

Espero sea útil

Nelson.

Última edición por nlsgarcia fecha: 08-03-2014 a las 23:03:37.
Responder Con Cita
 


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Detener un servicio Windows cmfab Varios 4 09-03-2014 18:21:47
Iniciar y detener jobs!! Melissa_12 MS SQL Server 3 20-11-2012 18:27:17
Iniciar servicio de windows jocey Varios 1 19-09-2008 21:29:13
Problema al iniciar servicio con windows. mcalmanovici Varios 2 27-06-2008 17:13:21
Detener\Iniciar proceso de windows jocey Varios 3 19-02-2008 02:41:26


La franja horaria es GMT +2. Ahora son las 06:27:09.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi