打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Delphi服务程序注册与卸载_weixin
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;
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Delphi把程序注册为服务
delphi 启动、停止服务函数
windwos 2000及NT 服务注册的具体实现
在WIN2000环境下编制Service
delphi 虚拟桌面原理及实现
检测远程文件存在
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服