DelphiFAQ Home Search:
General :: Windows :: Programming :: Windows with Delphi :: Windows API
Windows programming with Delphi

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Only the 40 most recently viewed articles are shown.
You can see the full list here.

Featured Article

Starting and stopping Windows services

The following two functions allow you to start or stop a Windows service - local or on a remote computer:

ServiceStart and ServiceStop.

Use them as shown in the block at the end.

Note:
Thanks to Henk Mulder who mailed me about a correction with the assignment to dwCurrentState. I think this code was written originally with Delphi 3 and probably Borland or Microsoft changed something from a signed integer to an unsigned word.

{ Thanks to Andrea Canu for pointing out a problem in this code (fixed now). }
 
 program test;
 
 uses
   WinSvc;
 
 {sc-----------------------------------------------------------------------
   ServiceStart
 
   sMachine:
      machine name, ie: \\SERVER
      empty = local machine
 
   Purpose:
      start service
      return TRUE if successful
 -----------------------------------------------------------------------sc}
 function ServiceStart(sMachine, sService: String) : Boolean;
 var
   schm,
   schs: SC_Handle;
   ss: TServiceStatus;
   psTemp: PChar;
   dwChkP: DWord;
 begin
   ss.dwCurrentState := 1; // originally -1, corrected by Henk Mulder
   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;
 
 
 {sc-----------------------------------------------------------------------
   ServiceStop
 
   Purpose:
    stop a service, parameters as in ServiceStart
 -----------------------------------------------------------------------sc}
 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
     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;
 
 
 begin
   if (ServiceStart('\\ComputerName', 'alerter')) then
   begin
 
     // ..
   end;
   if (ServiceStop('', 'alerter')) then
   begin
 
     // ..
   end;
 end.
You don't like the formatting? Check out SourceCoder then!