Inno Setup - Progress bar doesn't show when uninstall - inno-setup

I'm using Inno Setup to create my own installer. When user uninstall app I want delete some folder.
So I use CurUninstallStepChanged event to delete folder and show "progress bar" with npbstMarquee style (based on Inno Setup: How to handle progress bar on [UninstallDelete] section?).
Here is the code:
procedure DeleteFolder();
var
FindRec: TFindRec;
fullPath: string;
tmpMsg: string;
StatusText: string;
deletePath: string;
begin
{ find all and delete }
UninstallProgressForm.ProgressBar.Style := npbstMarquee;
StatusText := UninstallProgressForm.StatusLabel.Caption;
UninstallProgressForm.StatusLabel.WordWrap := True;
UninstallProgressForm.StatusLabel.AutoSize := True;
fullPath := 'C:\ProgramData\TestPath';
if FindFirst(ExpandConstant(fullPath + '\*'), FindRec) then
try
repeat
if (FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0) and
(FindRec.Name <> '.') and (FindRec.Name <> '..') then begin
deletePath := AddBackslash(fullPath) + FindRec.Name;
tmpMsg := 'Deleting...' + #13#10 + deletePath;
UninstallProgressForm.StatusLabel.Caption := tmpMsg;
DelTree(deletePath, True, True, True);
end;
until
not FindNext(FindRec);
finally
UninstallProgressForm.StatusLabel.Caption := StatusText;
FindClose(FindRec);
end;
UninstallProgressForm.ProgressBar.Style := npbstNormal;
end;
{ Uninstall event }
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
case CurUninstallStep of
usUninstall:
begin
DeleteFolder();
end;
end;
end;
If I using debug each line, I can see progress bar running. But when I using unins000.exe then only Caption can show, progress bar is not showing.
How can I fix it?

You have to pump the message queue to display/animate the progress bar.
Inno Setup: How to modify long running script so it will not freeze GUI?
Particularly, you can use the AppProcessMessage function from:
My SQL server discovery on LAN by listening port (Inno Setup)
Though with use of DelTree, the interval between calls to AppProcessMessage will be too big to animate the progress bar smoothly. You would have to implement a recursive delete explicitly to allow pumping the queue frequently enough.

Related

Including a summary of the files to download with Inno Setup 6.1.1 beta to Ready page?

