Delphi服务程序注册与卸载

 

//Delphi服务程序注册与卸载

uses winsvc;
function InstallService(ServiceName, DisplayName, FileName: string): boolean;
var
SCManager,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);
var
SCManager,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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值