Inno Setup - delete the installer after the install process - inno-setup

As part of the copy protection of my game, the installer needs to delete itself after the installation process. This code:
[Code]
procedure MyAfterInstall();
begin
DeleteFile('F:\TEST_SETUP\setup.exe');
end;
...does nothing because the setup runs.
Is there a solution to run an 'command line' or cmd that gets the full path of the installer (it could be everywhere on the disc of the client) and delete it after the install?

Add following method in your [CODE] section and you are all set...
[CODE]
procedure CurStepChanged(CurStep: TSetupStep);
var
strContent: String;
intErrorCode: Integer;
strSelf_Delete_BAT: String;
begin
if CurStep=ssDone then
begin
strContent := ':try_delete' + #13 + #10 +
'del "' + ExpandConstant('{srcexe}') + '"' + #13 + #10 +
'if exist "' + ExpandConstant('{srcexe}') + '" goto try_delete' + #13 + #10 +
'del %0';
strSelf_Delete_BAT := ExtractFilePath(ExpandConstant('{tmp}')) + 'SelfDelete.bat';
SaveStringToFile(strSelf_Delete_BAT, strContent, False);
Exec(strSelf_Delete_BAT, '', '', SW_HIDE, ewNoWait, intErrorCode);
end;
end;

If you need to delete the installer immediately after the installation, you have to implement some custom solution. As you have already found yourself, executable cannot delete itself as the executable is locked when it is running.
You can implement a small tool that you install by your installer. The tool will be run by the installer, when an installation finishes. The tool will keep running silently and trying to delete the installer, until is succeeds.
[Files]
; Install the tool
Source: "zapself.exe"; DestDir: "{app}"
[Run]
; Run the tool and pass the installer path
Filename: "{app}\zapself.exe"; Parameters: "{srcexe}"
Actually you do not need to build an .exe for this. A simple batch file created on-the-fly will do, for an example, see Unload a .NET DLL from an unmanaged process.
Need to say that I really do not understand why you are trying to do this.

Related

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?

Execute different BLOCK of commands in Inno Setup Run section based on Windows version

