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
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;
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With