unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WinSvc, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//-------------------------------------
// 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 := DWORD(-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;
//-------------------------------------
// 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;
procedure TForm1.Button1Click(Sender: TObject);
begin
// ¼ºñ½º ½ÃÀÛµÊ(¿©±â¼´Â DHCP Ŭ¶óÀÌ¾ðÆ®·Î Å×½ºÆ® ÇßÀ½)
if( ServiceRunning(
'',
'Dhcp' ) )then
begin
ShowMessage('DHCP Client service on the local computer is running');
end;
// ¼ºñ½º ÁßÁöµÊ
if( ServiceStopped(
'',
'Dhcp' ) )then
begin
ShowMessage('DHCP Client on the local computer is in the stopped state');
end;
{ // ´Ù¸¥ ÄÄÇ»ÅÍÀÇ ¼ºñ½º »óÅ ±¸Çϱâ
if( ServiceRunning(
'\ComputerName',
'Dhcp' ) )then
begin
ShowMessage("DHCP Client on \ComputerName is running take appropriate action here');
end;
}
end;
end.
|
|