I know there is already question Execute different command in Inno Setup Run section based on Windows version with very good answer.
My question is how to perform different blocks of commands for different target Windows versions. My problem is that I have ~10-15 commands that need to perform if target version is Windows 7 and the ~same amount of different commands for Windows 8 or above.
Is it possible to avoid the need to add ; OnlyBelowVersion: 6.2 after each command needed for first case and ; MinVersion: 6.2 after each command in the second block?
I know there is preprocessor conditions "#if", #else and #endif but that of course works only at compilation time
The question and answers Determine Windows version in Inno Setup although may look similar to this question are NOT answering it. I know how to determine Windows version in Inno Setup. I also know about those ; MinVersion: 6.2 and ; OnlyBelowVersion: 6.2 options. I am asking if it is possible to specify a block of commands (10-15 commands) and apply that option to entire block, not to each and every command individually.
The goal is not to avoid "cryptic version numbers" but to avoid repeating the same condition many times. And to avoid risk of forgetting it when block will grow over the time.
The solution that I found so far is to use CurStepChanged procedure:
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssPostInstall then
if IsWindows8OrLater() then
MsgBox('Running on Windows 8 Or Later', mbInformation, MB_OK)
{ 15 comands or call of W8-specific procedure goes here }
else begin
MsgBox('Running on Windows 7', mbInformation, MB_OK);
{ 15 comands or call of W7-specific procedure goes here }
end;
end;
But it looks a bit ugly to me...
There are no block-control features in the .iss file.
All you can do, to avoid repeating the cryptic version numbers, is to define a preprocessor variable like:
#define Windows8AndNewer "MinVersion: 6.2"
#define Windows7AndOlder "OnlyBelowVersion: 6.2"
[Run]
Filename: "Windows8-Command1.exe"; {#Windows8AndNewer}
Filename: "Windows8-Command2.exe"; {#Windows8AndNewer}
Filename: "Windows7-Command1.exe"; {#Windows7AndOlder}
Filename: "Windows7-Command2.exe"; {#Windows7AndOlder}
The only other way is to reimplement the [Run] section in the [Code] using the Exec function:
procedure Run(FileName: string);
var
ResultCode: Integer;
begin
Exec(FileName, '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode);
{ some error checking }
end;
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssPostInstall then
begin
if GetWindowsVersion() >= $06020000 then
begin
Log('Running on Windows 8 or later');
Run('Windows8-Command1.exe');
Run('Windows8-Command2.exe');
end
else
begin
Log('Running on Windows 7 or older');
Run('Windows7-Command1.exe');
Run('Windows7-Command2.exe');
end;
end;
end;

Inno Setup: Can the installer update itself?

My installer creates a folder with my app and the installer itself. The installer is later on used as an updater for the application.
All of this works well but I would like to update the installer itself and not just my application.
I download a zip from my server and expect everything inside the zip to override everything in the app folder (including the installer itself).
Every time I run the installer I get an error that a file is already in use.
Can the installer update itself?
You cannot replace running application.
You have these options:
Start the "updater" via batch file (referring to assumed shortcut to the updater in a Start menu or any other method of invocation), that makes a copy of the installer to a temporary location and runs the updater from there. When updating, update the original copy.
To avoid the batch file (and an unpleasant console window), you can use JScript. Or even make the installer (updater) do this itself (create a copy of itself, launch the copy, exit itself).
Use restartreplace flag in Files section entry to schedule installer/updater replace for the next Windows start.
Keeping the installer in the {app} directory is probably acceptable for small applications, for larger ones consider an updater, or even another location, (in the form of a feature request) {Backup} to refer to a path on some flash or removable drive.
Run the setup from the {app} directory and after the version check, download the installer to the {tmp} folder.
Exec the installer thus before quitting, keeping mind of possible mutex conditions in the code section of your script:
if Exec(ExpandConstant('{tmp}\{OutputBaseFilename}), '', '', SW_SHOW,
ewNoWait, ResultCode) then
// success/fail code follows
To copy the installer back to {app} the Install script will have this in Files:
[Files]
Source: "{srcexe}"; DestDir: "{app}"; Flags: external
Presumably the above line will not produce an error when the installer is actually run from {app}.
Then, to clean up, the next time the installer is run from the {src} (= {app}) directory, the downloaded one can be removed from the {tmp} directory with
DeleteFile({tmp}\{OutputBaseFilename})
I've run into this same problem recently. We have a main installer that manages a bunch of other setup packages for our applications, and I wanted to add some mechanism for this main installer to update itself.
I've managed to find a solution creating a second Inno Setup package that serves just as an updater for the main installer, and that updater goes embedded in the main installer.
So, we have a XML file in our website that gives the latest version available for the main installer:
[ InstallerLastVersion.xml ]
<?xml version="1.0" encoding="utf-8"?>
<installer name="Our Central Installer" file="OurInstaller.exe" version="1.0.0.1" date="10/15/2021" />
The main code for this auto-update functionality in the main installer is that:
[ OurInstaller.iss ]
[Files]
; This file won't be installed ('dontcopy' flag), it is just embedded
; into the installer to be extracted and executed in case it's necessary
; to update the Installer.
Source: ".\OurInstallerUpdater.exe"; Flags: dontcopy
[Code]
const
UrlRoot = 'http://ourwebsite.com/';
// Downloads a XML file from a website and loads it into XmlDoc parameter.
function LoadXml(XmlFile: String; var XmlDoc: Variant): Boolean;
begin
XmlDoc := CreateOleObject('MSXML2.DOMDocument');
XmlDoc.async := False;
Result := XmlDoc.Load(UrlRoot + XmlFile);
end;
// Checks if there's a newer version of the Installer
// and fires the updater if necessary.
function InstallerWillBeUpdated(): Boolean;
var
XmlDoc: Variant;
LastVersion, Filename, Param: String;
ResultCode: Integer;
begin
if not LoadXml('InstallerLastVersion.xml', XmlDoc) then
begin
Result := False;
Exit;
end;
// Gets the latest version number, retrieved from
// the XML file download from the website.
LastVersion := XmlDoc.documentElement.getAttribute('version');
// If this installer version is the same as the one available
// at the website, there's no need to update it.
if '{#SetupSetting("AppVersion")}' = LastVersion then
begin
Result := False;
Exit;
end;
if MsgBox('There is an update for this installer.' + #13#10 +
'Do you allow this installer to be updated right now?',
mbConfirmation, MB_YESNO) = IDNO then
begin
Result := False;
Exit;
end;
// Extracts the updater, that was embedded into this installer,
// to a temporary folder ({tmp}).
ExtractTemporaryFile('OurInstallerUpdater.exe');
// Gets the full path for the extracted updater in the temp folder.
Filename := ExpandConstant('{tmp}\OurInstallerUpdater.exe');
// The current folder where the installer is stored is going to be
// passed as a parameter to the updater, so it can save the new version
// of the installer in this same folder.
Param := ExpandConstant('/Path={src}');
// Executes the updater, with a command-line like this:
// OurInstallerUpdater.exe /Path=C:\InstallerPath
Result := Exec(Filename, Param, '', SW_SHOW, ewNoWait, ResultCode);
end;
function InitializeSetup(): Boolean;
begin
// Checks if the installer needs to be updated and fires the update.
// If the update is fired the installer must be ended, so it can be
// replaced with the new version. Returning this InitializeSetup()
// function with False already makes the installer to be closed.
if InstallerWillBeUpdated() then
begin
Result := False;
Exit;
end;
Result := True;
end;
Now to the updater code (I'm using the "new" DownloadTemporaryFile() function added in Inno Setup 6.1):
[ OurInstallerUpdater.iss ]
[Code]
const
UrlRoot = 'http://ourwebsite.com/';
Installer = 'OurInstaller.exe';
function InitializeSetup(): Boolean;
var
DestinationPath: String;
ResultCode: Integer;
begin
// Retrieves the parameter passed in the execution
// of this installer, for example:
// OurInstallerUpdater.exe /Path=C:\InstallerPath
// If no parameter was passed it uses 'C:\InstallerPath' as default.
// (where {sd} is the constant that represents the System Drive)
DestinationPath := ExpandConstant('{param:Path|{sd}\InstallerPath}') + '\' + Installer;
try
// Downloads the newer version of the installer to {tmp} folder.
DownloadTemporaryFile(UrlRoot + Installer, Installer, '', nil);
// Copies the downloaded file from the temp folder to the folder where
// the current installer is stored, the one that fired this updater.
FileCopy(ExpandConstant('{tmp}\') + Installer, DestinationPath, False);
// Runs the updated installer.
Exec(DestinationPath, '', '', SW_SHOW, ewNoWait, ResultCode);
except
MsgBox('The file ''' + Installer + ''' could not be downloaded.', mbInformation, MB_OK);
end;
// Returning False from this function implies that this
// updater can now be finished, since its goal has already
// been reached (to update the main installer).
Result := False;
end;
In this setting you have to build OurInstallerUpdater.exe before OurInstaller.exe, since the first one is embedded into the second one.
Some sources:
Inno Setup: Install file from Internet
Using {AppVersion} as a parameter for a function in Inno Setup
Exit from Inno Setup installation from [Code]
Is it possible to accept custom command line parameters with Inno Setup
How to get the installer path in Inno Setup?

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 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