⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 服务状态的判断、启动与停止 (2001年3月26日).txt

📁 自己对DELPHI学习的一点体会
💻 TXT
字号:
服务状态的判断、启动与停止 (2001年3月26日) 

网友更新  分类:Win API   作者: yanlei(推荐)  推荐:yanlei   阅读次数:321  
(http://www.codesky.net)  

--------------------------------------------------------------------------------
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
uses
WinSvc, WinTypes;
function ServiceGetKeyName(
sMachine,
sServiceDispName : string ) : string;
var
//
// service control
// manager handle
schm : SC_Handle;

//
// max key name len
nMaxNameLen : dWORD;
//
// temp. string
psServiceName : PChar;
begin
Result := '';

// expect a service key
// name shorter than 255
// characters
nMaxNameLen := 255;

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

// if successful...
if(schm > 0)then
begin
psServiceName :=
StrAlloc(nMaxNameLen+1);

if(nil <> psServiceName)then
begin
if( GetServiceKeyName(
schm,
PChar(sServiceDispName),
psServiceName,
nMaxNameLen))then
begin
psServiceName
[nMaxNameLen] := #0;

Result :=
StrPas( psServiceName );
end;

StrDispose(psServiceName);
end;

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

function ServiceStart(
sMachine,
sService : string ) : boolean;
var
//
// service control
// manager handle
schm,
//
// service handle
schs : SC_Handle;
//
// service status
ss : TServiceStatus;
//
// temp char pointer
psTemp : PChar;
//
// check point
dwChkP : DWord;
begin
//ss.dwCurrentState := -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),SERVICE_START or SERVICE_QUERY_STATUS);
// we want to
// start the service and
// query service status


// if successful...
if(schs > 0)then
begin
psTemp := Nil;
if(StartService(schs, 0, psTemp)) then
begin
// check status
if(QueryServiceStatus(schs,ss)) then
begin
while(SERVICE_RUNNING<> ss.dwCurrentState)do
begin
//
// dwCheckPoint contains a
// value that the service
// increments periodically
// to report its progress
// during a lengthy
// operation.
//
// save current value
//
dwChkP := ss.dwCheckPoint;

//
// wait a bit before
// checking status again
//
// dwWaitHint is the
// estimated amount of time
// the calling program
// should wait before calling
// QueryServiceStatus() again
//
// idle events should be
// handled here...
//
Sleep(ss.dwWaitHint);
application.ProcessMessages;

if(not QueryServiceStatus(
schs,
ss))then
begin
// couldn't check status
// break from the loop
break;
end;

if(ss.dwCheckPoint <
dwChkP)then
begin
// QueryServiceStatus
// didn't increment
// dwCheckPoint as it
// should have.
// avoid an infinite
// loop by breaking
break;
end;
end;
end;
end;

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

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

// return TRUE if
// the service status is running
Result :=SERVICE_RUNNING = ss.dwCurrentState;
end;
function ServiceStop(
sMachine,
sService : string ) : boolean;
var
//
// service control
// manager handle
schm,
//
// service handle
schs : SC_Handle;
//
// service status
ss : TServiceStatus;
//
// check point
dwChkP : DWord;
begin
// 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
// stop the service and
SERVICE_STOP or
// query service status
SERVICE_QUERY_STATUS);

// if successful...
if(schs > 0)then
begin
if(ControlService(
schs,
SERVICE_CONTROL_STOP,
ss))then
begin
// check status
if(QueryServiceStatus(
schs,
ss))then
begin
while(SERVICE_STOPPED
<> ss.dwCurrentState)do
begin
//
// dwCheckPoint contains a
// value that the service
// increments periodically
// to report its progress
// during a lengthy
// operation.
//
// save current value
//
dwChkP := ss.dwCheckPoint;

//
// wait a bit before
// checking status again
//
// dwWaitHint is the
// estimated amount of time
// the calling program
// should wait before calling
// QueryServiceStatus() again
//
// idle events should be
// handled here...
//
Sleep(ss.dwWaitHint);
application.ProcessMessages;

if(not QueryServiceStatus(
schs,
ss))then
begin
// couldn't check status
// break from the loop
break;
end;

if(ss.dwCheckPoint <
dwChkP)then
begin
// QueryServiceStatus
// didn't increment
// dwCheckPoint as it
// should have.
// avoid an infinite
// loop by breaking
break;
end;
end;
end;
end;

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

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

// return TRUE if
// the service status is stopped
Result :=
SERVICE_STOPPED =
ss.dwCurrentState;
end;



procedure TForm1.Button1Click(Sender: TObject);//启动服务
begin
if ServiceStart('',ServiceGetKeyName( '',edit1.Text) ) then
showmessage('成功')
else
showmessage('不成功');


end;

procedure TForm1.Button2Click(Sender: TObject);//停止服务
begin
if ServiceStop( '', ServiceGetKeyName('',edit1.Text) ) then
showmessage('成功')
else
showmessage('不成功');


end;
function ServiceGetStrCode(
nID : integer ) : string;
var
s : string;
begin
case nID of
SERVICE_STOPPED:
s := 'STOPPED';
SERVICE_RUNNING:
s := 'RUNNING';
SERVICE_PAUSED:
s := 'PAUSED';
SERVICE_START_PENDING:
s := 'START/PENDING';
SERVICE_STOP_PENDING:
s := 'STOP/PENDING';
SERVICE_CONTINUE_PENDING:
s := 'CONTINUE/PENDING';
SERVICE_PAUSE_PENDING:
s := 'PAUSE/PENDING';
else
s := 'UNKNOWN';
end;

Result := s;
end;

function ServiceGetStatus(
sMachine,
sService : string ) : DWord;
var
//
// service control
// manager handle
schm,
//
// service handle
schs : SC_Handle;
//
// service status
ss : TServiceStatus;
//
// current service status
dwState : integer;
begin
dwState:= -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
dwState := ss.dwCurrentState;
end;

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

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

Result := dwState;
end;



procedure TForm1.Button3Click(Sender: TObject);//检查服务状态
var
n : integer;
sKeyName,
s : string;
begin
sKeyName :=
ServiceGetKeyName(
'',
edit1.Text);//'Fax Service'



n := ServiceGetStatus(
'',
sKeyName );

s := ServiceGetStrCode( n );


if(SERVICE_STOPPED = n)then
begin
showmessage('停止');
end;
if(SERVICE_RUNNING = n)then
begin
showmessage('运行');
end;
end;
end.
注:在delphi深度探险中源码下载后改造  
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -