Delphi服務程式註冊與解除安裝

2021-08-27 02:21:45 字數 2194 閱讀 3216

uses winsvc;

function installservice(servicename, displayname, filename: string): boolean;

varscmanager,service: thandle;

args: pchar;

begin

result := false;

scmanager := openscmanager(nil, nil, sc_manager_all_access);

if scmanager = 0 then exit;

try service := createservice(scmanager, //控制代碼

pchar(servicename), //服務名稱

pchar(displayname), //顯示服務名

service_all_access, //服務訪問型別

service_win32_own_process, //服務型別 or service_interactive_process

service_auto_start, //自動啟動服務

service_error_ignore, //忽略錯誤

pchar(filename), //啟動的檔名

nil, //name of load ordering group (載入組名) 'localsystem'

nil, //標籤識別符號

nil, //相關性陣列名

nil, //帳戶(當前)

nil); //密碼(當前)

args := nil;

startservice(service, 0, args);

closeservicehandle(service);

finally

closeservicehandle(scmanager);

end;

result := true;

end;

procedure uninstallservice(servicename: string);

varscmanager,service: thandle;

servicestatus: service_status;

begin

scmanager := openscmanager(nil, nil, sc_manager_all_access);

if scmanager = 0 then exit;

try service := openservice(scmanager, pchar(servicename), service_all_access);

controlservice(service, service_control_stop, servicestatus);

deleteservice(service);

closeservicehandle(service);

finally

closeservicehandle(scmanager);

end;

end;

procedure servicectrlhandler(control: dword); stdcall;

begin

case control of

service_control_stop:

begin

stopped := true;

status.dwcurrentstate := service_stopped;

end;

service_control_pause:

begin

paused := true;

status.dwcurrentstate := service_paused;

end;

service_control_continue:

begin

paused := false;

status.dwcurrentstate := service_running;

end;

service_control_interrogate: ;

service_control_shutdown: stopped := true;

end;

setservicestatus(statushandle, status);

end;

Delphi服務程式註冊與解除安裝

delphi服務程式註冊與解除安裝 uses winsvc function installservice servicename,displayname,filename string boolean varscmanager,service thandle args pchar begin re...

Delphi服務程式註冊與解除安裝

delphi服務程式註冊與解除安裝 uses winsvc function installservice servicename,displayname,filename string boolean varscmanager,service thandle args pchar begin re...

delphi 建立服務,安裝與解除安裝服務

安裝與解除安裝 建立乙個bin檔案,裡面如下檔案 1 install.bat為安裝檔案內容 echo off dbmservice.exe install echo on 點選檔案,即安裝。右擊 計算機 管理 服務和應用程式 服務 找到該服務。啟動服務。2 uninstall.bat為解除安裝檔案內...