Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 what happened, then for the abort to look and act like the user pressed the Cancel button.

Is it possible to abort/terminate the setup?

Code examples would be really appreciated.

[Run] Filename: {tmp}\test.bat; WorkingDir: {tmp}; Flags: waituntilterminated runhidden 
like image 317
PM2 Avatar asked Jun 14 '11 15:06

PM2


2 Answers

Thank you, Robert. It is a common problem happening any time when script detects that setup cannot be continued. However, there is a problem in your solution. WizardForm.Close invokes cancel dialog, and installation is stopped only if user answers "Yes". To exit definitely, we should invoke CancelButtonClick.

[Files] Source: "MYPROG.EXE"; DestDir: "{app}"; AfterInstall: MyAfterInstall  [Code] var CancelWithoutPrompt: boolean;  function InitializeSetup(): Boolean; begin   CancelWithoutPrompt := false;   result := true; end;  procedure MyAfterInstall(); begin   { Do something }   if BadResult then begin     MsgBox('Should cancel because...',mbError,MB_OK)     CancelWithoutPrompt := true;     WizardForm.Close;   end; end;  procedure CancelButtonClick(CurPageID: Integer; var Cancel, Confirm: Boolean); begin   if CurPageID=wpInstalling then     Confirm := not CancelWithoutPrompt; end; 
like image 168
Mahris Avatar answered Sep 22 '22 12:09

Mahris


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; 
like image 42
Robert Love Avatar answered Sep 20 '22 12:09

Robert Love