当Run命令失败时,如何强制Inno Setup设置失败?

时间:2009-07-13 23:06:30

标签: inno-setup

我在Inno Setup脚本的[Run]部分有一些命令。现在,如果它们中的任何一个返回失败代码(非零返回值),则设置继续而不向用户发出任何警告。所需的行为是让安装失败并回滚。

如何启用此功能?我找不到任何会强制此行为的Run条目的标记。我错过了什么吗?

3 个答案:

答案 0 :(得分:9)

就我而言,您必须使用[Code]部分,使用Exec函数运行文件,在返回时检查ResultCode并运行卸载脚本。< / p>

答案 1 :(得分:4)

我是这样做的:

  1. 使用Inno Setup的{tmp}\install.error参数和BeforeInstall过程将错误消息(中止确认消息或仅通知消息)写入临时文件SaveStringToUTF8File。您可以使用Inno Setup的常量,例如{cm:YourCustomMessage}

  2. 使用Windows命令shell cmd.exe /s /c运行所需的程序。还可以使用del - https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx条件执行&&命令。因此,如果命令成功,则将删除错误消息文件(退出代码0)。请注意cmd.exe /s /c中的特殊报价处理。以下面的代码为例。

  3. 根据错误严重程度,使用Inno Setup的{tmp}\install.error参数和AfterInstallConfirmInstallAbortOnError程序检查错误消息文件NotifyInstallAbortOnError是否存在。他们将通过适当的通知或确认(以及可选的日志文件显示)中止安装并使用Exec(ExpandConstant('{uninstallexe}'), ...

    执行回滚
  4. ShouldAbortInstallation全局变量用于保持状态。 Inno Setup的ShouldSkipPage(PageID: Integer)功能用于隐藏最终页面。 [Run]部分中的所有命令都应使用带有Check功能的CheckInstallationIsNotAborted参数。它会阻止它们在某些时候失败后执行。

  5. 见下面的例子。希望这会有所帮助。

    [CustomMessages]
    InstallAbortOnErrorConfirmationMessage=An error has occurred during setup.%nAbort installation?
    InstallAbortOnErrorNotificationMessage=An error has occurred during setup.%nInstallation will be aborted.
    RunProgram1ErrorMsg=Post installation phase 1 failed. Should abort install?
    RunProgram2ErrorMsg=Post installation phase 2 failed. Installation will be aborted. Please, contact tech support.
    RunProgram1StatusMsg=Post installation phase 1 is in progress
    RunProgram2StatusMsg=Post installation phase 2 is in progress
    
    [Run]
    ; Write error text to file. Delete file on succeed. Abort installation if file exists after command execution.
    Filename: "cmd.exe"; Parameters: "/s /c "" ""{app}\program1.exe"" /param1 /param2:""val2"" && del /F /Q ""{tmp}\install.error"" """; \
      WorkingDir:"{app}"; Flags: runhidden; \
      BeforeInstall: SaveStringToUTF8File('{tmp}\install.error', '{cm:RunProgram1ErrorMsg}', False); \
      AfterInstall: ConfirmInstallAbortOnError('{tmp}\install.error', '{app}\logs\setup.log'); \
      StatusMsg: "{cm:RunProgram1StatusMsg}"; \
      Check: CheckInstallationIsNotAborted;
    Filename: "cmd.exe"; Parameters: "/s /c "" ""{app}\program2.exe"" && del /F /Q ""{tmp}\install.error"" """; \
      WorkingDir:"{app}"; Flags: runhidden; \
      BeforeInstall: SaveStringToUTF8File('{tmp}\install.error', '{cm:RunProgram2ErrorMsg}', False); \
      AfterInstall: NotifyInstallAbortOnError('{tmp}\install.error', '{app}\logs\setup.log'); \
      StatusMsg: "{cm:RunProgram2StatusMsg}"; \
      Check: CheckInstallationIsNotAborted;
    
    [Code]
    var
      ShouldAbortInstallation: Boolean;
    
    procedure SaveStringToUTF8File(const FileName, Content: String; const Append: Boolean);
    var
      Text: array [1..1] of String;
    begin
      Text[1] := Content;
      SaveStringsToUTF8File(ExpandConstant(FileName), Text, Append);
    end;
    
    function LoadAndConcatStringsFromFile(const FileName: String): String;
    var
      Strings: TArrayOfString;
      i: Integer;
    begin
      LoadStringsFromFile(FileName, Strings);
      Result := '';
      if High(Strings) >= Low(Strings) then
        Result := Strings[Low(Strings)];
      for i := Low(Strings) + 1 to High(Strings) do
        if Length(Strings[i]) > 0 then
          Result := Result + #13#10 + Strings[i];
    end;
    
    procedure ConfirmInstallAbortOnError(ErrorMessageFile, LogFileToShow: String);
    var
      ErrorCode: Integer;
      ErrorMessage: String;
    begin
      ErrorMessageFile := ExpandConstant(ErrorMessageFile);
      LogFileToShow := ExpandConstant(LogFileToShow);
    
      Log('ConfirmInstallAbortOnError is examining file: ' + ErrorMessageFile);
      if FileExists(ErrorMessageFile) then
      begin
        Log('ConfirmInstallAbortOnError: error file exists');
    
        { Show log file to the user }
        if Length(LogFileToShow) > 0 then
          ShellExec('', LogFileToShow, '', '', SW_SHOW, ewNoWait, ErrorCode);
    
        ErrorMessage := LoadAndConcatStringsFromFile(ErrorMessageFile);
        if Length(ErrorMessage) = 0 then
          ErrorMessage := '{cm:InstallAbortOnErrorConfirmationMessage}';
        if MsgBox(ExpandConstant(ErrorMessage), mbConfirmation, MB_YESNO) = IDYES then
        begin
          Log('ConfirmInstallAbortOnError: should abort');
          ShouldAbortInstallation := True;
          WizardForm.Hide;
          MainForm.Hide;
          Exec(ExpandConstant('{uninstallexe}'), '/SILENT', '', SW_HIDE,
               ewWaitUntilTerminated, ErrorCode);
          MainForm.Close;
        end;
      end;
      Log('ConfirmInstallAbortOnError finish');
    end;
    
    procedure NotifyInstallAbortOnError(ErrorMessageFile, LogFileToShow: String);
    var
      ErrorCode: Integer;
      ErrorMessage: String;
    begin
      ErrorMessageFile := ExpandConstant(ErrorMessageFile);
      LogFileToShow := ExpandConstant(LogFileToShow);
    
      Log('NotifyInstallAbortOnError is examining file: ' + ErrorMessageFile);
      if FileExists(ErrorMessageFile) then
      begin
        Log('NotifyInstallAbortOnError: error file exists');
    
        { Show log file to the user }
        if Length(LogFileToShow) > 0 then
          ShellExec('', LogFileToShow, '', '', SW_SHOW, ewNoWait, ErrorCode);
    
        ErrorMessage := LoadAndConcatStringsFromFile(ErrorMessageFile);
        if Length(ErrorMessage) = 0 then
          ErrorMessage := '{cm:InstallAbortOnErrorNotificationMessage}';
    
        MsgBox(ExpandConstant(ErrorMessage), mbError, MB_OK);
        Log('NotifyInstallAbortOnError: should abort');
        ShouldAbortInstallation := True;
        WizardForm.Hide;
        MainForm.Hide;
        Exec(ExpandConstant('{uninstallexe}'), '/SILENT', '', SW_HIDE,
             ewWaitUntilTerminated, ErrorCode);
        MainForm.Close;
      end;
      Log('NotifyInstallAbortOnError finish');
    end;
    
    function ShouldSkipPage(PageID: Integer): Boolean;
    begin
      Result := ShouldAbortInstallation;
    end;
    
    function CheckInstallationIsNotAborted(): Boolean;
    begin
      Result := not ShouldAbortInstallation;
    end;
    

答案 2 :(得分:1)

[Run]部分在安装完成后发生,因此此时无法回滚,因为它已经完成。

但是, 可以做的是在[Files]之后或执行方法所需的任何地方使用.exe部分中的AfterInstall。这会在完成安装之前运行,因此,此时取消将执行回滚操作,以删除所有文件。

如果将其与"CancelWithoutPrompt" from this answer结合使用,则可以以交互模式运行时回滚 。不幸的是,有doesn't seem to be a rollback for silent mode

[Files]
Source: src\myapp.exe; DestDir: "{app}"; AfterInstall: RunMyAppCheck

[Code]
var CancelWithoutPrompt: boolean;

function InitializeSetup(): Boolean;
begin
  CancelWithoutPrompt := false;
  result := true;
end;

procedure CancelButtonClick(CurPageID: Integer; var Cancel, Confirm: Boolean);
begin
  if CancelWithoutPrompt then
    Confirm := false; { hide confirmation prompt }
end;

procedure RunMyAppCheck();
var
  resultCode: int;
begin
  Exec(ExpandConstant('{app}\myapp.exe'), '--verify --example-other-params',
    '', SW_HIDE, ewWaitUntilTerminated, resultCode);
  
  if resultCode <> 0 then
  begin
    SuppressibleMsgBox(
      'MyApp failed, exit code ' + IntToStr(resultCode) + '. Aborting installation.',
      mbCriticalError, MB_OK, 0);
  
    CancelWithoutPrompt := true;
    WizardForm.Close;
  end;
end;