各位大虾,怎样判断NT下一个服务存在并且已经启动(100分)

  • 主题发起人 主题发起人 pw201
  • 开始时间 开始时间
P

pw201

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾,怎样判断NT下一个服务存在并且已经启动
 
在管理工具下的服务下查找相应的项目即可


看他是否已经启动
 
参考下面代码

unit Control;

interface

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

const
ServiceName='TomDemoService';

type
TfrmMain = class(TForm)
cmdStart: TButton;
cmdStop: TButton;
stsMessage: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure cmdStopClick(Sender: TObject);
procedure cmdStartClick(Sender: TObject);
private
{ Private declarations }
schService:SC_HANDLE;
schSCManager:SC_HANDLE;
ssStatus:TServiceStatus;
public
{ Public declarations }
end;

var
frmMain: TfrmMain;

implementation

{$R *.DFM}


procedure TfrmMain.FormCreate(Sender: TObject);
begin
schSCManager:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
if (schSCManager<=0) then
begin
MessageDlg('Tom Lee Demo Service is not installed.',mtError,[mbOk],0);
Application.Terminate;
end;

schService:=OpenService(schSCManager,ServiceName,SERVICE_ALL_ACCESS);
if (schService<=0) then
begin
MessageDlg('Tom Lee Demo Service can not open.',mtError,[mbOk],0);
Application.Terminate;
end;
end;

procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
end;

procedure TfrmMain.cmdStopClick(Sender: TObject);
begin
if ControlService(schService,SERVICE_CONTROL_STOP,ssStatus) then
begin
stsMessage.SimpleText:='Stopping Service Now ';
Sleep(1000);
while (QueryServiceStatus(schService,ssStatus)) do
begin
Application.ProcessMessages;
if ssStatus.dwCurrentState=SERVICE_STOP_PENDING then
begin
stsMessage.SimpleText:=stsMessage.SimpleText+('.');
Sleep(1000);
end
else
break;
end;

if ssStatus.dwCurrentState=SERVICE_STOPPED then
stsMessage.SimpleText:='Service Stop Ok'
else
stsMessage.SimpleText:='Service Stop Fail';
end
else
stsMessage.SimpleText:='Service Stop Fail';
end;

procedure TfrmMain.cmdStartClick(Sender: TObject);
var
Argv:PChar;
begin
if StartService(schService,0,Argv) then
begin
stsMessage.SimpleText:='Start Service Now ';
Sleep(1000);
while (QueryServiceStatus(schService,ssStatus)) do
begin
Application.ProcessMessages;
if ssStatus.dwCurrentState=SERVICE_START_PENDING then
begin
stsMessage.SimpleText:=stsMessage.SimpleText+('.');
Sleep(1000);
end
else
break;
end;

if ssStatus.dwCurrentState=SERVICE_RUNNING then
stsMessage.SimpleText:='Service Start Ok'
else
stsMessage.SimpleText:='Service Start Fail';
end
else
stsMessage.SimpleText:='Service Start Fail';
end;

end.
 
下面是我写过的一个函数,记得要uses WinSvc单元

// 操纵 Windows NT/2000 的服务
Function TMainForm.ServiceControl(aServiceName: String; aOp: Integer) : Boolean;
Var
scm : Cardinal;
svc : Cardinal;
sts : _SERVICE_STATUS;
arg : PChar;
begin
Result := true;
If ( ( GetVersion AND $80000000 ) = 0 ) Then // Is NT/2000
Begin
scm := OpenSCManager( Nil, Nil, SC_MANAGER_CONNECT );
svc := OpenService( scm, PChar( aServiceName ), SERVICE_ALL_ACCESS );
If ( svc <> 0 ) Then // *****这里是检查服务是否存在*****
Begin
QueryServiceStatus( svc, sts ); // *****这里就是检查服务的状态*****
If ( ( ( aOp = 0 ) AND ( sts.dwCurrentState <> SERVICE_STOPPED ) )
OR ( ( aOp = 1 ) AND ( sts.dwCurrentState <> SERVICE_RUNNING ) ) ) Then
Begin
arg := Nil;
If ( aOp = 0 ) Then // Stop
Result := ControlService( svc, SERVICE_CONTROL_STOP, sts )
Else
Result := StartService( svc, 0, arg );
End;
CloseServiceHandle( svc );
End;
CloseServiceHandle( scm );
End;
end;
 
谢谢各位,待程序通过后就来分分
 
多人接受答案了。
 

Similar threads

后退
顶部