DelphiFAQ Home Search:

Starting and stopping Windows services

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

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!

Comments:

2006-11-13, 23:56:05
roberto@elkgrove.us from United States  
How to interact with my application and my service? I know there are file mapping amd com obj but i was wondering if there is a more direct way like passing an handle back and forward between the 2 applications. Thanks.

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.