编程控制系统服务
工作中可能需要多次啟動、停止某項服務(程序),若每次都(計算機管理--->服務和應用程序--->服務)手動操作,未免有點繁瑣... {**************************************************************************************}
{ 單元作者:Simon.Hu/ADelphiCoder?????????????????????????????????????????????????????? }
{ 創建日期:2009/10/12????????????????????????????????????????????????????????????????????????? }
{ 參考說明:經修改部分網上源碼而成??????????????????????????????????????????????????? ? }
{ 測試環境:Delphi 7 SP1 + WinXP SP3????????????????????????????????????????????????? }
{ 注意事項:程序中所指的"服務名"是指某項服務的"服務名稱",非其"描述"?}
{**************************************************************************************} unit unt_SvcCtrl;
???
interface uses
? SysUtils, Windows, Messages, Winsvc, Dialogs;
???
function MyStartService(const SvcName: string): Boolean;
function MyStopService(const SvcName: string): Boolean;
function MyQueryServiceState(const SvcName: string): string;
function MyCreateService(const SvcName, FilePath: String): Boolean;
function MyDeleteService(const SvcName: string): Boolean; implementation { 開啟服務 }
function MyStartService(const SvcName: string): Boolean;
var
? SH1, SH2 : SC_HANDLE;
? PC?????? : PChar;
begin
? Result := False; SH1 := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
? if SH1 <= 0 then
??? Exit; SH2 := OpenService(SH1, PChar(SvcName), SERVICE_ALL_ACCESS);
? if SH2 <= 0 then
??? Exit; try
??? Result := StartService(SH2, 0, PC);
???
??? CloseServiceHandle(SH2);
??? CloseServiceHandle(SH1);
? except
??? CloseServiceHandle(SH2);
??? CloseServiceHandle(SH1);
???
??? Exit;
? end;
end; { 停止服務 }
function MyStopService(const SvcName: string): Boolean;
var
? SH1, SH2 : SC_HANDLE;
? SvcState : TServiceStatus;
begin
? Result := False;
???
? SH1 := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
? if SH1 <= 0 then
??? Exit; SH2 := OpenService(SH1, PChar(SvcName), SERVICE_ALL_ACCESS);
? if SH2 <= 0 then
??? Exit; try
??? Result := ControlService(SH2, SERVICE_CONTROL_STOP, SvcState);
???
??? CloseServiceHandle(SH1);
??? CloseServiceHandle(SH2);
? except
??? CloseServiceHandle(SH1);
??? CloseServiceHandle(SH2);
???
??? Exit;
? end;
end; { 查詢當前服務的狀態 }
function MyQueryServiceState(const SvcName: string): string;
var
? SH1, SH2 : SC_HANDLE;
? SvcState : TServiceStatus;
begin??
? Result := '未安裝';
???
? SH1 := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
? if SH1 <= 0 then
??? Exit; SH2 := OpenService(SH1, PChar(SvcName), SERVICE_ALL_ACCESS);
? if SH2 <= 0 then
??? Exit; try
??? QueryServiceStatus(SH2, SvcState);
???
??? if SvcState.dwCurrentState = SERVICE_RUNNING then
????? Result := '啟動'?????? //Run
??? else if SvcState.dwCurrentState = SERVICE_RUNNING then
????? Result := 'Wait'?????? //Runing
??? else if SvcState.dwCurrentState = SERVICE_START_PENDING then
????? Result := 'Wait'?????? //Pause
??? else if SvcState.dwCurrentState = SERVICE_STOP_PENDING then
????? Result := '停止'?????? //Pause
??? else if SvcState.dwCurrentState = SERVICE_PAUSED then
????? Result := '暫停'?????? //Pause
??? else if SvcState.dwCurrentState = SERVICE_STOPPED then
????? Result := '停止'?????? //Stop
??? else if SvcState.dwCurrentState = SERVICE_CONTINUE_PENDING then
????? Result := 'Wait'?????? //Pause
??? else if SvcState.dwCurrentState = SERVICE_PAUSE_PENDING then
????? Result := 'Wait';????? //Pause CloseServiceHandle(SH1);
??? CloseServiceHandle(SH2);
? except
??? CloseServiceHandle(SH1);
??? CloseServiceHandle(SH2);
???
??? Exit;
? end;
end; { 建立服務 }
function MyCreateService(const SvcName, FilePath: string): Boolean;
var??
? SH1, SH2 : SC_HANDLE;
begin
? Result := False; if FilePath = '' then
??? Exit; SH1 := OpenSCManager(nil, nil, SC_MANAGER_CREATE_SERVICE);
? if SH1 <= 0 then
??? Exit; try
??? SH2 := CreateService(
???????????????????????? SH1,
???????????????????????? PChar(SvcName),
???????????????????????? PChar(SvcName),
???????????????????????? 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 SH2 <= 0 then
??? begin
????? ShowMessage(SysErrorMessage(GetlastError));
????? Exit;
??? end;
???
??? CloseServiceHandle(SH1);
??? CloseServiceHandle(SH2); Result := True;
? except
??? CloseServiceHandle(SH1);
??? CloseServiceHandle(SH2);
???
??? Exit;
? end;
end; { 卸載服務 }
function MyDeleteService(const SvcName: string): Boolean;
var
? SH1, SH2 : SC_HANDLE;
begin
? Result := False; SH1 := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
? if SH1 <= 0 then
??? Exit; SH2 := OpenService(SH1, PChar(SvcName), STANDARD_RIGHTS_REQUIRED);
? if SH2 <= 0 then
??? Exit; try
??? Result := DeleteService(SH2); if not Result then
????? ShowMessage(SysErrorMessage(GetlastError)); CloseServiceHandle(SH2);
??? CloseServiceHandle(SH1);
? except
??? CloseServiceHandle(SH2);
??? CloseServiceHandle(SH1);
???
??? Exit;
? end;
end; end.
{ 單元作者:Simon.Hu/ADelphiCoder?????????????????????????????????????????????????????? }
{ 創建日期:2009/10/12????????????????????????????????????????????????????????????????????????? }
{ 參考說明:經修改部分網上源碼而成??????????????????????????????????????????????????? ? }
{ 測試環境:Delphi 7 SP1 + WinXP SP3????????????????????????????????????????????????? }
{ 注意事項:程序中所指的"服務名"是指某項服務的"服務名稱",非其"描述"?}
{**************************************************************************************} unit unt_SvcCtrl;
???
interface uses
? SysUtils, Windows, Messages, Winsvc, Dialogs;
???
function MyStartService(const SvcName: string): Boolean;
function MyStopService(const SvcName: string): Boolean;
function MyQueryServiceState(const SvcName: string): string;
function MyCreateService(const SvcName, FilePath: String): Boolean;
function MyDeleteService(const SvcName: string): Boolean; implementation { 開啟服務 }
function MyStartService(const SvcName: string): Boolean;
var
? SH1, SH2 : SC_HANDLE;
? PC?????? : PChar;
begin
? Result := False; SH1 := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
? if SH1 <= 0 then
??? Exit; SH2 := OpenService(SH1, PChar(SvcName), SERVICE_ALL_ACCESS);
? if SH2 <= 0 then
??? Exit; try
??? Result := StartService(SH2, 0, PC);
???
??? CloseServiceHandle(SH2);
??? CloseServiceHandle(SH1);
? except
??? CloseServiceHandle(SH2);
??? CloseServiceHandle(SH1);
???
??? Exit;
? end;
end; { 停止服務 }
function MyStopService(const SvcName: string): Boolean;
var
? SH1, SH2 : SC_HANDLE;
? SvcState : TServiceStatus;
begin
? Result := False;
???
? SH1 := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
? if SH1 <= 0 then
??? Exit; SH2 := OpenService(SH1, PChar(SvcName), SERVICE_ALL_ACCESS);
? if SH2 <= 0 then
??? Exit; try
??? Result := ControlService(SH2, SERVICE_CONTROL_STOP, SvcState);
???
??? CloseServiceHandle(SH1);
??? CloseServiceHandle(SH2);
? except
??? CloseServiceHandle(SH1);
??? CloseServiceHandle(SH2);
???
??? Exit;
? end;
end; { 查詢當前服務的狀態 }
function MyQueryServiceState(const SvcName: string): string;
var
? SH1, SH2 : SC_HANDLE;
? SvcState : TServiceStatus;
begin??
? Result := '未安裝';
???
? SH1 := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
? if SH1 <= 0 then
??? Exit; SH2 := OpenService(SH1, PChar(SvcName), SERVICE_ALL_ACCESS);
? if SH2 <= 0 then
??? Exit; try
??? QueryServiceStatus(SH2, SvcState);
???
??? if SvcState.dwCurrentState = SERVICE_RUNNING then
????? Result := '啟動'?????? //Run
??? else if SvcState.dwCurrentState = SERVICE_RUNNING then
????? Result := 'Wait'?????? //Runing
??? else if SvcState.dwCurrentState = SERVICE_START_PENDING then
????? Result := 'Wait'?????? //Pause
??? else if SvcState.dwCurrentState = SERVICE_STOP_PENDING then
????? Result := '停止'?????? //Pause
??? else if SvcState.dwCurrentState = SERVICE_PAUSED then
????? Result := '暫停'?????? //Pause
??? else if SvcState.dwCurrentState = SERVICE_STOPPED then
????? Result := '停止'?????? //Stop
??? else if SvcState.dwCurrentState = SERVICE_CONTINUE_PENDING then
????? Result := 'Wait'?????? //Pause
??? else if SvcState.dwCurrentState = SERVICE_PAUSE_PENDING then
????? Result := 'Wait';????? //Pause CloseServiceHandle(SH1);
??? CloseServiceHandle(SH2);
? except
??? CloseServiceHandle(SH1);
??? CloseServiceHandle(SH2);
???
??? Exit;
? end;
end; { 建立服務 }
function MyCreateService(const SvcName, FilePath: string): Boolean;
var??
? SH1, SH2 : SC_HANDLE;
begin
? Result := False; if FilePath = '' then
??? Exit; SH1 := OpenSCManager(nil, nil, SC_MANAGER_CREATE_SERVICE);
? if SH1 <= 0 then
??? Exit; try
??? SH2 := CreateService(
???????????????????????? SH1,
???????????????????????? PChar(SvcName),
???????????????????????? PChar(SvcName),
???????????????????????? 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 SH2 <= 0 then
??? begin
????? ShowMessage(SysErrorMessage(GetlastError));
????? Exit;
??? end;
???
??? CloseServiceHandle(SH1);
??? CloseServiceHandle(SH2); Result := True;
? except
??? CloseServiceHandle(SH1);
??? CloseServiceHandle(SH2);
???
??? Exit;
? end;
end; { 卸載服務 }
function MyDeleteService(const SvcName: string): Boolean;
var
? SH1, SH2 : SC_HANDLE;
begin
? Result := False; SH1 := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
? if SH1 <= 0 then
??? Exit; SH2 := OpenService(SH1, PChar(SvcName), STANDARD_RIGHTS_REQUIRED);
? if SH2 <= 0 then
??? Exit; try
??? Result := DeleteService(SH2); if not Result then
????? ShowMessage(SysErrorMessage(GetlastError)); CloseServiceHandle(SH2);
??? CloseServiceHandle(SH1);
? except
??? CloseServiceHandle(SH2);
??? CloseServiceHandle(SH1);
???
??? Exit;
? end;
end; end.
轉載于:https://blog.51cto.com/adelphicoder/214665
總結
- 上一篇: 限制 TEdit 只能接收 数字 输入.
- 下一篇: Windows 7技巧之Telnet组件