Inno Setup-在复制文件之前正确停止服务 [英] Inno Setup - properly stop service before file copy

查看:393
本文介绍了Inno Setup-在复制文件之前正确停止服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Our installation process includes a Windows Service which is installed if our software is configured to be installed as a server (vs. a client installation). I added a service library to be able to manage the services, then in the files, I added handlers for BeforeInstall and AfterInstall events...

[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;

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

When installing the service for the first time (doesn't already exist and isn't currently running), the installation/starting of this service works just fine. However, when running this installer on an existing installation (upgrade), the installer stops when it recognizes that this service is running, and prompts to terminate the process (before it calls the BeforeServiceInstall() handler)...

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

How do I prevent this prompt from appearing for services? I'm avoiding having to require a restart and would still like this prompt to appear for all other files.

推荐答案

当前尚无直接方法可以从文件中排除文件是否正在使用中.您可以全局禁用此控件(通过将 CloseApplications 指令值设置为no),我不建议这样做.或者,您可以为文件设置过滤器,将对其进行检查(在 CloseApplicationsFilter 指令),您可能需要使用例如列出除服务可执行文件之外的所有文件,这些文件很难维护.

There is currently no direct way to exclude a file from checking if it's in use. You can disable this control globally (by setting CloseApplications directive value to no), which I wouldn't recommend. Or you can set a filter for files, which will be checked (in the CloseApplicationsFilter directive), which for you might require e.g. to list all the files except your service executable, which is hard to maintain.

您还可以通过指定一个与您的任何文件都不匹配的过滤器列出所有要检查的文件,然后从 RegisterExtraCloseApplicationsResources 事件方法与上述指令中的操作方法相同.

You may also list all the files to be checked by specifying a filter which won't match any of your files and adding them from the RegisterExtraCloseApplicationsResources event method is the same as doing this from the mentioned directive.

我建议您从 PrepareToInstall 事件方法.它的参考明确暗示了这一点(由我强调):

What I would suggest is to stop your service from the PrepareToInstall event method. Its reference explicitly suggests this (emphasized by me):

您可以使用此事件功能来检测并安装丢失的事件 先决条件和/或关闭将要关闭的任何应用程序 进行更新.

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

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

This event method is executed before all the file in use checks are performed and allows you to say that you need a system restart for cases when stopping of your service fails for some reason. If you wouldn't require restart, you may just return a string with some sensible message what happened to the user.

对于您的脚本,这仅意味着将代码从BeforeServiceInstall过程移动到

For your script it would just mean to move the code from your BeforeServiceInstall procedure to the PrepareToInstall event method and remove the BeforeInstall parameter from the entry.

这篇关于Inno Setup-在复制文件之前正确停止服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