Read installation path from JSON file in Inno Setup - inno-setup

I want to create a script for Inno Setup where the install path would be taken from a file in defined directory - no registry. I suppose it would require writing specific code for it, where would be defined some variable which will contain the value after reading the file. The path and name of the file is always the same for any user so the only value that changes is the install path.
Complete structure, where InstallLocation is the variable:
{
"FormatVersion": 0,
"bIsIncompleteInstall": false,
"AppVersionString": "1.0.1",
...
"InstallLocation": "h:\\Program Files\\Epic Games\\Limbo",
...
}
Any ideas for ideal code that would do this?
Thank you

Implement a scripted constant to provide the value to DefaultDirName directive.
You can use JsonParser library to parse the JSON config file.
[Setup]
DefaultDirName={code:GetInstallLocation}
[Code]
#include "JsonParser.pas"
// Here go the other functions the below code needs.
// See the comments at the end of the post.
const
CP_UTF8 = 65001;
var
InstallLocation: string;
<event('InitializeSetup')>
function InitializeSetupParseConfig(): Boolean;
var
Json: string;
ConfigPath: string;
JsonParser: TJsonParser;
JsonRoot: TJsonObject;
S: TJsonString;
begin
Result := True;
ConfigPath := 'C:\path\to\config.json';
Log(Format('Reading "%s"', [ConfigPath]));
if not LoadStringFromFileInCP(ConfigPath, Json, CP_UTF8) then
begin
MsgBox(Format('Error reading "%s"', [ConfigPath]), mbError, MB_OK);
Result := False;
end
else
if not ParseJsonAndLogErrors(JsonParser, Json) then
begin
MsgBox(Format('Error parsing "%s"', [ConfigPath]), mbError, MB_OK);
Result := False;
end
else
begin
JsonRoot := GetJsonRoot(JsonParser.Output);
if not FindJsonString(JsonParser.Output, JsonRoot, 'InstallLocation', S) then
begin
MsgBox(Format('Cannot find InstallLocation in "%s"', [ConfigPath]),
mbError, MB_OK);
Result := False;
end
else
begin
InstallLocation := S;
Log(Format('Found InstallLocation = "%s"', [InstallLocation]));
end;
ClearJsonParser(JsonParser);
end;
end;
function GetInstallLocation(Param: string): string;
begin
Result := InstallLocation;
end;
The code uses functions from:
How to parse a JSON string in Inno Setup? (ParseJsonAndLogErrors, ClearJsonParser, GetJsonRoot, FindJsonValue and FindJsonString);
Inno Setup - Convert array of string to Unicode and back to ANSI (MultiByteToWideChar and LoadStringFromFileInCP).

Related

How to verify if program is installed in system with Inno Setup Pascal Script?

