日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

编程控制系统服务

發布時間:2023/12/15 windows 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编程控制系统服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
工作中可能需要多次啟動、停止某項服務(程序),若每次都(計算機管理--->服務和應用程序--->服務)手動操作,未免有點繁瑣... {**************************************************************************************}
{ 單元作者: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

總結

以上是生活随笔為你收集整理的编程控制系统服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。