How to force Inno Setup setup to fail when Run command fails?

后端 未结 4 2184
悲&欢浪女
悲&欢浪女 2020-12-03 18:23

I have some commands in the [Run] section of my Inno Setup script. Right now, if any of them returns a failure code (non-zero return value), the setup continue

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 18:35

    The [Run] section happens after installation is complete, so there's no rollback possible at that point, because it's already finalized.

    However, what you can do is use AfterInstall in the [Files] section, after your .exe or whatever is required to execute your method. This runs before finalizing the installation, so canceling at this point does a rollback that removes all files.

    If you combine that with the "CancelWithoutPrompt" from this answer you can do a rollback when running in interactive mode. Unfortunately, there 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;
    

提交回复
热议问题