请问如何用delphi实现监测并控制SQLSERVER的运行和停止?(100分)

  • 主题发起人 主题发起人 d5
  • 开始时间 开始时间
好高难的问题!
 
真的很难吗?要不再加50分!(100分?!)
另外,不限用delphi!
 
以下我写的一个控制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 .



 
flier:怎样检测呢?
 
呵呵, 用的是OpenSCManager, OpenService, QueryServiceStatus等api.
var
ttt:TServiceControl;
begin
ttt:=Tservicecontrol.create('MSSQLServer','Machine Name Where SQLServer Running');
Select ttt.status
case SERVICE_STOPPED : showmessage('SQLServer is stopped!');
case.....
end;

 
你也可以使用这些API:
ResUtilStartResourceService
ResUtilStopResourceService
ResUtilStopService
ResUtilVerifyResourceService
ResUtilVerifyService
这些都是MS WinAPI。
 
本来想让你关注一下SERVICE的,竟然把代码都贴了,厉害呀厉害!
 
多人接受答案了。
 
后退
顶部