以下我写的一个控制Service的对象,可以方便实现对Service
unit ServiceControl;
interface
uses
Windows, SysUtils, WinSvc, SvcMgr;
type
TServiceControl = class(TObject)
private
FMachine, FService: string;
function GetNTStatus: DWord;
function GetStatus: TCurrentStatus;
protected
public
function Start: Boolean;
function Stop: Boolean;
function Pause: Boolean;
function Continue: Boolean;
constructor Create(Service: string; Machine: string = '');
property NTStatus: DWord read GetNTStatus;
property Status: TCurrentStatus read GetStatus;
property Service: string read FService write FService;
property Machine: string read FMachine write FMachine;
published
end; { TServiceControl }
function StatusToNT(Status: TCurrentStatus): DWord;
function StatusFromNt(Status: DWord): TCurrentStatus;
implementation
function StatusToNT(Status: TCurrentStatus): DWord;
const
NTServiceStatus: array[TCurrentStatus] of DWord = (SERVICE_STOPPED,
SERVICE_START_PENDING, SERVICE_STOP_PENDING, SERVICE_RUNNING,
SERVICE_CONTINUE_PENDING, SERVICE_PAUSE_PENDING, SERVICE_PAUSED);
begin
Result := NTServiceStatus[Status];
end;
function StatusFromNt(Status: DWord): TCurrentStatus;
const
CurrentStatus: array[1..7] of TCurrentStatus = (csStopped, csStartPending,
csStopPending, csRunning, csContinuePending, csPausePending, csPaused);
begin
Result := CurrentStatus[Status];
end;
{ TServiceControl }
constructor TServiceControl.Create(Service: string; Machine: string = '');
begin
FService := Service;
FMachine := Machine;
end;
function TServiceControl.GetStatus: TCurrentStatus;
begin
Result := StatusFromNT(GetNTStatus);
end;
function TServiceControl.GetNTStatus: DWord;
var
SvcMgr, Svc: SC_HANDLE;
SvcStatus: TServiceStatus;
begin
Result := 0;
SvcMgr := OpenSCManager(PChar(FMachine), nil, SC_MANAGER_CONNECT);
if SvcMgr = 0 then RaiseLastWin32Error;
try
Svc := OpenService(SvcMgr, PChar(FService), SERVICE_QUERY_STATUS);
if Svc = 0 then RaiseLastWin32Error;
try
if QueryServiceStatus(Svc, SvcStatus) then
Result := SvcStatus.dwCurrentState
else
RaiseLastWin32Error;
finally
CloseServiceHandle(Svc);
end;
finally
CloseServiceHandle(SvcMgr);
end;
end;
function TServiceControl.Start: Boolean;
var
psTemp: PChar;
CheckPoint: DWord;
SvcMgr, Svc: SC_HANDLE;
SvcStatus: TServiceStatus;
begin
SvcMgr := OpenSCManager(PChar(FMachine), nil, SC_MANAGER_CONNECT);
if SvcMgr = 0 then RaiseLastWin32Error;
try
Svc := OpenService(SvcMgr, PChar(FService), SERVICE_START or SERVICE_QUE
RY_STATUS);
if Svc = 0 then RaiseLastWin32Error;
try
psTemp := nil;
if StartService(Svc, 0, psTemp) then
begin
if not QueryServiceStatus(Svc, SvcStatus) then
RaiseLastWin32Error
else
with SvcStatus do
begin
while (SERVICE_RUNNING <> dwCurrentState) do
begin
CheckPoint := dwCheckPoint;
Sleep(dwWaitHint);
if not QueryServiceStatus(Svc, SvcStatus) then
Break;
if dwCheckPoint < CheckPoint then
Break;
end;
end;
end;
finally
CloseServiceHandle(Svc);
end;
finally
CloseServiceHandle(SvcMgr);
end;
Result := SERVICE_RUNNING = SvcStatus.dwCurrentState;
end;
function TServiceControl.Stop: Boolean;
var
CheckPoint: DWord;
SvcMgr, Svc: SC_HANDLE;
SvcStatus: TServiceStatus;
begin
SvcMgr := OpenSCManager(PChar(FMachine), nil, SC_MANAGER_CONNECT);
if SvcMgr = 0 then RaiseLastWin32Error;
try
Svc := OpenService(SvcMgr, PChar(FService), SERVICE_STOP or SERVICE_QUER
Y_STATUS);
if Svc = 0 then RaiseLastWin32Error;
try
if ControlService(Svc, SERVICE_CONTROL_STOP, SvcStatus) then
begin
if not QueryServiceStatus(Svc, SvcStatus) then
RaiseLastWin32Error
else
with SvcStatus do
begin
while (SERVICE_STOPPED <> dwCurrentState) do
begin
CheckPoint := dwCheckPoint;
Sleep(dwWaitHint);
if not QueryServiceStatus(Svc, SvcStatus) then
Break;
if dwCheckPoint < CheckPoint then
Break;
end;
end;
end;
finally
CloseServiceHandle(Svc);
end;
finally
CloseServiceHandle(SvcMgr);
end;
Result := SERVICE_STOPPED = SvcStatus.dwCurrentState;
end;
function TServiceControl.Continue: Boolean;
var
CheckPoint: DWord;
SvcMgr, Svc: SC_HANDLE;
SvcStatus: TServiceStatus;
begin
SvcMgr := OpenSCManager(PChar(FMachine), nil, SC_MANAGER_CONNECT);
if SvcMgr = 0 then RaiseLastWin32Error;
try
Svc := OpenService(SvcMgr, PChar(FService), SERVICE_PAUSE_CONTINUE or SE
RVICE_QUERY_STATUS);
if Svc = 0 then RaiseLastWin32Error;
try
if ControlService(Svc, SERVICE_CONTROL_CONTINUE, SvcStatus) then
begin
if not QueryServiceStatus(Svc, SvcStatus) then
RaiseLastWin32Error
else
with SvcStatus do
begin
while (SERVICE_RUNNING <> dwCurrentState) do
begin
CheckPoint := dwCheckPoint;
Sleep(dwWaitHint);
if not QueryServiceStatus(Svc, SvcStatus) then
Break;
if dwCheckPoint < CheckPoint then
Break;
end;
end;
end;
finally
CloseServiceHandle(Svc);
end;
finally
CloseServiceHandle(SvcMgr);
end;
Result := SERVICE_RUNNING = SvcStatus.dwCurrentState;
end;
function TServiceControl.Pause: Boolean;
var
CheckPoint: DWord;
SvcMgr, Svc: SC_HANDLE;
SvcStatus: TServiceStatus;
begin
SvcMgr := OpenSCManager(PChar(FMachine), nil, SC_MANAGER_CONNECT);
if SvcMgr = 0 then RaiseLastWin32Error;
try
Svc := OpenService(SvcMgr, PChar(FService), SERVICE_PAUSE_CONTINUE or SE
RVICE_QUERY_STATUS);
if Svc = 0 then RaiseLastWin32Error;
try
if ControlService(Svc, SERVICE_CONTROL_PAUSE, SvcStatus) then
begin
if not QueryServiceStatus(Svc, SvcStatus) then
RaiseLastWin32Error
else
with SvcStatus do
begin
while (SERVICE_PAUSED <> dwCurrentState) do
begin
CheckPoint := dwCheckPoint;
Sleep(dwWaitHint);
if not QueryServiceStatus(Svc, SvcStatus) then
Break;
if dwCheckPoint < CheckPoint then
Break;
end;
end;
end;
finally
CloseServiceHandle(Svc);
end;
finally
CloseServiceHandle(SvcMgr);
end;
Result := SERVICE_PAUSED = SvcStatus.dwCurrentState;
end;
end.
--
. 生命的意义在于 // ____/ //_ / //_/ .
. 希望、 / / /___/_/// / //_/__ __ _ _★ .
. 工作、 / / ____// / / // / /'__ / //`'_/ .
. 爱你的人、 / / /___/ / / /___/ / /// __/// / // .
. 和你爱的人 / /___/ / /_____/ /__/ /____/ / /_/ .
. …… //___/ //_____///__///____/ //_/ @126.com .