微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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事件方法停止您的服务.它的参考明确地表明了(我强调的):

You can use this event function to detect and install missing
prerequisites and/or to shutdown any application which is about to
be updated
.

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

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

原文地址:https://www.jb51.cc/delphi/101469.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