Created
August 15, 2012 02:14
-
-
Save xingfuqiu/3354945 to your computer and use it in GitHub Desktop.
Delphi:服务控制单元
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit ServiceControl; | |
interface | |
uses | |
WinSvc, | |
SysUtils, | |
Windows; | |
type | |
TServiceControl = class(TObject) | |
public | |
constructor Create; | |
destructor Destroy; override; | |
function DeleteServices(ServiceName: String): Integer; | |
function GetServicesState(ServiceName: String ;var State: String): Integer; | |
function InstallServices(FileName, ServiceName, DisPlayName: String; var Err: | |
String): Integer; | |
function StartServices(ServiceName: String; var Err: String): Integer; | |
function StopServices(ServiceName: String): Integer; | |
end; | |
implementation | |
{ TServiceControl } | |
constructor TServiceControl.Create; | |
begin | |
inherited; | |
end; | |
destructor TServiceControl.Destroy; | |
begin | |
inherited; | |
end; | |
function TServiceControl.DeleteServices(ServiceName: String): Integer; | |
var | |
hSCM,hSCS:SC_HANDLE; | |
begin | |
hSCS := 0; | |
hSCM := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS); | |
if hSCM <> 0 then | |
begin | |
hSCS := OpenService(hSCM, pChar(ServiceName), SC_MANAGER_ALL_ACCESS); | |
if hSCS <> 0 then | |
begin | |
DeleteService(hSCS); //删除服务 | |
Result := hSCS; | |
end else Result := 0; | |
end else Result := 0; | |
CloseServiceHandle(hSCM); | |
CloseServiceHandle(hSCS); | |
end; | |
function TServiceControl.GetServicesState(ServiceName: String ;var State: | |
String): Integer; | |
var | |
hSCM, hSCS: SC_HANDLE; | |
Ser_State: TServiceStatus; | |
begin | |
Result := -1; | |
hSCM := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS); | |
if hSCM <> 0 then | |
begin | |
hSCS := OpenService(hSCM, pChar(ServiceName), SC_MANAGER_ALL_ACCESS); | |
if hSCS <> 0 then | |
begin | |
QueryServiceStatus(hSCS, Ser_State); | |
{*** -1 = Error opening service ***} | |
{*** 1 = SERVICE_STOPPED ***} | |
{*** 2 = SERVICE_START_PENDING ***} | |
{*** 3 = SERVICE_STOP_PENDING ***} | |
{*** 4 = SERVICE_RUNNING ***} | |
{*** 5 = SERVICE_CONTINUE_PENDING ***} | |
{*** 6 = SERVICE_PAUSE_PENDING ***} | |
{*** 7 = SERVICE_PAUSED ***} | |
case Ser_State.dwCurrentState of | |
1: State := '服务已停止'; | |
2: State := '服务正在启动'; | |
3: State := '服务正在停止'; | |
4: State := '服务运行正常'; | |
5: State := '服务正在恢复'; | |
6: State := '服务正在被暂停'; | |
7: State := '服务已暂停'; | |
else State := Format('取服务状态失败, 错误代码:%d', [Ser_State.dwCurrentState]); | |
end; | |
Result := Ser_State.dwCurrentState; | |
end else | |
begin | |
State := '打开服务出错, 服务未安装.'; | |
Result := -1; | |
end; | |
CloseServiceHandle(hSCM); | |
CloseServiceHandle(hSCS); | |
end else State := '打开服务管理器出错.'; | |
end; | |
function TServiceControl.InstallServices(FileName, ServiceName, DisPlayName: | |
String; var Err: String): Integer; | |
var | |
hSCM, hSCS, hCheck:SC_HANDLE; | |
begin | |
hSCS := 0; | |
hSCM := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS); | |
if hSCM <> 0 then | |
begin //打开服务管理器 | |
hCheck := OpenService(hSCM, pChar(ServiceName), SC_MANAGER_ALL_ACCESS); | |
if hCheck <> 0 then //服务已安装 | |
begin | |
OutputDebugString(pChar('安装'+ServiceName+'失败, 错误原因: 服务已存在.')); | |
CloseServiceHandle(hSCM); | |
CloseServiceHandle(hCheck); | |
Err := '服务已安装'; | |
Result := 0; | |
Exit; | |
end; | |
hSCS := CreateService(hSCM, pChar(ServiceName), pChar(DisPlayName), SERVICE_ALL_ACCESS, | |
SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, pChar(FileName), | |
nil, nil, nil, nil, nil); | |
Err := '安装成功'; | |
Result := hSCS; | |
CloseServiceHandle(hCheck); | |
end else | |
begin | |
Err := '打开服务管理器失败'; | |
Result := hSCM; | |
end; | |
CloseServiceHandle(hSCM); | |
CloseServiceHandle(hSCS); | |
end; | |
function TServiceControl.StartServices(ServiceName: String; var Err: String): | |
Integer; | |
var | |
hSCM,hSCS:SC_HANDLE; | |
Args: pChar; | |
SC_State: TServiceStatus; | |
begin | |
hSCS := 0; | |
hSCM := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS); | |
if hSCM <> 0 then | |
begin | |
hSCS := OpenService(hSCM, pChar(ServiceName), SC_MANAGER_ALL_ACCESS); | |
//取服务状态 | |
QueryServiceStatus(hSCS, SC_State); | |
if SC_State.dwCurrentState = 4 then | |
begin | |
CloseServiceHandle(hSCM); | |
CloseServiceHandle(hSCS); | |
Err := '服务已启动'; | |
OutputDebugString(pChar('启动'+ServiceName+'失败, 错误原因:服务已启动')); | |
Result := 0; | |
Exit; | |
end; | |
Args := nil; | |
StartService(hSCS, 0, Args); | |
Result := hSCS; | |
end else Result := hSCM; | |
CloseServiceHandle(hSCM); | |
CloseServiceHandle(hSCS); | |
end; | |
function TServiceControl.StopServices(ServiceName: String): Integer; | |
var | |
hSCM,hSCS:SC_HANDLE; | |
Rec_status :TServiceStatus; | |
begin | |
hSCS := 0; | |
hSCM := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS); | |
if hSCM <> 0 then | |
begin | |
hSCS := OpenService(hSCM, pChar(ServiceName), SC_MANAGER_ALL_ACCESS); | |
//取服务状态 | |
QueryServiceStatus(hSCS, Rec_status); | |
if Rec_status.dwCurrentState = SERVICE_STOPPED then | |
begin | |
OutputDebugString(pChar('停止'+serviceName+'失败, 错误原因:服务已停止')); | |
Result := 0; | |
end else | |
begin | |
ControlService(hSCS, SERVICE_CONTROL_STOP, rec_status); //停止服务 | |
if Rec_status.dwCurrentState = SERVICE_STOPPED then | |
Result := 1 | |
else | |
Result := 0; | |
end; | |
end else Result := hSCM; | |
CloseServiceHandle(hSCM); | |
CloseServiceHandle(hSCS); | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment