Inno Setup: Error handling in [Run] section [duplicate] - inno-setup

This question already has answers here:
How to force Inno Setup setup to fail when Run command fails?
(4 answers)
Using Process Exit code to show error message for a specific File in [Run]
(2 answers)
Closed 2 years ago.
I've been creating my first installers with Inno Setup and while they work, I want to look into some more profound error handling in case things go south during installation.
Here are the script snippets containing what I have so far. All of this works, questions follow below.
[Components]
Name: "base"; Description: "Install base files needed for this application"; Types: full compact custom; Flags: fixed
Name: "fonts"; Description: "Install Spartan fonts for a correct rendering of the application"; Types: full custom
Name: "updater"; Description: "Create a scheduled task to run the component updater on a daily basis"; Types: full custom
[Tasks]
Name: "update"; Description: "Run the updater after the installation to bring all application files up to date"
[Files]
; Application files
Source: "{#MyAppSourceNetwork1}\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion; Components: base
Source: "{#MyAppSourceNetwork1}\{#MyAppcfgName}"; DestDir: "{app}"; Flags: ignoreversion; Components: base
Source: "{#MyAppSourceNetwork1}\Source\Componenten_COM\*"; DestDir: "{app}\Componenten_COM"; Flags: ignoreversion recursesubdirs createallsubdirs; Components: base
Source: "{#MyAppSourceNetwork1}\Source\Componenten_RegAsm\*"; DestDir: "{app}\Componenten_RegAsm"; Flags: ignoreversion recursesubdirs createallsubdirs; Components: base
Source: "{#MyAppSourceNetwork2}\*"; DestDir: "{#MyAppLocalFolderLib}\OCXinstaller"; Flags: ignoreversion recursesubdirs createallsubdirs; Components: base
; Updater
Source: "{#MyAppSource}\Updater\*"; DestDir: "{#MyAppLocalFolderLib}\Updater"; Flags: ignoreversion recursesubdirs createallsubdirs; Components: base
[Run]
; Create scheduled task for updater
Filename: "powershell"; \
Parameters: "-Executionpolicy Bypass -NoProfile -WindowStyle Hidden -Command ""Register-ScheduledTask -TaskName '{#MyAppTSUpdaterName}' -Description 'Smartclient component updater' -User System -Action (New-ScheduledTaskAction -Execute wscript -Argument '{#MyAppLocalFolderLib}\Updater\{#MyAppUpdateScript}') -Trigger (New-ScheduledTaskTrigger -Daily -At 07:00) -Settings (New-ScheduledTaskSettingsSet -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -DontStopOnIdleEnd -ExecutionTimeLimit (New-TimeSpan -Hours 2) -MultipleInstances IgnoreNew)"""; \
Description: "Create scheduled task for updater"; \
StatusMsg: "Creating scheduled task ""{#MyAppTSUpdaterName}""..."; \
Flags: runhidden; \
Components: updater
; Run scheduled task for updater
Filename: "wscript"; \
Parameters: """{#MyAppLocalFolderLib}\Updater\{#MyAppUpdateScript}"""; \
Description: "Run Smartclient component updater"; \
StatusMsg: "Running Smartclient updater. Please wait, this will take a few minutes..."; \
Flags: runhidden; \
BeforeInstall: SetMarqueeProgress(True); \
AfterInstall: SetMarqueeProgress(False); \
Tasks: update
; Offer option to launch application after installation
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent unchecked
[Code]
// Show infinite progress bar while waiting
procedure SetMarqueeProgress(Marquee: Boolean);
begin
if Marquee then
begin
WizardForm.ProgressGauge.Style := npbstMarquee;
end
else
begin
WizardForm.ProgressGauge.Style := npbstNormal;
end;
end;
For the purpose of testing, I deliberatly wrote some errors in the commands (RRRegister-ScheduledTask / UUUpdater / NNNotepad) and in the log file below, you can clearly see that the commands do not return code 0.
During the installation I got message boxes for the wscript and notepad commands, making clear that something went wrong, with the return code and an OK button. However, the Powershell command doesn't give a message at all. It only returns an error when I write the command wrong (PPPowershell.exe) but not when there is an error in the Parameters flag. Any idea why? Because I'm sure Powershell returns some nice red lines indicating an error occured.
Additionally, I notice that Inno Setup writes in the log that the installation succeeded (7th line in my snippet) although it even still needs to start with the [Run] section. When I write the commands without typos, the setup does everything correctly, but it doesn't give me much confidence in how my installers might indicate success even when some actions didn't succeed. Why isn't the [Run] block considered to be an integral part if the installation?
2020-06-10 08:52:15.025 Saving uninstall information.
2020-06-10 08:52:15.025 Deleting uninstall key left over from previous administrative 32-bit install.
2020-06-10 08:52:15.027 Creating new uninstall key: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\{C3DAA6E8-7152-40C7-8487-F4A1BDC3EB92}_is1
2020-06-10 08:52:15.027 Writing uninstall key values.
2020-06-10 08:52:15.030 Detected previous non administrative install? No
2020-06-10 08:52:15.030 Detected previous administrative 64-bit install? No
2020-06-10 08:52:15.047 Installation process succeeded.
2020-06-10 08:52:15.052 -- Run entry --
2020-06-10 08:52:15.052 Run as: Current user
2020-06-10 08:52:15.053 Type: Exec
2020-06-10 08:52:15.053 Filename: powershell
2020-06-10 08:52:15.053 Parameters: -Executionpolicy Bypass -NoProfile -WindowStyle Hidden -Command "RRRegister-ScheduledTask -TaskName 'Smartclient Updater' -Description 'Smartclient component updater' -User System -Action (New-ScheduledTaskAction -Execute wscript -Argument 'C:\Smartclient_MH_LIB\Updater\Mediahuis_Smartclient_Components_Updater.vbs') -Trigger (New-ScheduledTaskTrigger -Daily -At 07:00) -Settings (New-ScheduledTaskSettingsSet -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -DontStopOnIdleEnd -ExecutionTimeLimit (New-TimeSpan -Hours 2) -MultipleInstances IgnoreNew)"
2020-06-10 08:52:19.509 Process exit code: 1
2020-06-10 08:52:19.511 -- Run entry --
2020-06-10 08:52:19.511 Run as: Current user
2020-06-10 08:52:19.511 Type: Exec
2020-06-10 08:52:19.511 Filename: wscript
2020-06-10 08:52:19.511 Parameters: "C:\Smartclient_MH_LIB\UUUpdater\Mediahuis_Smartclient_Components_Updater.vbs"
2020-06-10 08:57:47.994 Process exit code: 1
2020-06-10 08:57:47.996 -- Run entry --
2020-06-10 08:57:47.996 Run as: Current user
2020-06-10 08:57:47.996 Type: Exec
2020-06-10 08:57:47.996 Filename: NNNotepad
2020-06-10 08:57:47.997 Exception message:
2020-06-10 08:57:47.997 Message box (OK):
Unable to execute file:
NNNotepad
CreateProcess failed; code 2.
The system cannot find the file specified.
2020-06-10 08:57:50.009 User chose OK.
So where should I take this from here to be able to handle the return codes of these commands?
How can I specify which return codes from a [Run] line should be considered fatal, and which not? How do I catch them? I understand I will probably need to write [Code] blocks, maybe using Exec? But how do I trigger them from a [Run] line? Using Check or something else?
Can someone give an example how to catch the return code from the Powershell or Wscript command and how to throw a message box which manipulates the behaviour of the installer (e.g. quit if you click Abort, continue if you click Ignore)?
How do you manipulate the exit code from the setup itself in case things go wrong? Or is this a big no-no?
I read things about Windows process exit codes that are different than return codes in Pascal scripting. This is confusing the hell out of me. Can someone explain?
I've been looking into many examples and similar topics, but couldn't really find a useful case which I could edit to suit my needs.
Thanks for reading this far and for any help / insight you can offer.
EDIT 2020/06/16
Sorry for the delay. Meanwhile the topic has been closed, but I wanted to let you know how I solved it eventually and that I'm grateful for the help you offered. With the suggestions provided by Martin Prikryl and some additional searching I was able to reach my goals: write a [Code] block which lets me perform additional tasks, catch the return code and override the Setup exitcode if I wish to. I will post my generic code example below for anyone who needs a similar solution.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; // Example - Run additional actions purely in the [Code] block during the ssPostInstall step (executed after the [Run] block) and use ExitCode to modify the default exit code of the setup
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[Tasks]
Name: "runscript"; Description: "Run a script"
[Code]
// Start with exmpty exit code, can be used to override setup exit code with GetCustomSetupExitCode
var
ExitCode: Integer;
// Show infinite progress bar while waiting
procedure SetMarqueeProgress(Marquee: Boolean);
begin
if Marquee then
begin
WizardForm.ProgressGauge.Style := npbstMarquee;
end
else
begin
WizardForm.ProgressGauge.Style := npbstNormal;
end;
end;
// Run post-installation commands
procedure CurStepChanged(CurStep: TSetupStep);
var
ResultCode: Integer;
ProgramNAme: String;
ProgramArguments: String;
begin
if CurStep = ssPostInstall then
begin
// Change progressbar
SetMarqueeProgress(True);
// Run script if task is selected
if IsTaskSelected('runscript') then
begin
WizardForm.StatusLabel.Caption := 'Running script. This may take a few minutes...';
ProgramName := 'wscript';
ProgramArguments := ExpandConstant('{sd}\Smartclient_MH_LIB') + '\script\{#MyAppUpdateScript}';
Log('Running ' + ProgramName + ' ' + ProgramArguments);
if Exec(ProgramName, ProgramArguments, '', SW_HIDE, ewWaitUntilTerminated, ResultCode) then
begin
Log('ResultCode = ' + IntToStr(ResultCode) + '.');
if ResultCode = 0 then
begin
Log('Setup successfully ran the script.')
end
else
begin
Log('Setup failed to run the script.');
ExitCode := ExitCode + 1;
end;
end;
end
else
begin
Log('Skipping update because task "update" is not selected.');
end;
// Change progressbar
SetMarqueeProgress(False);
end;
end;
// Override setup exit code
function GetCustomSetupExitCode: Integer;
begin
if ExitCode <> 0 then
begin
Log('Returning custom exit code ' + IntToStr(ExitCode));
end;
Result := ExitCode;
end;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Related

