?
Solved

Restarting a service

Posted on 2003-03-18
3
Medium Priority
?
232 Views
Last Modified: 2010-04-04
How can I restart a windows service from within Delphi?

Cheers.

0
Comment
Question by:dvk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 1000 total points
ID: 8160354
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
 
LVL 6

Expert Comment

by:DaFox
ID: 8160392
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
 
LVL 6

Expert Comment

by:DaFox
ID: 8160410
Wow! I took me 5 minutes to copy the source code? Confusing; I guess I have to knock off work now! ;-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question