虽然自己解决了,但是总感觉有点不对,代码如下。有没有错误呀?
unit publicServ;
interface
uses
Windows,SysUtils,WinSvc;
type TServices = class
private
hscManager:SC_HANDLE;
hService:dword;
returnstatus:TServiceStatus;
function OpenEachHandle(PcharHost,ServiceName
Char):integer;
public
function SeeServStatus(PcharHost,ServiceName
Char):integer;
function SerStop(PcharHost,ServiceName
Char):integer;
function SerPause(PcharHost,ServiceName
Char):integer;
function SerContinue(PcharHost,ServiceName
Char):integer;
function SerInterrogate(PcharHost,ServiceName
Char):integer;
function SerShutDown(PcharHost,ServiceName
Char):integer;
function SerStart(PcharHost,ServiceName
Char):integer;
procedure CloseSerAll;
end;
implementation
{ TServices }
procedure TServices.CloseSerAll;
begin
CloseServiceHandle(hService);//关闭该Service
CloseServiceHandle(hscmanager);
end;
function TServices.OpenEachHandle(PcharHost,ServiceName
Char): integer;
begin
Result:=0;
hscManager:=OpenSCManager(PCharHost,nil,SC_MANAGER_ENUMERATE_SERVICE);
if hscmanager < 0 then
begin
//无法打开control manager database
Result:=-1;
Exit;
end;
hService := OpenService(hscmanager,ServiceName,SERVICE_ALL_ACCESS);
if hService <0 then
begin
//无法打开service
Result:=-2;
Exit;
end;
end;
//------------------------------------------------------------------------------
// 函数:SeeServStatus
// 功能:查看Nt下某一服务的状态
// 返回值: <0 为错误
// SERVICE_STOPPED = $00000001;
// SERVICE_START_PENDING = $00000002;
// SERVICE_STOP_PENDING = $00000003;
// SERVICE_RUNNING = $00000004;
// SERVICE_CONTINUE_PENDING = $00000005;
// SERVICE_PAUSE_PENDING = $00000006;
// SERVICE_PAUSED = $00000007;
//------------------------------------------------------------------------------
function TServices.SeeServStatus(PcharHost,ServiceName
Char):integer;
begin
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1;
CloseSerAll;
Exit;
end;
QueryServiceStatus(hService,returnstatus);
Result:=returnstatus.dwCurrentState;
//SERVICE_STOPPED
end;
//------------------------------------------------------------------------------
//命令集
//------------------------------------------------------------------------------
//继续
function TServices.SerContinue(PcharHost,ServiceName
Char):integer;
begin
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1 ;
CloseSerAll;
Exit;
end;
ControlService(hService,SERVICE_CONTROL_CONTINUE,returnstatus);
CloseSerAll;
end;
//询问
function TServices.SerInterrogate(PcharHost,ServiceName
Char):integer;
begin
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1 ;
CloseSerAll;
Exit;
end;
ControlService(hService,SERVICE_CONTROL_INTERROGATE,returnstatus);
CloseSerAll;
end;
//暂停
function TServices.SerPause(PcharHost,ServiceName
Char):integer;
begin
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1 ;
CloseSerAll;
Exit;
end;
ControlService(hService,SERVICE_CONTROL_PAUSE,returnstatus);
CloseSerAll;
end;
//down
function TServices.SerShutDown(PcharHost,ServiceName
Char):integer;
begin
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1 ;
CloseSerAll;
Exit;
end;
ControlService(hService,SERVICE_CONTROL_SHUTDOWN,returnstatus);
CloseSerAll;
end;
//启动
function TServices.SerStart(PcharHost,ServiceName
Char):integer;
var arg
Char;
begin
Arg:=nil;
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1;
CloseSerAll;
Exit;
end;
if not StartService(hService,0,arg) then
Result:=-1;
CloseSerAll;
end;
//停止
function TServices.SerStop(PcharHost,ServiceName
Char):integer;
begin
if OpenEachHandle(PCharHost,ServiceName)<0 then
begin
Result:=-1 ;
CloseSerAll;
Exit;
end;
ControlService(hService,SERVICE_CONTROL_STOP,returnstatus);
CloseSerAll;
end;
end.