Call exe in Inno Setup uninstall - inno-setup

I have an issue with the Inno Setup uninstaller. I have a exe file I want to executable to keep track of the installation and uninstallations. The exe is really simple and sends a message to a server.
[Files]
Source: "Tracker\LocalSandboxInstallTracker.exe"; DestDir: "{app}/Tracker";
Source: "Tracker\LocalSandboxInstallTracker.exe.config"; DestDir: "{app}/Tracker";
Source: "Tracker\Tracker.Client.dll"; DestDir: "{app}/Tracker";
[Run]
Filename: "{app}\Tracker\LocalSandboxInstallTracker.exe"; Parameters: " {#MyAppVersion} install"; Flags: runhidden; StatusMsg: "Sending tracking data..."
[Code]
procedure InitializeUninstallProgressForm();
var
ResultCode: Integer;
begin
Exec ('{app}\Tracker\LocalSandboxInstallTracker.exe',' {#MyAppVersion} uninstall','',SW_SHOW, ewWaitUntilTerminated, ResultCode);
end;
The call at the installation works well, but not at the uninstall. I placed a breakpoint to my Exec command and it really goes through there, but the exe does not seem to be called.

You must expand the {app} constant before passing it to the Exec script function. Use the ExpandConstant whenever you need to get the value of the constant. Modify your script this way:
Exec(ExpandConstant('{app}\Tracker\LocalSandboxInstallTracker.exe'),
'{#MyAppVersion} uninstall', '', SW_SHOW,
ewWaitUntilTerminated, ResultCode);
Also, you should check the function result and the output result code to react when the Exec function fails. The error code you'll get in the ResultCode you can check against the System Error Codes reference or use SysErrorMessage(ResultCode) to get the error description from script.

You have to call the ExpandConstant function if you want to use constants like {app} in your Exec call:
[Code]
procedure InitializeUninstallProgressForm();
var
ResultCode: Integer;
begin
Exec (ExpandConstant('{app}\Tracker\LocalSandboxInstallTracker.exe')
,' {#MyAppVersion} uninstall','',SW_SHOW, ewWaitUntilTerminated, ResultCode);
end;
Other way, you're failing to locate the exe.

Related

Inno Setup - Avoid displaying filenames of sub-installers

