Delphi   发布时间:2022-04-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Inno安装 – 在文件复制前正确停止服务大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我们的安装过程包括一个 Windows服务,如果我们的软件被配置为安装为服务器(与客户端安装),则安装该服务.我添加了 a service library能够管理的服务,然后在文件中,我为BeforeInstall和AfterInstall事件添加了处理程序…
[Files]
source: "Myservice.exe"; DestDir: "{app}"; check: IsServer; BeforeInstall: BeforeserviceInstall('Myservicename','Myservice.exe'); AfterInstall: AfterserviceInstall('Myservicename','Myservice.exe')

procedure BeforeserviceInstall(SvcName,Filename: String);
var
  S: Longword;
begin
  //If service is installed,it needs to be stopped
  if serviceExists(SvcName) then begin
    S:= SimpleQueryservice(SvcName);
    if S <> serviCE_STOPPED then begin
      SimpleStopservice(SvcName,True,TruE);
    end;
  end;
end;

procedure AfterserviceInstall(SvcName,Filename: String);
begin
  //If service is not installed,it needs to be installed now
  if not serviceExists(SvcName) then begin
    if SimpleCreateservice(SvcName,'My service Name',ExpandConstant('{app}')+'\' + Filename,serviCE_AUTO_START,'',false,TruE) then begin
      //service successfully installed
      SimpleStartservice(SvcName,TruE);
    end else begin
      //service failed to install

    end;
  end;
end;

当第一次安装服务(不存在并且当前不运行)时,此服务的安装/启动工作正常.但是,在现有安装(升级)上运行此安装程序时,安装程​​序在识别到此服务正在运行时停止,并提示终止进程(在调用BeforeserviceInstall()处理程序之前)…

如何防止此提示出现在服务中?我避免不必要重新启动,并且仍然希望这个提示出现在所有其他文件中.

解决方法

目前没有直接方法来排除文件是否正在使用.您可以全局禁用此控件(通过将 CloseApplications指令值设置为no),我不推荐这样做.或者您可以为文件设置一个过滤器,这将被检查(在 CloseApplicationsFilter指令中),您可能需要这样的过滤器.列出除服务可执行文件之外的所有文件,这很难维护.

您也可以列出所有要检查的文件,方法是指定一个不匹配任何文件的过滤器,并从RegisterExtraCloseApplicationsResources事件方法中添加它们与上述指令相同.

我建议的是从PrepareToInstall事件方法停止您的服务.它的参明确地表明了(我强调的):

在执行所有正在使用的文件检查之前执行此事件方法,并且允许您说出因某些原因停止服务失败时需要重新启动系统.如果您不需要重新启动,您可能只是返回一个字符串,并显示了一些明智的消息.

对于您的脚本,这意味着将代码从BeforeserviceInstall过程移动到PrepareToInstall事件方法,并从条目中删除BeforeInstall参数.

大佬总结

以上是大佬教程为你收集整理的Inno安装 – 在文件复制前正确停止服务全部内容,希望文章能够帮你解决Inno安装 – 在文件复制前正确停止服务所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。