With Inno Download Plugin I had a code that registers a list of files to download and adds the list to "Ready" page memo at the same time:
Building memo text for Inno Download Plugin
I have modified the code to work with Inno Setup 6.1.1 download page, instead of IDP:
procedure AddFileForDownload(Url, FileName: string);
begin
DownloadPage.Add(Url, FileName, '');
FilesToDownload := FilesToDownload + ' ' + ExtractFileName(FileName) + #13#10;
Log('File to download: ' + Url);
end;
Then I adjusted NextButtonClick like this:
function NextButtonClick(CurPageID: integer): boolean;
begin
Result := True;
if (CurPageID = wpReady) then
begin
DownloadPage.Clear;
if (dotNetNeeded) then begin
{ We need to download the 4.6.2 setup from the Microsoft Website }
dotNetRedistPath := ExpandConstant('{tmp}\NDP451-KB2858728-x86-x64-AllOS-ENU.exe');
AddFileForDownload(dotnetRedistURL, 'NDP451-KB2858728-x86-x64-AllOS-ENU.exe');
end;
if (bVcRedist64BitNeeded) then
begin
{ We need to download the 64 Bit VC Redistributable from the Microsoft Website }
vcRedist64BitPath := ExpandConstant('{tmp}\vc_redist.x64.exe');
AddFileForDownload(vcRedist64BitURL, 'vc_redist.x64.exe');
end;
if (bVcRedist32BitNeeded) then
begin
{ We need to download the 32 Bit VC Redistributable from the Microsoft Website }
vcRedist32BitPath := ExpandConstant('{tmp}\vc_redist.x86.exe');
AddFileForDownload(vcRedist32BitURL, 'vc_redist.x86.exe');
end;
if (WizardIsTaskSelected('downloadhelp')) then
AddFileForDownload('{#HelpDocSetupURL}', 'HelpDocSetup.exe');
DownloadPage.Show;
try
try
DownloadPage.Download;
Result := True;
except
SuppressibleMsgBox(AddPeriod(GetExceptionMessage), mbCriticalError, MB_OK, IDOK);
Result := False;
end;
finally
DownloadPage.Hide;
end;
end;
end;
I ran the installer, and checked the wizard option to download the help documentation, and yet the Ready page displays only:
The Download section is not being added. How can that be? When I click Next it does continue to the next page to download the file.
I added some extra logging for FilesToDownload and it is interesting:
2020-11-01 11:44:22.409 UpdateReadyMemo FileToDownload:
2020-11-01 11:44:25.671 File to download: https://www.publictalksoftware.co.uk/downloads/MSAHelpDocumentationSetup.exe
2020-11-01 11:44:25.671 FileToDownload: HelpDocSetup.exe
The UpdateReadyMemo method is being called before we populate the variable. Confused!
I got confused a bit initially. Because the issue is obvious. Your code executes when you click "Install" button on the "Ready" page. So obviously only after the "Ready" page shows.
You have to call the AddFileForDownload earlier. Maybe to NextButtonClick(wpSelectTasks).
function NextButtonClick(CurPageID: integer): boolean;
begin
Result := True;
if (CurPageID = wpSelectTasks) then
begin
DownloadPage.Clear;
if (dotNetNeeded) then
begin
// We need to download the 4.6.2 setup from the Microsoft Website
dotNetRedistPath :=
ExpandConstant('{tmp}\NDP451-KB2858728-x86-x64-AllOS-ENU.exe');
AddFileForDownload(
dotnetRedistURL, 'NDP451-KB2858728-x86-x64-AllOS-ENU.exe');
end;
if (bVcRedist64BitNeeded) then
begin
// We need to download the 64 Bit VC Redistributable
// from the Microsoft Website
vcRedist64BitPath := ExpandConstant('{tmp}\vc_redist.x64.exe');
AddFileForDownload(vcRedist64BitURL, 'vc_redist.x64.exe');
end;
if (bVcRedist32BitNeeded) then
begin
// We need to download the 32 Bit VC Redistributable
// from the Microsoft Website
vcRedist32BitPath := ExpandConstant('{tmp}\vc_redist.x86.exe');
AddFileForDownload(vcRedist32BitURL, 'vc_redist.x86.exe');
end;
if (WizardIsTaskSelected('downloadhelp')) then
AddFileForDownload('{#HelpDocSetupURL}', 'HelpDocSetup.exe');
end
else
if (CurPageID = wpReady) then
begin
DownloadPage.Show;
try
try
DownloadPage.Download;
Result := True;
except
SuppressibleMsgBox(
AddPeriod(GetExceptionMessage), mbCriticalError, MB_OK, IDOK);
Result := False;
end;
finally
DownloadPage.Hide;
end;
end;
end;
(untested)

Discard the /TYPE and /COMPONENTS parameter on upgrade

