Board index » delphi » How do I communicate with the Service Control Manager?

How do I communicate with the Service Control Manager?

Does anyone know how I can start, stop and check the status on a service
(through the SCM?)?

I tried to use the OpenSCManager function to get a handle to the SCM but
Delphi responded with Undeclared Identifier. I have included Windows,
Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls,
Menus, ComCtrls, StdCtrls, Buttons in my uses clause.

Anyone know how to solve this?

Thanks,
Micke

 

Re:How do I communicate with the Service Control Manager?


I got this from these forums at an earlier time:

Looking for a way to check the status of Windows services?
"QueryServiceStatus()" Win32 function can do this for you, but you have
to setup a few things beforehand using "OpenSCManager()" and
"OpenService()" functions as demonstrated below.

             uses WinSvc;

             //-------------------------------------
             // get service status
             //
             // return status code if successful
             // -1 if not
             //
             // return codes:
             //   SERVICE_STOPPED
             //   SERVICE_RUNNING
             //   SERVICE_PAUSED
             //
             // following return codes
             // are used to indicate that
             // the service is in the
             // middle of getting to one
             // of the above states:
             //   SERVICE_START_PENDING
             //   SERVICE_STOP_PENDING
             //   SERVICE_CONTINUE_PENDING
             //   SERVICE_PAUSE_PENDING
             //
             // sMachine:
             //   machine name, ie: \\SERVER   empty = local machine
             //
             // sService
             //   service name, ie: Alerter
             //
             function ServiceGetStatus(
               sMachine,
               sService : string ) : DWord;
             var
               //
               // service control
               // manager handle
               schm,
               //
               // service handle
               schs   : SC_Handle;
               //
               // service status
               ss     : TServiceStatus;
               //
               // current service status
               dwStat : DWord;
             begin
               dwStat := -1;

               // connect to the service
               // control manager
               schm := OpenSCManager(
                 PChar(sMachine),
                 Nil,
                 SC_MANAGER_CONNECT);

               // if successful...
               if(schm > 0)then
               begin
                 // open a handle to
                 // the specified service
                 schs := OpenService(
                   schm,
                   PChar(sService),
                   // we want to
                   // query service status
                   SERVICE_QUERY_STATUS);

                 // if successful...
                 if(schs > 0)then
                 begin
                   // retrieve the current status
                   // of the specified service    
                   if(QueryServiceStatus(
                        schs,
                        ss))then
                   begin
                     dwStat := ss.dwCurrentState;
                   end;

                   // close service handle
                   CloseServiceHandle(schs);
                 end;

                 // close service control
                 // manager handle
                 CloseServiceHandle(schm);
               end;

               Result := dwStat;
             end;

             Block #1 : Delphi code. Right click chkss.pas to download.

             Now that we have a basic function -- "ServiceGetStatus()"
-- to get
             the status of a specified service running on a specified
machine,
             let's simplify the process even more with the following
             "ServiceRunning()" and "ServiceStopped()" functions.

             //-------------------------------------
             // return TRUE if the specified
             // service is running, defined by
             // the status code SERVICE_RUNNING.
             // return FALSE if the service
             // is in any other state, including
             // any pending states
             //
             function ServiceRunning(
               sMachine,
               sService : string ) : boolean;
             begin
               Result := SERVICE_RUNNING =
                 ServiceGetStatus(
                   sMachine, sService );
             end;

             //-------------------------------------
             // return TRUE if the specified
             // service was stopped, defined by
             // the status code SERVICE_STOPPED.
             //
             function ServiceStopped(
               sMachine,
               sService : string ) : boolean;
             begin
               Result := SERVICE_STOPPED =
                 ServiceGetStatus(
                   sMachine, sService );
             end;

             Block #2 : Delphi code. Right click chkss2.pas to download.

             Both "ServiceRunning()" and "ServiceStopped()" functions
             expect a standard machine name and a service name. The
machine
             name can be empty if you're checking a service running on
the local
             machine. For example:

             if( ServiceRunning(
               '\\ComputerName',
               'alerter' ) )then
             begin
               // "alerter" service on \\ComputerName
               // is running
               // take appropriate action here
             end;

             if( ServiceRunning(
               '',
               'alerter' ) )then
             begin
               // "alerter" service on the
               // local computer is running
             end;

             if( ServiceStopped(
               '',
               'alerter' ) )then
             begin
               // "alerter" service on the
               // local computer is in the
               // "stopped" state (not running)
             end

HTH

Rkr

Quote
"Mikael ?gren" wrote:

> Does anyone know how I can start, stop and check the status on a service
> (through the SCM?)?

> I tried to use the OpenSCManager function to get a handle to the SCM but
> Delphi responded with Undeclared Identifier. I have included Windows,
> Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls,
> Menus, ComCtrls, StdCtrls, Buttons in my uses clause.

> Anyone know how to solve this?

> Thanks,
> Micke

Re:How do I communicate with the Service Control Manager?


Thanks!! It really helped a lot!!

/Micke

Other Threads