I am trying to use the idea from Inno Setup - How to hide certain filenames while installing? (FilenameLabel)
The only sure solution is to avoid installing the files, you do not want to show, using the [Files] section. Install them using a code instead. Use the ExtractTemporaryFile and FileCopy functions
But the files that I want to hide are using in the [Run] Section:
[Files]
Source: "_Redist\DXWebSetup.exe"; DestDir: "{tmp}"; Flags: deleteafterinstall
[Run]
Filename: "{tmp}\DXWebSetup.exe"; Components: DirectX; StatusMsg: "Installing DirectX..."; \
BeforeInstall: StartWaitingForDirectXWindow; AfterInstall: StopWaitingForDirectXWindow
How to hide (while installing, in filenamelabel) using [Files] section, ExtractTemporaryFile and FileCopy functions?
The easiest is to give up on the standard [Files] and [Run] sections and code everything on your own in the CurStepChanged event fuction:
[Files]
Source: "dxwebsetup.exe"; Flags: dontcopy
[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
ProgressPage: TOutputProgressWizardPage;
ResultCode: Integer;
begin
if CurStep = ssInstall then { or maybe ssPostInstall }
begin
if IsComponentSelected('DirectX') then
begin
ProgressPage := CreateOutputProgressPage('Installing prerequsities', '');
ProgressPage.SetText('Installing DirectX...', '');
ProgressPage.Show;
try
ExtractTemporaryFile('dxwebsetup.exe');
StartWaitingForDirectXWindow;
Exec(ExpandConstant('{tmp}\dxwebsetup.exe'), '', '', SW_SHOW,
ewWaitUntilTerminated, ResultCode);
finally
StopWaitingForDirectXWindow;
ProgressPage.Hide;
end;
end;
end;
end;
This even gives you a chance to check for results of the sub-installer. And you can e.g. prevent the installation from continuing, when the sub-installer fails or is cancelled.
Then it's easier to use the PrepareToInstall instead of the CurStepChanged.
Another option is to display a custom label, while extracting the sub-installer.
See Inno Setup - How to create a personalized FilenameLabel with the names I want?

How to use Inno Setup to run a command like steam://

I am trying to make my Inno Setup program to run this command steam://
This command is used to open Steam program via the windows RUN tool.
I press WindowsKey+R and type the command steam:// and it opens the Steam program.
How can i make Inno Setup call this command?
I tried the following without success:
[Run]
Filename: "C:\Users\LUCAS\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories\Run.lnk"; Parameters: "steam://;
also tried that code bellow, and calling AfterInstall: RunOtherInstaller; on [Files] section, but it gives error on installation: %1 is not a valid Win32 application
[Code]
procedure RunOtherInstaller;
var
ResultCode: Integer;
begin
if not Exec(ExpandConstant('C:\Users\LUCAS\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories\Run.lnk'), 'steam://', '', SW_SHOWNORMAL,
ewWaitUntilTerminated, ResultCode)
then
MsgBox('Error!!' + #13#10 +
SysErrorMessage(ResultCode), mbError, MB_OK);
end;
This link is a little strange... It actually points to nowhere when i try to follow it, but it is what calls the windows RUN tool.
I know i could call the Steam.exe from the default folder C:\Program Files (x86)\Steam\Steam.exe but i am trying to avoid problems with users who not have Steam on default folder... So i am trying to use this method running this "External Protocol" (i dont know if this is the right name for it): steam://
You can check registry for Steam location.
Part of my script for triggering installation:
[Code]
function SteamNotInstalled(): Boolean;
var
Path: String;
ErrorCode: Integer;
begin
Result := True;
if (RegQueryStringValue(HKEY_LOCAL_MACHINE, 'Software\Valve\Steam',
'InstallPath', Path)) and (FileExists(Path + '\Steam.exe')) then begin
ShellExec('', ExpandConstant('"' + Path + '\Steam.exe' + '"'), ' -install' + ExpandConstant(' "{src}"'),
'', SW_SHOW, ewNoWait, ErrorCode);
Result := False;
end;
end;
Or you can use shellexec in [Run] section
You can open the steam:// URL as any other URL.
procedure OpenUrl(Url: string);
var
ErrorCode: Integer;
begin
ShellExec('open', Url, '', '', SW_SHOWNORMAL, ewNoWait, ErrorCode);
end;
Or use postinstall [Run] section entry:
[Run]
Filename: steam://xxx; Description: "Run game"; Flags: postinstall shellexec
See also
How to open a web site after uninstallation?
Inno Setup Compiler: How to auto start the default browser with given url?

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;

Execute a command after uninstall

I need my uninstall to run a command after it's removed the files it has installed.
[UninstallRun] is no use as I understand it runs BEFORE files are removed.
I kind of need a "postuninstall" flag.
Any suggestions as to how I can accomplish the above?
See "Uninstall Event Functions" in the documentation. You can use for instance CurUninstallStepChanged when 'CurUninstallStep' is 'usPostUninstall'.
In the same way there is a [Run] section, Inno allows to you to define an [UninstallRun] section to specify which files of your Installer package should be executed on unistall.
For example:
[UninstallRun]
Filename: {app}\Scripts\DeleteWindowsService.bat; Flags: runhidden;
Alternatively, solution proposed by #Sertac Akyuz, which makes use of event functions can be used for tunning a bit more unistalling actions. Here is an example of the usage of CurUninstallStepChanged function among other related functions.
https://github.com/HeliumProject/InnoSetup/blob/master/Examples/UninstallCodeExample1.iss
; -- UninstallCodeExample1.iss --
;
; This script shows various things you can achieve using a [Code] section for Uninstall
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
DefaultGroupName=My Program
UninstallDisplayIcon={app}\MyProg.exe
OutputDir=userdocs:Inno Setup Examples Output
[Files]
Source: "MyProg.exe"; DestDir: "{app}"
Source: "MyProg.chm"; DestDir: "{app}"
Source: "Readme.txt"; DestDir: "{app}"; Flags: isreadme
[Code]
function InitializeUninstall(): Boolean;
begin
Result := MsgBox('InitializeUninstall:' #13#13 'Uninstall is initializing. Do you really want to start Uninstall?', mbConfirmation, MB_YESNO) = idYes;
if Result = False then
MsgBox('InitializeUninstall:' #13#13 'Ok, bye bye.', mbInformation, MB_OK);
end;
procedure DeinitializeUninstall();
begin
MsgBox('DeinitializeUninstall:' #13#13 'Bye bye!', mbInformation, MB_OK);
end;
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
case CurUninstallStep of
usUninstall:
begin
MsgBox('CurUninstallStepChanged:' #13#13 'Uninstall is about to start.', mbInformation, MB_OK)
// ...insert code to perform pre-uninstall tasks here...
end;
usPostUninstall:
begin
MsgBox('CurUninstallStepChanged:' #13#13 'Uninstall just finished.', mbInformation, MB_OK);
// ...insert code to perform post-uninstall tasks here...
end;
end;
end;

How to get Inno Setup to unzip a file it installed (all as part of the one installation process)

To save bandwidth/space as well as prevent accidental meddling, the installation files for a database product (call it Ajax), have been zipped up (call that file "AJAX_Install_Files.ZIP). I would like to have Inno-Setup "install" (i.e., copy) the AJAX_Install_Files.ZIP file to the destination, and then Unzip the files into the same folder where the .ZIP file is located. A subsequent program would be fired off by Inno Setup to actually run the install of product "Ajax".
I've looked through the documentation, FAQ, and KB at the Inno Setup website, and this does not seem possible other than writing a Pascal script (code) - would that be correct, or are there are any alternative solutions?
You can use an external command line tool for unzipping your archive, see here for example. Put it in your [Files] section:
[Files]
Source: "UNZIP.EXE"; DestDir: "{tmp}"; Flags: deleteafterinstall
Then call it in your [Run] section, like this:
[Run]
Filename: "{tmp}\UNZIP.EXE"; Parameters: "{tmp}\ZipFile.ZIP -d C:\TargetDir"
(You'll probably want to take your target directory from a script variable, so there is some more work that needs to be done)
You can use the shell Folder.CopyHere method to extract a ZIP.
const
SHCONTCH_NOPROGRESSBOX = 4;
SHCONTCH_RESPONDYESTOALL = 16;
procedure UnZip(ZipPath, TargetPath: string);
var
Shell: Variant;
ZipFile: Variant;
TargetFolder: Variant;
begin
Shell := CreateOleObject('Shell.Application');
ZipFile := Shell.NameSpace(ZipPath);
if VarIsClear(ZipFile) then
RaiseException(
Format('ZIP file "%s" does not exist or cannot be opened', [ZipPath]));
TargetFolder := Shell.NameSpace(TargetPath);
if VarIsClear(TargetFolder) then
RaiseException(Format('Target path "%s" does not exist', [TargetPath]));
TargetFolder.CopyHere(
ZipFile.Items, SHCONTCH_NOPROGRESSBOX or SHCONTCH_RESPONDYESTOALL);
end;
Note that the flags SHCONTCH_NOPROGRESSBOX and SHCONTCH_RESPONDYESTOALL work on Windows Vista and newer.
For an example of extracting some files only, see:
How to get Inno Setup to unzip a single file?
I answered a very similar question and some of the details apply.
I would question why you need a ZIP file of the contents? I personally would place the uncompressed files into the setup. I would then have two [category] entries one for the application and one for the data. Default both the be checked.
This would allow the users to install a fresh set of the data if needed at a later date.
If you really want a ZIP file and want to keep it easy you could, ship both the zip files and the uncompressed files in the same setup.
Update:
By default files that get placed in your setup.exe are compressed.
You can also have the files extracted to a temporary location so you can run your
installation application, then have them deleted.
[Files]
Source: "Install1.SQL"; DestDir: "{tmp}"; Flags:deleteafterinstall;
Source: "Install2.SQL"; DestDir: "{tmp}"; Flags:deleteafterinstall;
You can just create silent self-extracting archive (SFX) archive, example described here how to create SFX archive for stuff you need, and write Pascal code to just run it like this (script for Inno Setup 6.0.2):
[Tasks]
Name: "intallSenselockDriver"; Description: "Install Senselock driver."; GroupDescription: "Install the necessary software:";
[Code]
function ExecTmpFile(FileName: String): Boolean;
var
ResultCode: Integer;
begin
if not Exec(ExpandConstant('{tmp}\' + FileName), '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode)
then
begin
MsgBox('Other installer failed to run!' + #13#10 + SysErrorMessage(ResultCode), mbError, MB_OK);
Result := False;
end
else
Result := True;
end;
procedure RunOtherInstallerSFX(ArchiveName: String; ExePath: String);
begin
ExtractTemporaryFile(ArchiveName);
ExecTmpFile(ArchiveName);
ExecTmpFile(ExePath);
end;
function PrepareToInstall(var NeedsRestart: Boolean): String;
begin
if WizardIsTaskSelected('intallSenselockDriver') then
RunOtherInstallerSFX('1_senselock_windows_3.1.0.0.exe', '1_senselock_windows_3.1.0.0\InstWiz3.exe');
Result := '';
end;
It worked perfectly for me.
Using Double quotes worked for me.
Single quotes were not working.
[Files]
Source: "unzip.exe"; DestDir: "{userappdata}\{#MyAppName}\{#InputFolderName}"; Flags: ignoreversion
[Run]
Filename: "{userappdata}\{#MyAppName}\{#InputFolderName}\unzip.exe"; Parameters: " ""{userappdata}\{#MyAppName}\{#InputFolderName}\ZIPFILENAME.zip"" -d ""{userappdata}\{#MyAppName}\{#InputFolderName}"" "; Flags: runascurrentuser

Resources