delphi 操作服务代码 该内容来自
网络,上传此文的目的:1、为了获取分数;2、我认为它能对编程爱好者提供一点 点帮助。 unit Unit_Service; interface uses Windows, Messages, SysUtils,Winsvc,Dialogs; function function function function function implementation StartServices(Const SvrName: String): Boolean; StopServices(Const SvrName: String): Boolean; QueryServiceStatu(Const SvrName: String): String; CreateServices(Const SvrName,FilePath: String): Boolean; DeleteServices(Const SvrName: String): Boolean;
//开启服务 function StartServices(Const var a,b: SC_HANDLE; c: PChar; begin Result := False; a if b if try Result := := a := b
SvrName:
String):
Boolean;
OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS); <= 0 then Exit;
OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS); <= 0 then Exit;
StartService(b,0,c);
CloseServiceHandle(b); CloseServiceHandle(a); except CloseServiceHandle(b); CloseServiceHandle(a); Exit; end; end;
//停止服务 function StopServices(Const var a,b: SC_HANDLE; d: TServiceStatus; begin Result := False; a if b if try Result := := a := b
SvrName:
String):
Boolean;
OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS); <= 0 then Exit;
OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS); <= 0 then Exit;
ControlService(b,SERVICE_CONTROL_STOP,d);
CloseServiceHandle(a); CloseServiceHandle(b); except CloseServiceHandle(a); CloseServiceHandle(b); Exit; end; end;
//
查询当前服务的状态 function QueryServiceStatu(Const var a,b: SC_HANDLE; d: TServiceStatus; begin Result := '未安装 '; a if b := a :=
SvrName:
String):
String;
OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS); <= 0 then Exit;
OpenService(a,PChar(SvrName),SERVICE_ALL_ACCESS);
if try
b
<=
0
then
Exit;
QueryServiceStatus(b,d); if else else else else else else then else Result := 'Wait ' if d.dwCurrentState Result := 'Wait '; //Pause = SERVICE_PAUSE_PENDING //Pause then d.dwCurrentState = SERVICE_RUNNING then Result := '启动 ' //Run if d.dwCurrentState = SERVICE_RUNNING then Result := 'Wait ' //Runing if d.dwCurrentState = SERVICE_START_PENDING then Result := 'Wait ' //Pause if d.dwCurrentState = SERVICE_STOP_PENDING then Result := '停止 ' //Pause if d.dwCurrentState = SERVICE_PAUSED then Result := '暂停 ' //Pause if d.dwCurrentState = SERVICE_STOPPED then Result := '停止 ' //Stop if d.dwCurrentState = SERVICE_CONTINUE_PENDING
CloseServiceHandle(a); CloseServiceHandle(b); except CloseServiceHandle(a); CloseServiceHandle(b); Exit; end; end;
{建立服务} function CreateServices(Const var a,b: SC_HANDLE; begin Result := False; if a FilePath := ='' then
SvrName,FilePath:
String):
Boolean;
Exit;
OpenSCManager(nil,nil,SC_MANAGER_CREATE_SERVICE);
if a try b
<= :=
0
then
Exit;
CreateService(a,PChar(SvrName), PChar(SvrName), SERVICE_ALL_ACCESS, SERVICE_INTERACTIVE_PROCESS SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,SERVICE_ERROR_NORMAL, PChar(FilePath),nil,nil,nil,nil,nil); if b <= 0 then begin ShowMessag
e( SysErrorMessage( Exit;
or
GetlastError
));
end; CloseServiceHandle(a); CloseServiceHandle(b); Result except CloseServiceHandle(a); CloseServiceHandle(b); Exit; end; end; := True;
{卸载服务} function DeleteServices(Const var a,b: SC_HANDLE; begin Result := False; a if b if try := a := b
SvrName:
String):
Boolean;
OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCES