Inno Setup: How to abort/terminate setup during install?

后端 未结 4 576
眼角桃花
眼角桃花 2020-12-01 12:19

During my install, I run a bat file. If the bat file returns an error, I need to abort/terminate the setup. I\'d like for it to do a MsgBox telling the user wha

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 13:10

    The problem is that [Run] occurs after the Installation process successfully complete. So you can't cancel at this point, you can only uninstall. Also [Run] does not allow you to get the exit code.

    So you have a few options.

    Use Event: procedure CurStepChanged(CurStep: TSetupStep);

    And the call the {tmp}\test.bat using Exec or ExecAsOriginalUser both of these return the ResultCode. You can then prompt the user to uninstall.

    However I think that performing a cancel would be easier.

    To do that, create an AfterInstall Event on the last file in your project. And execute the program from this event, as you can cancel from this event.

    Here is some sample code that shows how it could be done.

    [Files]
    Source: "MYPROG.EXE"; DestDir: "{app}"; AfterInstall: MyAfterInstall
    
    [Code]
    procedure MyAfterInstall();
    var
     ResCode : Integer;
    begin
     if Exec(ExpandConstant('{tmp}') + '\test.bat',
             '', SW_HIDE, ewWaitUntilTerminated, ResCode) then
     begin
       { Program Ran successfully ResCode now contains exit code results }
    
       { if Exit was 10 then Cancel Installation. }
       if ResCode = 10 then
       begin
          WizardForm.Close;
       end;       
     end
     else
     begin
       { Problem running Program }
       MsgBox('Error', SysErrorMessage(ResCode), mbError, MB_OK);
     end;
    
    end;
    

提交回复
热议问题