I want to verify if user has Docker installed in their system.
If it is installed proceed further otherwise display an error message.
Previously I was looking at the registry group in Windows but it's not the correct way.
I want to check if cmd gives correct output for command docker.
function GetHKLM: Integer;
begin
if IsWin64 then
Result := HKLM64
else
Result := HKLM32;
end;
function GetHKU: Integer;
begin
if IsWin64 then
Result := HKCU64
else
Result := HKCU32;
end;
function InitializeSetup: Boolean;
begin
// Opening the setup installer initially
Result := True;
//if the docker is present in the machine registry return True else checking user registry
if not RegKeyExists(GetHKLM, 'SOFTWARE\Docker Inc.') then
begin
if not RegKeyExists(GetHKU, 'Software\Docker Inc.') then
begin
// return False to prevent installation to continue
Result := False;
// Display that you need to install docker.
SuppressibleMsgBox('<Docker not found!>', mbCriticalError, MB_OK, IDOK);
end;
end;
end;
How do I do this with just cmd? Instead of checking registry.. How can I run the command line and verify the output?
for etc:
function checkdocker() :Boolean;
var
dockerfound: string;
begin
Result :=
ShellExecute(application.handle, 'docker', nil, nil, SW_MAXIMIZE)
end;
function InitializeSetup: Boolean;
begin
Result := True;
if not checkdocker then;
SuppressibleMsgBox('<Docker not found!>', mbCriticalError, MB_OK, IDOK);
else
#continue
end;
To answer your literal question: Just use Exec and check the result code:
function CheckDocker: Boolean;
var
ResultCode: Integer;
begin
Result :=
Exec('docker', '', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) and
(ResultCode = 0);
if Result then Log('Succeeded executing docker')
else Log('Failed to execute docker');
end;
(based on How to get an output of an Exec'ed program in Inno Setup?)
Though there's more efficient way to check if docker.exe executable is in a search path. Use FileSearch. See How can I check SQLCMD.EXE if it is installed on client in Inno Setup.

Check installation path for spaces and special symbol in Inno Setup

I cannot find a solution for checking the user selected path to be without any spaces or special characters.
Can you help me?
You can check for space like this:
[Code]
function NextButtonClick(CurPageID: Integer): Boolean;
var
Dir: string;
Msg: string;
begin
Result := True;
if CurPageID = wpSelectDir then
begin
Dir := WizardForm.DirEdit.Text;
if Pos(' ', Dir) > 0 then
begin
Msg := 'The path cannot contain spaces';
if WizardSilent then Log(Msg)
else MsgBox(Msg, mbError, MB_OK);
Result := False;
end;
end;
end;
You may consider using SuppressibleMsgBox function:
What does it mean that message boxes are being suppressed in Inno Setup?

Download additional files based on contents of first download using IDP

Want to download files based on contents of first download using Inno Download Plugin (IDP). How to do that?
Here is my code
[Code]
procedure InitializeWizard();
var
line: string;
line2: string;
url: string;
appname: string;
begin
idpAddFile('http://download.website.com/files.txt', ExpandConstant('{tmp}\files.txt'));
idpDownloadAfter(wpReady);
TryGetFileLine(expandConstant('{tmp}\files.txt'), 0, line);
TryGetFileLine(expandConstant('{tmp}\files.txt'), 1, line2);
url := line;
appname := line2;
idpAddFile(url, ExpandConstant('{tmp}\'+appname));
idpDownloadAfter(wpReady);
end;
Here second file starts downloading before the first file finishes. So how to make it one after another?
Tell IDP to only download the list initially. Then wait for the download to finish (for that see Running a program after it is downloaded in Code section in Inno Setup) and based on the results, create new download list and restart the download.
var
ListDownloaded: Boolean;
procedure InitializeWizard();
begin
idpAddFile('http://www.example.com/files.txt', ExpandConstant('{tmp}\files.txt'));
idpDownloadAfter(wpReady);
ListDownloaded := False;
end;
function NextButtonClick(CurPageID: Integer): Boolean;
var
Url, AppName: string;
begin
Result := True;
if CurPageID = IDPForm.Page.ID then
begin
if not ListDownloaded then
begin
TryGetFileLine(ExpandConstant('{tmp}\files.txt'), 0, Url);
TryGetFileLine(ExpandConstant('{tmp}\files.txt'), 1, AppName);
idpClearFiles;
idpAddFile(Url, ExpandConstant('{tmp}\' + AppName));
idpFormActivate(nil); { This restarts the download }
Result := False;
ListDownloaded := True;
end;
end;
end;

I want to write a log file instead of message box in inno setup

I tried to use the following code but the log is not writing
procedure CurStepChanged(CurStep: TSetupStep);
var
logfilepathname, logfilename, newfilepathname: string;
begin
logfilepathname := ExpandConstant('{log}');
logfilename := ExtractFileName(logfilepathname);
newfilepathname := ExpandConstant('C:\Spectrum\StaticFilesLog\') + logfilename;
if CurStep = ssDone then
begin
FileCopy(logfilepathname, newfilepathname, false);
end;
end;
function SQLServerInstallation: Boolean;
begin
if (IsSQlServer2012Present = True) then
begin
Result := True;
end
else if (IsSQlServer2005Present = True) then
begin
Log('File : ' + 'Static file installation'); // this is not working
Log('SQL server 2005 is present in your machine. Please install SQL');
// It should write the log but not writing
ExitProcess(0);
end
else
begin
Log('File : ' + 'Static file installation'); // This is not working
Log('SQL server was not installed in your machine please install 2005')
// It should write the log but not writing
ExitProcess(0);
end;
end;

Use a part of a registry key/value in the Inno Setup script

I have a need to retrieve a path to be used for some stuffs in the installer according an other application previously installed on the system.
This previous application hosts a service and only provides one registry key/value hosting this information: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\APPLICATION hosting the value ImagePath which Data is "E:\TestingDir\Filename.exe".
I need a way to only extract the installation path (E:\TestingDir) without the Filename.exe file.
Any suggestion?
thanks a lot
You can achieve this using a scripted constant.
You define a function that produces the value you need:
[Code]
function GetServiceInstallationPath(Param: string): string;
var
Value: string;
begin
if RegQueryStringValue(
HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Services\APPLICATION',
'ImagePath', Value) then
begin
Result := ExtractFileDir(Value);
end
else
begin
Result := { Some fallback value }
end;
end;
And then you refer to it using {code:GetServiceInstallationPath} where you need it (like in the [Run] section).
For example:
[Run]
Filename: "{code:GetServiceIntallationPath}\SomeApp.exe"
Actually, you probably want to retrieve the value in InitializeSetup already, and cache the value in a global variable for use in the scripted constant. And abort the installation (by returning False from InitializeSetup), in case the other application is not installed (= the registry key does not exist).
[Code]
var
ServiceInstallationPath: string;
function InitializeSetup(): Boolean;
var
Value: string;
begin
if RegQueryStringValue(
HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Services\APPLICATION',
'ImagePath', Value) then
begin
ServiceInstallationPath := ExtractFileDir(Value);
Log(Format('APPLICATION installed to %s', [ServiceInstallationPath]));
Result := True;
end
else
begin
MsgBox('APPLICATION not installed, aborting installation', mbError, MB_OK);
Result := False;
end;
end;
function GetServiceInstallationPath(Param: string): string;
begin
Result := ServiceInstallationPath;
end;
See also a similar question: Using global string script variable in Run section in Inno Setup.
Solved this way:
[code]
var
ServiceInstallationPath: string;
function MyProgCheck(): Boolean;
var
Value: string;
begin
if RegQueryStringValue(
HKEY_LOCAL_MACHINE, 'SYSTEM\ControlSet001\Services\JLR STONE VCATS TO MES',
'ImagePath', Value) then
begin
ServiceInstallationPath := ExtractFileDir(Value);
Result := True;
end
else
begin
Result := False;
end;
end;
and in the [RUN] section I put as check the TRUE condition or FALSE condition on this function according the needs...Thanks everybody answering!

Resources