Inno Setup: Checking existence of a file in 32-bit System32 (Sysnative) folder - inno-setup

I have a sys file in the System32\Drivers folder called gpiotom.sys (custom sys file). My my application is strictly 32-bit compatible only, so my installer runs in 32-bit mode. My script needs to find if this sys file exists or not.
I used the FileExists function explained on the below post but it does not work since it only works for 64-bit application only:
InnoSetup (Pascal): FileExists() doesn't find every file
Is there any way I can find if my sys file exists or not in a 32-bit mode?
Here is my code snippet in Pascal Script language:
function Is_Present() : Boolean;
begin
Result := False;
if FileExists('{sys}\driver\gpiotom.sys') then
begin
Log('File exists');
Result := True;
end;
end;

In general, I do not think there is any problem running an installer for 32-bit application in 64-bit mode. Just make sure you use 32-bit paths where necessary, like:
[Setup]
DefaultDirName={pf32}\My Program
Anyway, if you want to stick with 32-bit mode, you can use EnableFsRedirection function to disable WOW64 file system redirection.
With use of this function, you can implement a replacement for FileExists:
function System32FileExists(FileName: string): Boolean;
var
OldState: Boolean;
begin
if IsWin64 then
begin
Log('64-bit system');
OldState := EnableFsRedirection(False);
if OldState then Log('Disabled WOW64 file system redirection');
try
Result := FileExists(FileName);
finally
EnableFsRedirection(OldState);
if OldState then Log('Resumed WOW64 file system redirection');
end;
end
else
begin
Log('32-bit system');
Result := FileExists(FileName);
end;
if Result then
Log(Format('File %s exists', [FileName]))
else
Log(Format('File %s does not exists', [FileName]));
end;

Related

Hardcode startup switches into INNO Setup script and retrieve them in [code] in InnoIDE

When I compile and then run my Inno script, I want to parse hard coded command line switches. So, instead of running my script as c:\myInstall.exe /myswitch I want to run it via IDE and have this switch included at the start here
With this code, I want to retrieve this switch in ParamStr(x)
function MyParams(param: String): string;
begin
MsgBox(ParamStr(3), mbError, MB_OK);
Result := MyParameter;
end;
{Parameter Detection--end}
function InitializeSetup(): Boolean;
begin
MyParams('XXX');
end;
I found a tool, which does this for you - you can set DEBUG variables if you use
Inno Script Studio

Inno Script: Skip password if the application is already installed

I am trying to create an Inno Setup installer that will require a password from the user if the application has never been installed on the local machine.
I have the script that gets a password, and I have a Code section that checks for the existence of the uninstall registry key, but being new to Inno Setup scripting, I'm not sure how to link the two parts together.
Can anyone explain how to forgo the user from entering a password if the app is already installed?
Here is the (test) script...
#define myAppID "2B7D6E48-74A8-4070-8BA7-621115D6FD00"
[Setup]
AppId={{{#myAppID}}
Password=123456
[Code]
function checkForPreviousInstall(): Boolean;
begin
Result := False;
if RegKeyExists(HKEY_LOCAL_MACHINE,'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{#myAppId}_is1') or
RegKeyExists(HKEY_CURRENT_USER, 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{#myAppId}_is1') then
begin
MsgBox('The application is installed already.', mbInformation, MB_OK);
Result := True;
end;
end;
Skip the password page, if the application is installed already.
Use ShouldSkipPage event function:
function ShouldSkipPage(PageID: Integer): Boolean;
begin
Result := False;
if (PageID = wpPassword) and checkForPreviousInstall then Result := True;
end;

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?

Inno setup search for existing file

How can I search for an existing exe file and then use that directory for my installer ?
If the exe file is not found I would like the user to browse for the path. In case the exe file is installed somewhere else.
Senario 1 (most common cases):
Default dir is c:\test\My program
This should be shown as the path on the "Select Destination Location" page
When the user press Next, there should be a check. To make sure that the default dir exist (c:\test\My program)
If it exist, the user should just continue to the Ready to Install page.
Senario 2 (very seldom cases):
Default dir is c:\test\My program
This should be shown as the path on the "Select Destination Location" page
When the user press Next, there should be a check. To make sure that the default dir exist (c:\test\My program)
If it does not exist, the user should be prompt for the path to "My program". The user should afterwards continue to the Ready to Install page.
I then just trust that the user selects the correct path
How can I do this in InnoSetup ?
I do a similar thing with my installer already. The first thing I do is need to read the registry value from the program, and if that registry value is absent then I select that program's default directory. For example:
DefaultDirName={reg:HKLM\Software\Activision\Battlezone II,STInstallDir|reg:HKLM\Software\Activision\Battlezone II,Install121Dir|{pf32}\Battlezone II}
Now, the user runs the installer, and it has to check if the program is in the right folder. It does so by checking that the program's executable file already exists. I use this piece of code to do so.
{ Below code warns end user if he tries to install into a folder that does not contain bzone.exe. Useful if user tries to install into addon or any non-BZ2 folder. }
function NextButtonClick(CurPageID: Integer): Boolean;
begin
Log('NextButtonClick(' + IntToStr(CurPageID) + ') called');
case CurPageID of
wpSelectDir:
if not FileExists(ExpandConstant('{app}\bzone.exe')) then begin
MsgBox('Setup has detected that that this is not the main program folder of a Battlezone II install, and the created shortcuts to launch {#MyAppName} will not work.' #13#13 'You should probably go back and browse for a valid Battlezone II folder (and not any subfolders like addon).', mbError, MB_OK);
end;
wpReady:
end;
Result := True;
end;
The above code simply checks that the target executable exists and warns the user if it does not, giving him the chance to go back and change directories but also to go ahead with the install anyways.
Also, as you appear to be installing a patch or addon to an existing program, I recommend you set
DirExistsWarning=no
AppendDefaultDirName=false
And optionally to prevent an unnecessary screen if you are not creating start menu entries
DisableProgramGroupPage=yes
I would make a file input page and let user choose the Picture.exe binary location manually, when it won't be found on expected location.
You can follow the commented version of this code:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output
[Files]
Source: "CurrentBinary.exe"; DestDir: "{app}"
Source: "PictureExtension.dll"; DestDir: "{code:GetDirPath}"
[Code]
var
FilePage: TInputFileWizardPage;
function GetDirPath(const Value: string): string;
begin
Result := '';
if FileExists(FilePage.Values[0]) then
Result := ExtractFilePath(FilePage.Values[0]);
end;
procedure InitializeWizard;
var
FilePath: string;
begin
FilePage := CreateInputFilePage(wpSelectDir, 'Select Picture.exe location',
'Where is Picture.exe installed ?', 'Select where Picture.exe is located, ' +
'then click Next.');
FilePage.Add('Location of Picture.exe:', 'Picture project executable|Picture.exe',
'.exe');
FilePage.Edits[0].ReadOnly := True;
FilePage.Edits[0].Color := clBtnFace;
FilePath := ExpandConstant('{pf}\Picture\Picture.exe');
if FileExists(FilePath) then
FilePage.Values[0] := FilePath;
end;

Resources