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
I've used some code from answers here to compose complete solution to run commands safely in [Run] section with proper notification and rollback on error.
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
usingExec
orExecAsOriginalUser
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.
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.
Just to round out the other possibilities:
If you can check the prerequisite condition before gathering any information from the user, then the best place to do the check is in an
InitializeSetup
function. This allows you to display aMsgBox
and then exit withResult := False
to abort the installation.If you need to gather some information from the user first (such as the installation directory) but can still check the condition without installing any files (other than perhaps a few via
ExtractTemporaryFile
), then the best place is in thePrepareToInstall
function. This allows you to display an error message (by returning it), at which point the user can either go Back and correct something or exit the installation themselves.If the condition you're checking relates specifically to the user selection on a particular page (again, such as the target directory), and you can do the check quickly and without altering the user's system at all, then it's best to handle that in
NextButtonClick
; you can display aMsgBox
with the error and then returnFalse
to prevent moving on to the next page.If you have to wait until after installing everything else, then it's kinda too late to exit the installation, but if you want to do that anyway then Robert's answer will suffice.