In case of an upgrade / re-installation, is there a way to discard the /TYPE and /COMPONENTS parameter value passed on the command line to the installer and instead use the previously used values ?
I can read the values used earlier from Registry (or alternatively make out the details based on existence of files assuming they have not been manually altered)
I have read the following threads and can disable the "Select Components" page in UI mode
Inno Setup Skip "Select Components" page when /Type command-line parameter is specified
InnoSetup: Disable components page on upgrade
However, if the aforesaid parameters are passed from command line, they seem to override the defaults.
You cannot discard them.
What you can do is to check if those parameters were provided and if they were:
Re-launch the installer without them (show below), or
Read the previously selected type and components from registry and re-set the controls accordingly.
Re-launching the installer without /TYPE= and /COMPONENTS=
const
UninstallKey =
'Software\Microsoft\Windows\CurrentVersion\Uninstall\' +
'{#SetupSetting("AppId")}_is1';
function IsUpgrade: Boolean;
var
Value: string;
begin
Result :=
(RegQueryStringValue(HKLM, UninstallKey, 'UninstallString', Value) or
RegQueryStringValue(HKCU, UninstallKey, 'UninstallString', Value)) and
(Value <> '');
end;
function ShellExecute(hwnd: HWND; lpOperation: string; lpFile: string;
lpParameters: string; lpDirectory: string; nShowCmd: Integer): THandle;
external 'ShellExecuteW#shell32.dll stdcall';
function InitializeSetup(): Boolean;
var
Params, S: string;
Relaunch: Boolean;
I, RetVal: Integer;
begin
Result := True;
if IsUpgrade then
begin
Relaunch := False;
// Collect current instance parameters
for I := 1 to ParamCount do
begin
S := ParamStr(I);
if (CompareText(Copy(S, 1, 7), '/TYPES=') = 0) or
(CompareText(Copy(S, 1, 12), '/COMPONENTS=') = 0) then
begin
Log(Format('Will re-launch due to %s', [S]));
Relaunch := True;
end
else
begin
// Unique log file name for the child instance
if CompareText(Copy(S, 1, 5), '/LOG=') = 0 then
begin
S := S + '-sub';
end;
// Do not pass our /SL5 switch
// This should not be needed since Inno Setup 6.2,
// see https://groups.google.com/g/innosetup/c/pDSbgD8nbxI
if CompareText(Copy(S, 1, 5), '/SL5=') <> 0 then
begin
Params := Params + AddQuotes(S) + ' ';
end;
end;
end;
if not Relaunch then
begin
Log('No need to re-launch');
end
else
begin
Log(Format('Re-launching setup with parameters [%s]', [Params]));
RetVal :=
ShellExecute(0, '', ExpandConstant('{srcexe}'), Params, '', SW_SHOW);
Log(Format('Re-launching setup returned [%d]', [RetVal]));
Result := (RetVal > 32);
// if re-launching of this setup succeeded, then...
if Result then
begin
Log('Re-launching succeeded');
// exit this setup instance
Result := False;
end
else
begin
Log(Format('Elevation failed [%s]', [SysErrorMessage(RetVal)]));
end;
end;
end;
end;
The code is for Unicode version of Inno Setup.
The code can be further improved to keep the master installer waiting for the child installer to complete. When can make a difference, particularly if the installer is executed by some automatic deployment process.

Inno Setup: Check if file exists anywhere in C: drive

Some questions/solutions I found on here were similar but not quite what I needed.
I'm trying to create an installer for a Python application I've created for Windows. The installer calls another installer (openscad_installer.exe) and the user has the choice to install that wherever they like (i.e. I don't know the destination and would need to be able to find it) or not to install it at all.
I essentially need to check if the openscad.exe file exists (i.e. if it is installed) anywhere on the computer (in the C: drive) and if it does not exist then I need to uninstall my software.
The uninstall process seems simple enough but I don't know how to find out if the file exists. Thanks for the help.
Searching the file in C: drive (and possibly any other drive, as an user may choose to install a software anywhere else) is doable, but can take ages.
I'd suggest you instead check for an existence of the SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\OpenSCAD registry key:
const
OpenSCADRegKey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\OpenSCAD';
function PrepareToInstall(var NeedsRestart: Boolean): String;
var
ResultCode: integer;
begin
Exec('OpenSCAD-xxx-Installer.exe', '', '', SW_SHOW, ewWaitUntilTerminated,
ResultCode);
if RegKeyExists(HKEY_CURRENT_USER_32, OpenSCADRegKey) or
RegKeyExists(HKEY_CURRENT_USER_64, OpenSCADRegKey) or
RegKeyExists(HKEY_LOCAL_MACHINE_32, OpenSCADRegKey) or
RegKeyExists(HKEY_LOCAL_MACHINE_64, OpenSCADRegKey) then
begin
Log('OpenSCAD is installed');
end
else
begin
Log('OpenSCAD is not installed');
// Abort installation
Result := 'OpenSCAD is not installed';
Exit;
end;
end;
If you need to know the installation location, read and parse the UninstallString value:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\OpenSCAD]
"UninstallString"="C:\\Program Files\\OpenSCAD\\Uninstall.exe"
If you insist on searching for openscad.exe use:
function FindFile(RootPath: string; FileName: string): string;
var
FindRec: TFindRec;
FilePath: string;
begin
Log(Format('Searching %s for %s', [RootPath, FileName]));
if FindFirst(RootPath + '\*', FindRec) then
begin
try
repeat
if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
begin
FilePath := RootPath + '\' + FindRec.Name;
if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
begin
Result := FindFile(FilePath, FileName);
if Result <> '' then Exit;
end
else
if CompareText(FindRec.Name, FileName) = 0 then
begin
Log(Format('Found %s', [FilePath]));
Result := FilePath;
Exit;
end;
end;
until not FindNext(FindRec);
finally
FindClose(FindRec);
end;
end
else
begin
Log(Format('Failed to list %s', [RootPath]));
end;
end;
Yet another option is looking for the file in search path:
How can I check SQLCMD.EXE if it is installed on client in Inno Setup

