unit ServiceMgr;
interface
uses Windows,Messages,SysUtils,Winsvc,Dialogs;
function StartServices(Const SvrName:String):Boolean;
function StopServices(Const SvrName:String):Boolean;
function QueryServiceStatu(Const SvrName: String):String;
function CreateServices(Const SvrName,FilePath:String):Boolean;
function DeleteServices(Const SvrName: String):Boolean;
function IsServiceExisted(Const SvrName: String):Boolean;
implementation
//开启服务
function StartServices(Const SvrName: String): Boolean;
var
sMgr, sHandle:SC_HANDLE;
c:PChar;
begin
Result:=False;
sMgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
if sMgr <=0 then Exit;
sHandle := OpenService(sMgr, PChar(SvrName), SERVICE_ALL_ACCESS);
if sHandle <=0 then Exit;
try
Result:=StartService(sHandle, 0, c);
CloseServiceHandle(sHandle);
CloseServiceHandle(sMgr);
except
CloseServiceHandle(sHandle);
CloseServiceHandle(sMgr);
end;
end;
//停止服务
function StopServices(Const SvrName: String): Boolean;
var
sMgr, sHandle: SC_HANDLE;
d: TServiceStatus;
begin
Result := False;
sMgr := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
if sMgr <=0 then Exit;
sHandle := OpenService(sMgr,PChar(SvrName),SERVICE_ALL_ACCESS);
if sHandle <=0 then Exit;
try
Result:=ControlService(sHandle, SERVICE_CONTROL_STOP,d);
CloseServiceHandle(sMgr);
CloseServiceHandle(sHandle);
except
CloseServiceHandle(sMgr);
CloseServiceHandle(sHandle);
end;
end;
//查询当前服务的状态
function QueryServiceStatu(Const SvrName: String): String;
var
sMgr, sHandle: SC_HANDLE;
d: TServiceStatus;
begin
Result := '未安装';
sMgr := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
if sMgr <=0 then Exit;
sHandle := OpenService(sMgr,PChar(SvrName),SERVICE_ALL_ACCESS);
if sHandle <= 0 then Exit;
try
QueryServiceStatus(sHandle, d);
if d.dwCurrentState = SERVICE_RUNNING then
Result := '启动' //Run
else if d.dwCurrentState = SERVICE_RUNNING then
Result := 'Wait' //Runing
else if d.dwCurrentState = SERVICE_START_PENDING then
Result := 'Wait' //Pause
else if d.dwCurrentState = SERVICE_STOP_PENDING then
Result := '停止' //Pause
else if d.dwCurrentState = SERVICE_PAUSED then
Result := '暂停' //Pause
else if d.dwCurrentState = SERVICE_STOPPED then
Result := '停止' //Stop
else if d.dwCurrentState = SERVICE_CONTINUE_PENDING then
Result := 'Wait' //Pause
else if d.dwCurrentState = SERVICE_PAUSE_PENDING then
Result := 'Wait'; //Pause
CloseServiceHandle(sMgr);
CloseServiceHandle(sHandle);
except
CloseServiceHandle(sMgr);
CloseServiceHandle(sHandle);
end;
end;
{建立服务}
function CreateServices(Const SvrName,FilePath: String): Boolean;
var
sMgr, sHandle:SC_HANDLE;
begin
Result:=False;
if FilePath = '' then Exit;
sMgr := OpenSCManager(nil,nil,SC_MANAGER_CREATE_SERVICE);
if sMgr <= 0 then Exit;
try
sHandle := CreateService(sMgr, PChar(SvrName),
PChar(SvrName),
SERVICE_ALL_ACCESS,
SERVICE_INTERACTIVE_PROCESS or SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,
PChar(FilePath),nil,nil,nil,nil,nil);
if sHandle <= 0 then begin
ShowMessage( SysErrorMessage(GetlastError));
Exit;
end;
CloseServiceHandle(sMgr);
CloseServiceHandle(sHandle);
Result := True;
except
CloseServiceHandle(sMgr);
CloseServiceHandle(sHandle);
Exit;
end;
end;
{卸载服务}
function DeleteServices(Const SvrName: String): Boolean;
var
sMgr, sHandle:SC_HANDLE;
begin
Result:=False;
sMgr := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
if sMgr <= 0 then Exit;
sHandle :=OpenService(sMgr,PChar(SvrName),STANDARD_RIGHTS_REQUIRED);
if sHandle <= 0 then Exit;
try
Result := DeleteService(sHandle);
if not Result then
ShowMessage(SysErrorMessage(GetlastError));
CloseServiceHandle(sHandle);
CloseServiceHandle(sMgr);
except
CloseServiceHandle(sHandle);
CloseServiceHandle(sMgr);
Exit;
end;
end;
function IsServiceExisted(Const SvrName: String):Boolean;
var
sMgr, sHandle:SC_HANDLE;
begin
Result:=False;
sMgr := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
if sMgr <= 0 then Exit;
sHandle :=OpenService(sMgr, PChar(SvrName), STANDARD_RIGHTS_REQUIRED);
if sHandle > 0 then
Result := True;
end;
end.