• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Restarting a service

How can I restart a windows service from within Delphi?

Cheers.

0
dvk
Asked:
dvk
  • 2
1 Solution
 
geobulCommented:
Hi,

Try this:

unit Unit1;

interface

uses
 Windows, Messages, SysUtils, 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}

function ServiceStart(sMachine, sService: String) : Boolean;
var
  schm,   schs: SC_Handle;
  ss: TServiceStatus;
  psTemp: PChar;
  dwChkP: DWord;
begin
  ss.dwCurrentState := 0;
  schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
  if (schm>0) then  begin
   try
    schs := OpenService(schm, PChar(sService), SERVICE_START or SERVICE_QUERY_STATUS);
    if (schs>0) then begin
     try
      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 <> 0 then  
               Break;
          end;
     finally
      CloseServiceHandle(schs);
     end;
    end;
   finally
     CloseServiceHandle(schm);
   end;
  end;
 Result := SERVICE_RUNNING=ss.dwCurrentState;
end;

function ServiceStop(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
   try
    schs := OpenService(schm, PChar(sService), SERVICE_STOP or SERVICE_QUERY_STATUS);
    if (schs>0) then begin
     try
       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 <> 0 then
              Break;
          end;
     finally
      CloseServiceHandle(schs);
     end;
    end;
   finally
    CloseServiceHandle(schm);
   end;
  end;
  Result := SERVICE_STOPPED=ss.dwCurrentState;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ServiceStart('','Alerter');
//ServiceStart('\\computername','Alerter');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ServiceStop('','Alerter');
end;

end.

Regards, Geo
0
 
DaFoxCommented:
Hi.

You have to stop and start it again.

Markus

-

// don't forget to call CloseServiceHandle on the handle returned by OpenService!
procedure OpenService(const ServiceName: String): SC_HANDLE
var
  ServiceManager: THandle;
begin
  ServiceManager := OpenSCManager(nil, nil,
    SC_MANAGER_ALL_ACCESS);
  try
    if (ServiceManager = 0) then RaiseLastWin32Error;
    Result := WinSvc.OpenService(ServiceManager,
      PChar(ServiceName), SERVICE_ALL_ACCESS);
    if (Result = 0) then RaiseLastWin32Error;
  finally
    CloseServiceHandle(ServiceManager);
  end;
end;

function IsServiceStarted(const ServiceName: String);
var
  ServiceHandle: SC_HANDLE;
  Status: TServiceStatus;
begin
  ServiceHandle := OpenService(ServiceName);
  try
    QueryServiceStatus(ServiceHandle, Status);
    Result := (Status.dwCurrentState = SERVICE_RUNNING);
  finally
    CloseServiceHandle(ServiceHandle);
  end;
end;

procedure StopService(ServiceHandle: SC_HANDLE);
var
  Status: TServiceStatus;
begin
  QueryServiceStatus(ServiceHandle, Status);
  if Status.dwCurrentState = SERVICE_RUNNING then
  begin
    if not ControlService(ServiceHandle, SERVICE_CONTROL_STOP,
      Status) then RaiseLastWin32Error;
  end;
end;

procedure StopService(ServiceHandle: SC_SERVICE);
var
  Status: TServiceStatus;
begin
  QueryServiceStatus(ServiceHandle, Status);
  if Status.dwCurrentState = SERVICE_RUNNING then
  begin
    if not ControlService(FService, SERVICE_CONTROL_STOP,
      Status) then RaiseLastWin32Error;
  end;
end;
0
 
DaFoxCommented:
Wow! I took me 5 minutes to copy the source code? Confusing; I guess I have to knock off work now! ;-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now