How to set StatusMsg from PrepareToInstall event function

My application requires .NET Framework to be installed so I run .NET installation in PrepareToIntall event function. While the installation is running I would like to display some simple message on Wizard.
I found How to set the status message in [Code] Section of Inno install script? but the solution there doesn't work for me.
I tried
WizardForm.StatusLabel.Caption := CustomMessage('InstallingDotNetMsg');
and also
WizardForm.PreparingLabel.Caption := CustomMessage('InstallingDotNetMsg');
EDIT
I have to do this in PrepareToInstall function, because I need to stop the setup when .net installation fails.
Code looks like this right now:
function PrepareToInstall(var NeedsRestart: Boolean): String;
var
isDotNetInstalled : Boolean;
errorCode : Integer;
errorDesc : String;
begin
isDotNetInstalled := IsDotNetIntalledCheck();
if not isDotNetInstalled then
begin
//WizardForm.PreparingLabel.Caption := CustomMessage('InstallingDotNetMsg');
WizardForm.StatusLabel.Caption := CustomMessage('InstallingDotNetMsg');
ExtractTemporaryFile('dotNetFx40_Full_x86_x64.exe');
if not ShellExec('',ExpandConstant('{tmp}\dotNetFx40_Full_x86_x64.exe'),'/passive /norestart', '', SW_HIDE, ewWaitUntilTerminated, errorCode) then
begin
errorDesc := SysErrorMessage(errorCode);
MsgBox(errorDesc, mbError, MB_OK);
end;
isDotNetInstalled := WasDotNetInstallationSuccessful();
if not isDotNetInstalled then
begin
Result := CustomMessage('FailedToInstalldotNetMsg');
end;
end;
end;
Any Ideas how to achieve this?
The StatusLabel is hosted by the InstallingPage wizard page while you're on PreparingPage page in the PrepareToInstall event method. So that's a wrong label. Your attempt to set the text to the PreparingLabel was correct, but failed because that label is hidden by default (it is shown when you return non empty string as a result to the event method).
But you can show it for a while (you are using ewWaitUntilTerminated flag, so your installation is synchronous, thus it won't hurt anything):
[Code]
function PrepareToInstall(var NeedsRestart: Boolean): String;
var
WasVisible: Boolean;
begin
// store the original visibility state
WasVisible := WizardForm.PreparingLabel.Visible;
try
// show the PreparingLabel
WizardForm.PreparingLabel.Visible := True;
// set a label caption
WizardForm.PreparingLabel.Caption := CustomMessage('InstallingDotNetMsg');
// do your installation here
finally
// restore the original visibility state
WizardForm.PreparingLabel.Visible := WasVisible;
end;
end;
Another solution is to use CreateOutputProgressPage to display a progress page over the top of the Preparing to Install page. See the CodeDlg.iss example script included with Inno for an example of the usage; it's fairly straightforward.

Uninstall when running inno setup with application already installed

I have just started using inno setup, and it seems to work well. However, when I run the installer with the app already installed it reinstalls. I would like to give the user to uninstall. Is this possible, and if so, how can it be done?
To be specific, I have written a game for a homework assignment. I made an installer using inno setup. The app installs fine and can be uninstalled using the control panel, but my professor would like to be able to uninstall the application by re-running the installer and choosing an uninstall option. This will save him time since he has about 50 of these assignments to mark.
Thanks,
Gerry
The next script will make the following options form when the application is already installed on the target system when the setup is started:
When the user clicks Repair button, the setup is normally started. When user clicks the Uninstall button, the previously installed application is uninstalled. When user closes that form, nothing happens.
Here is the script (don't forget to specify, ideally some unique, value for the AppId setup directive in your script):
[Setup]
AppName=My Program
AppVersion=1.5
AppId=1C9FAC66-219F-445B-8863-20DEAF8BB5CC
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output
[CustomMessages]
OptionsFormCaption=Setup options...
RepairButtonCaption=Repair
UninstallButtonCaption=Uninstall
[Code]
const
mrRepair = 100;
mrUninstall = 101;
function ShowOptionsForm: TModalResult;
var
OptionsForm: TSetupForm;
RepairButton: TNewButton;
UninstallButton: TNewButton;
begin
Result := mrNone;
OptionsForm := CreateCustomForm;
try
OptionsForm.Width := 220;
OptionsForm.Caption := ExpandConstant('{cm:OptionsFormCaption}');
OptionsForm.Position := poScreenCenter;
RepairButton := TNewButton.Create(OptionsForm);
RepairButton.Parent := OptionsForm;
RepairButton.Left := 8;
RepairButton.Top := 8;
RepairButton.Width := OptionsForm.ClientWidth - 16;
RepairButton.Caption := ExpandConstant('{cm:RepairButtonCaption}');
RepairButton.ModalResult := mrRepair;
UninstallButton := TNewButton.Create(OptionsForm);
UninstallButton.Parent := OptionsForm;
UninstallButton.Left := 8;
UninstallButton.Top := RepairButton.Top + RepairButton.Height + 8;
UninstallButton.Width := OptionsForm.ClientWidth - 16;
UninstallButton.Caption := ExpandConstant('{cm:UninstallButtonCaption}');
UninstallButton.ModalResult := mrUninstall;
OptionsForm.ClientHeight := RepairButton.Height + UninstallButton.Height + 24;
Result := OptionsForm.ShowModal;
finally
OptionsForm.Free;
end;
end;
function GetUninstallerPath: string;
var
RegKey: string;
begin
Result := '';
RegKey := Format('%s\%s_is1', ['Software\Microsoft\Windows\CurrentVersion\Uninstall',
'{#emit SetupSetting("AppId")}']);
if not RegQueryStringValue(HKEY_LOCAL_MACHINE, RegKey, 'UninstallString', Result) then
RegQueryStringValue(HKEY_CURRENT_USER, RegKey, 'UninstallString', Result);
end;
function InitializeSetup: Boolean;
var
UninstPath: string;
ResultCode: Integer;
begin
Result := True;
UninstPath := RemoveQuotes(GetUninstallerPath);
if UninstPath <> '' then
begin
case ShowOptionsForm of
mrRepair: Result := True;
mrUninstall:
begin
Result := False;
if not Exec(UninstPath, '', '', SW_SHOW, ewNoWait, ResultCode) then
MsgBox(FmtMessage(SetupMessage(msgUninstallOpenError), [UninstPath]), mbError, MB_OK);
end;
else
Result := False;
end;
end;
end;
For some reason your code
RegKey := Format('%s\%s_is1', ['Software\Microsoft\Windows\CurrentVersion\Uninstall',
'{#emit SetupSetting("AppId")}']);
returned an extra { to the _is1 value. I didn't had the time to check why or where i was wrong in my implementation,
all i confirm is that my installer works with the
RegKey := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#emit SetupSetting("AppId")}_is1');
alternate.
Hope it helps.
Thank you for the code sample.
When using Inno Setup, there's no reason to uninstall a previous version unless that version was installed by a different installer program. Otherwise upgrades are handled automatically.
Your answer is here :
InnoSetup: How to automatically uninstall previous installed version? previous-installed-version

Resources