rahulinfo.com: Some Helps, some Tips, some Musing

Managing Windows Services using a Delphi Application

Windows services and knowing the status of a particular service are a often repeated process during application development as well as smoothing running of an application that require these services.
This code snippet is an example of how to get the status of a particular service in window in your program so that you may use the status for efficient and successful run of your program.
We will be using a Win 32 api QueryServiceStatus() in delphi coding to get the status in the following way .


if( ServiceRunning( '\ComputerName','\servicename' ) )then
begin
  // Here you may mention the code if this particlar service is running.
end;

if( ServiceRunning(  '',  '\servicename' ) )then
begin
  // If machine is a local computer no need to mention computer name

end;

if( ServiceStopped( '', '\servicename' ) )then
begin
  // Put here code if the particular service is not running
end;

In order to use the above code we need to define the functions like servicerunning and service stopped.

Here is how these functions are defined.

We start by including winsvc which is the delphi interface to interact with windows services.

uses WinSvc;

function WhatisServiceStatus( sMachine,  sService : string ) : DWord;
//Here sMachine is the name of your machine in a network and blank for a standalone machine.
var
  schm,  schs   : SC_Handle; //service handles
  ss     : TServiceStatus;     //service status
  dwStat : DWord;           //current status
begin
  dwStat := -1;
  schm := OpenSCManager( PChar(sMachine), Nil,   SC_MANAGER_CONNECT);

  if(schm > 0)then  //if connection to service manager is successful.
  begin
    schs := OpenService( schm, PChar(sService),      SERVICE_QUERY_STATUS);
  // we open a handle to the desired service and ask for the staus.

    if(schs > 0)then      //if status is returned
    begin

      if(QueryServiceStatus( schs,  ss))then //ask for current status
      begin
        dwStat := ss.dwCurrentState;
      end;     

      CloseServiceHandle(schs); //we have the staus so close handle.
    end;

    CloseServiceHandle(schm); //close connection to service manager.
  end;

  Result := dwStat; //Result have the current status of the service.
end;

Now we have the function that get us the current status of the service
and determining the status we can infer if service is running ,stopped,or service are pending.This function will get return true if service is running and will get false is either service is not running or in a pending state like service_start_pending or service_stop_pending.

function ServiceRunning( sMachine, sService : string ) : boolean;
begin
  Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService );
end;
function ServiceStopped( sMachine, sService : string ) : boolean;
begin
  Result := SERVICE_STOPPED = ServiceGetStatus( sMachine, sService );
end;

This will return if the service is stopped.

So now you do have some niftly code so that you can ascertain in your program the current status of a particular service just by adding some lines of codes.

You can follow any responses to this entry through the RSS 2.0 feed.