服务的管理和控制
如果要自己定义服务的控制行为,需要override服务的DoCustomControl:
function TSvrXXXXX.DoCustomControl(CtrlCode: DWord): Boolean;
begin
Result := inherited DoCustomControl(CtrlCode);
case CtrlCode of
0:;
end;
end;
注意:CtrlCode的值必须大于128!
而外部的控制可以使用API函数ControlService来做:
function ServiceControl(const ServiceName: string; const ControlCode: integer;const Computer: PChar = nil): Boolean;
var
SCM, SCH: SC_Handle;
ServiceStatus: TServiceStatus;
begin
if IsLocalComputer(Computer) then
SCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS)
else
SCM := OpenSCManager(Computer, nil, SC_MANAGER_ALL_ACCESS);
if SCM <> 0 then
begin
SCH := OpenService(SCM, PChar(ServiceName), SERVICE_ALL_ACCESS);
if SCH <> 0 then
begin
Result := ControlService(SCH, ControlCode, ServiceStatus);
CloseServiceHandle(SCH);
end;
CloseServiceHandle(SCM);
end;
end;
需要注意的是,服务和控制部分的CtrlCode的定义必须一致!同时CtrolCode必须在128-255之间!