如何在 Inno Setup 的 post-install 中强制重启

How to force a reboot in post-install in Inno Setup

在我的设置中,我必须安装外部驱动程序。
在极少数情况下安装失败,我必须删除旧驱动程序并重新启动才能重试。

我在ssPostInstall.

安装外接驱动
procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssPostInstall then
  begin
    if Exec(ExpandConstant('{app}\external.exe'), '-install', '', SW_SHOW,
            ewWaitUntilTerminated, ResultCode) then
    begin
      { handle success if necessary; ResultCode contains the exit code }
    end
    else begin
      { handle failure if necessary; ResultCode contains the error code }
      bReboot := true;
    end;
  end;

function NeedRestart(): Boolean;
begin
  Result := bReboot;
end;

不幸的是,这不起作用,因为 NeedRestartssPostInstall 之前被调用。

还有其他方法可以触发重启吗?
我不想设置 AlwaysRestart = yes

我可以让弹出一个 MsgBox 来通知用户并告诉他们该怎么做。但如果能在设置中自动处理就更好了。

您可以更快地进行安装。例如,在 external.exe 安装后立即使用 AfterInstall:

[Files]
Source: "external.exe"; DestDir: "{app}"; AfterInstall: InstallDriver
[Code]
procedure InstallDriver;
begin
  if Exec(ExpandConstant('{app}\external.exe'), '-install', '', SW_SHOW,
          ewWaitUntilTerminated, ResultCode) then
  begin
    { handle success if necessary; ResultCode contains the exit code }
  end
    else
  begin
    { handle failure if necessary; ResultCode contains the error code }
    bReboot := true;
  end;
end;

另一种选择是使用 ssInstall 步骤(甚至 PrepareToInstall event) and extract the file programmatically using ExtractTemporaryFile.


顺便说一句,如果 external.exe 只是一个安装程序,您可能希望将其 "install" 到 {tmp}(以便自动删除)。