How to run program or a batch file from Code section of Inno Setup? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I insert Run (unzip.exe and a batch file) within the Code section instead of Run? I attempted the approach used here Inno Setup: Install other installer and run it before continuing my install but unable to get it to work so I reverted to using the Run section to run two scripts. What I have done so far appears sloppy. The Inno Setup "finished" page displays an option checkbox to run the batch script, whereas I'd prefer it to run automatically before reaching this stage.
[Setup]
PrivilegesRequired=admin
[Files]
Source: "CC.exe"; DestDir: "{pf}\CC"; DestName: "CC.exe"
Source: "bbb.update.zip"; DestDir: "{userdesktop}"; Flags: deleteafterinstall
Source: "unzip.exe"; DestDir: "{userdesktop}"; Flags: deleteafterinstall
[Run]
Filename: "{userdesktop}\unzip.exe"; \
Parameters: "x {userdesktop}\bbb.update.zip -d {userdesktop}"; \
Flags: runascurrentuser nowait
Filename: "{userdesktop}\update.bat"; \
Flags: runascurrentuser nowait postinstall skipifsilent
(the update.bat file cleans up after installing)
Use Exec function. For example in CurStepChanged event function.
Also you need to wrap the paths in command parameters to quotes, in case they contain spaces.
[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
ErrorCode: Integer;
begin
if CurStep = ssPostInstall then
begin
Exec(
ExpandConstant('{userdesktop}\unzip.exe'),
ExpandConstant('x "{userdesktop}\bbb.update.zip" -d "{userdesktop}"'),
'', SW_HIDE, ewNoWait, ErrorCode);
Exec(
ExpandConstant('{userdesktop}\update.bat'), '', '', SW_HIDE, ewNoWait, ErrorCode);
end;
end;

Inno Setup: How to run a code procedure in Run section or before Run section?

I want to remove the old database before installing the new one, to update it for the user.
I have the following scenario:
In my Components section I have an option for the user:
[Components]
Name: "updateDatabase"; Description: "Update Database"; Types: custom; \
Flags: checkablealone disablenouninstallwarning
And I have in Code section, a procedure to execute, if the user selects this option, in the run section, before installing the new one.
[Code]
procedure RemoveOldDatabase();
begin
...
end;
[Run]
**--> Here I want to call RemoveOldDatabase if Components: updateDatabase is checked**
Filename: "database.exe"; StatusMsg: "Installing new database..."; Components: updateDatabase
The installation of the new database works fine. The problem is I want to remove the old one before installing the new one, calling the procedure RemoveOldDatabase.
Is it possible by only using Inno Setup?
Thanks.
One way, in my view really simple and still descriptive, is to execute your procedure as a BeforeInstall parameter function of your [Run] section entry. A BeforeInstall parameter function is executed once right before an entry is processed (and only if it's processed, which in your case is when the component is selected). You would write just this:
[Run]
Filename: "database.exe"; Components: UpdateDatabase; BeforeInstall: RemoveOldDatabase
[Code]
procedure RemoveOldDatabase;
begin
{ ... }
end;

Inno Setup: Asking for directory page if a task checked

Under the task section I have
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; \
GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
Name: "installFolder"; Description: "Install project folder."; \
GroupDescription:" folder";
and in the Files section is this particular folder
Source: "C:\\Output\LEA\*.*"; DestDir: {code:GetDataDir}; \
Flags: createallsubdirs recursesubdirs ignoreversion;
My aim is to test the for the checked button and then have a window to ask for the directory to install the folder to.
if WizardForm.TasksList.Checked[3] then
GetDataDir;
Can this be done without the need to create pages or the one page to get the directory?
Also, is this a good way to handle extra files that are optional and will be installed to a different location than the default {app} location?
The confusing part for me thus far is when it's all compiled, the GetDataDir is being called before the page to select Tasks. So I choose my directory and then I'm asked whether I want to install it or not. I don't know how to go about getting the GetDataDir to occur afterwards.
The wizard model in Inno Setup means you should always create the wizard pages, but you can skip the ones that don't need to be shown.
This can be done in the ShouldSkipPage() event function by calling IsTaskSelected():
function ShouldSkipPage(PageID: Integer): Boolean;
begin
if (PageID = InstallFolderPage.ID) and not IsTaskSelected('installFolder') then
Result := True
else
Result := False
end;
In this case, with only a single check, it can be shortened to:
function ShouldSkipPage(PageID: Integer): Boolean;
begin
Result := (PageID = InstallFolderPage.ID) and not IsTaskSelected('installFolder')
end;
As TLama said, you don't need to do anything special in the {code:...} functions, just return the appropriate value directly.
You simply need to add '; Tasks: installFolder' at the end of your Source... line, then it won't be called unless the task as been selected.
Source: "C:\\Output\LEA\*.*"; DestDir: {code:GetDataDir}; Flags: createallsubdirs recursesubdirs ignoreversion; Tasks: installFolder

PowerShell Script after install

Newbie question: I would like to run a powershell script (.ps1) at the end of the inno-setup install. Can anyone give me a tip on where to put this? I want the user prompted to be asked if he wants to run this script.
Oh yes, what this script does is run netsh.exe to open up a port, the script is clever and it grabs Env:username and Env:userdomain from the current context. Would the context be the admin who is running the setup? or would it be the original user that ran the setup.exe?
Another way is to run the script using the ShellExec from the code.
[Files]
Source: "yourPowershell.ps1"; DestDir: "{app}"; Flags: overwritereadonly replacesameversion promptifolder;
[Tasks]
Name: "runpowershell"; Description: "Do you want to run Powershell script?"
[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
ErrorCode: Integer;
ReturnCode: Boolean;
begin
if CurStep = ssPostInstall then begin
if(IsTaskSelected('runpowershell')) then begin
ExtractTemporaryFile('yourPowershell.ps1');
ReturnCode := ShellExec('open', '"PowerShell"', ExpandConstant(' -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File "{tmp}\YourPowershell.ps1"'), '', SW_SHOWNORMAL, ewWaitUntilTerminated, ErrorCode);
if (ReturnCode = False) then
MsgBox('Message about problem. Error code: ' + IntToStr(ErrorCode) + ' ' + SysErrorMessage(ErrorCode), mbInformation, MB_OK);
end;
end;
[Run]
.....; Description: Run Script; Flags: postinstall
(See the help for more details.) By default this will display a checkbox and run under the original user's context (although it depends a bit on how the installer is run).
You might want to reconsider this approach, though; if you are performing a machine-wide install then you should probably open the port machine-wide too. You can do this with pure Inno code calling WinAPIs -- no powershell required. (Which is a good thing, because it might not be installed.)
Alternatively if you want to keep it a per-user setting you should consider making your application prompt the user for a decision on first run. After all, why give an option to only one of the many possible users of your app?

How to rename a file using inno setup

I want to first rename an existing file 'My program old' to 'My program v2' but only if 'My program v2' doesn't already exist.
Then I want to rename 'My program' to 'My program old' but only if 'My program old' doesn't already exist.
And then I want to install 'My program' from the installer but only if 'My program' doesn't already exist.
I would be very grateful for any guidance !
I would give a try to something like this. In the ssInstall stage of the CurStepChanged event, which occurs just before the installation process starts, just check if the file doesn't exist with the FileExists function, and if not, then just call the RenameFile function, which will silently fail if the source file doesn't exist, so you don't need to care if the source file exists. In the [Files] section you can then use the onlyifdoesntexist flag for your last requirement. You can follow the commented version of this script if you want:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Files]
Source: "My program"; DestDir: "{app}"; Flags: onlyifdoesntexist
[Code]
function GetFileName(const AFileName: string): string;
begin
Result := ExpandConstant('{app}\' + AFileName);
end;
procedure CurStepChanged(CurStep: TSetupStep);
begin
if (CurStep = ssInstall) then
begin
if not FileExists(GetFileName('My program v2')) then
RenameFile(GetFileName('My program old'), GetFileName('My program v2'));
if not FileExists(GetFileName('My program old')) then
RenameFile(GetFileName('My program'), GetFileName('My program old'));
end;
end;

Resources