Inno Setup - 在文件复制之前正确停止服务

Jer*_*dge 15 service inno-setup

我们的安装过程包括Windows服务,如果我们的软件配置为作为服务器安装(与客户端安装相比),则会安装该服务.我添加了一个服务库以便能够管理服务,然后在文件中添加了处理程序BeforeInstallAfterInstall事件......

[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, True);
    end else begin
      //Service failed to install

    end;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

首次安装服务时(尚未存在且当前未运行),此服务的安装/启动工作正常.但是,在现有安装(升级)上运行此安装程序时,安装程​​序会在识别出此服务正在运行时停止,并提示终止该进程(调用BeforeServiceInstall()处理程序之前)...

提示消息

如何阻止此提示出现在服务中?我不需要重新启动,仍然希望显示所有其他文件的提示.

TLa*_*ama 11

目前没有直接的方法可以排除文件检查文件是否正在使用中.您可以全局禁用此控件(通过将CloseApplications指令值设置为no),我不建议这样做.或者你也可以设置文件过滤器,将检查(在CloseApplicationsFilter指令),这对于您可能需要如列出,除了你的服务的可执行文件,这是很难维持的所有文件.

您还可以通过指定与任何文件都不匹配的过滤器列出要检查的所有文件,并从RegisterExtraCloseApplicationsResources事件方法添加它们与从上述指令执行此操作相同.

我建议的是从PrepareToInstall事件方法中停止服务.它的参考明确暗示了这一点(我强调):

您可以使用此事件功能来检测和安装缺少的先决条件和/ 或关闭任何即将更新的应用程序.

此事件方法在执行所有正在使用的文件检查之前执行,并允许您说出由于某种原因停止服务失败的情况需要重新启动系统.如果您不需要重新启动,您可能只返回一个字符串,其中包含用户发生的一些明智的消息.

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