Can I turn this into a [Run] entry in Inno Setup? [duplicate] - inno-setup

At the end of an installation I need to run a Pascal function that updates a flight simulator .cfg file (called .ini file in Inno Setup). The Pascal function exists in its [Code] section and runs correctly. I would like to run this Pascal function in the [Run] section using StatusMsg to tell the user what is going on.
[Run]
Filename: {code:FsxEditSceneryFile|Add#<scenerySpec>}; StatusMsg: "Add scenery to FSX";
; <scenerySpec> is just a place holder of the actual scenery specification!
All works as expected with the exception that Inno Setup forces me to use a string as return value of the Pascal function. However the Filename statement requires a Boolean as return value to specify if the execution was successful (True) or failed (False). This type mismatch produces an error message box at the end of the execution of the Filename statement saying
CreateProcess failed; Code 87. Wrong Parameter.
Any proposal how this could be solved? I know that event functions exist that I could use e.g. CurStepChanged() but I find the StatusMsg mechanism very nice to tell the user what is done by the installation.

You are abusing Filename parameter resolution to execute some code. It's undocumented when the parameter value is resolved. This makes your approach unreliable. You cannot know that the value is resolved, while the StatusMsg is displaying. And moreover, the value must resolve to an executable path anyway. And Inno Setup will try to execute it (hence the error). What you probably do not want. Do not do that.
Instead, as you already suggested, use the CurStepChanged. You can display the status message from Pascal code by accessing the WizardForm.StatusLabel.
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssPostInstall then
begin
WizardForm.StatusLabel.Caption := 'Installing something...';
{ Install something }
end;
end;

In case somebody else is stumbling across this problem, here's a solution that will allow you to run pascal script procedures "in place" of any statement in the [RUN] section.
I needed to move the execution of a powershell script to the [CODE] section, since the [RUN] section doesn't provide a way of reacting to the exit code of external cmd programs.
As I've got a bunch of sub installers that I need to execute in a particular order within my setup, it's really handy to have the option to control the position of the pascal script within the [RUN] section and not just when entering the [RUN] sections setup step.
It works by using a "dummy" program (like ping) as Filename for the [RUN] statement, then using the build-in BeforeInstall parameter to call the pascal script procedure containing the actual execution logic.
[RUN] section
Filename: "ping"; BeforeInstall: RunSQLSetupScript; Components: "thirds\db"; StatusMsg: "Installing SQL Server ..." ; Flags: runhidden
Pascal script procedure containing the actual execution logic
(Note that {tmp}\{#SQLServerInstallScript} would be the actual path to the script in this example, as it is generally advised to avoid hard coded paths and use constants + the temporary directory instead.)
// runs the SQL setup script and terminates the setup process, if the script returns an exit code that indicates an error
procedure RunSQLSetupScript();
var
runSuccess: Boolean;
retVar: Integer;
msgText: String;
begin
runSuccess := ShellExec('', 'powershell.exe', '-NoProfile -File ' + ExpandConstant('{tmp}\{#SQLServerInstallScript}'), '', SW_SHOW, ewWaitUntilTerminated, retVar);
// the external script will return an exit code > 0 if an error occurred
if (runSuccess = False) or (retVar > 0) then
begin
msgText := 'SQL Server setup script returned error code ' + IntToStr(retVar) + ', indicating an unsuccessful installation of SQL Server. Setup will now terminate.';
MsgBox(msgText, mbCriticalError, MB_OK);
// => further handle error case here, like cancelling the running setup or log the issue etc.
end;
end;

Related

Innosetup return flag [duplicate]

This question already has an answer here:
Inno Setup Section [Run] with condition
(1 answer)
Closed 4 years ago.
I am working on compiling an Inno Setup project. What I am trying to do is check if a folder exists, and if the folder does not exist then I want to uncheck a checkbox in the [run] section.
I was trying to accomplish this through the [Code] section. However, I cannot figure out how to call the function in the flags of my [Run] section.
In my code section, I have the following function that checks to see if the directory exists, if it does not then I attempt to set the flag to be unchecked if the directory does exist I just return any flag.
[Code]
function VerifyDir(DirName: String): Flag;
begin
{Check if directory exists, if it does then set the check flag to unchecked}
if not DirExists(DirName) then
Result := unchecked
end;
{Directory Exists return a flag}
Result := nowait
end;
Then in my [Run] section, I attempt to pass back the flag from the function like so:
[Run]
Filename: C:\3S\LegacyAppFolder\Update.exe; Description: Blah Blah Blah; \
Flags: VerifyDir('C:\3S\LegacyAppFolder')
However, I get an error when I try to compile the installer
Parameter "Flags" includes an unknown flag.
I assume this is because either I cannot have an inline function and I need to go about this a different way, or this is not possible at all.
You don’t want to use the flags section to do the test.
If you look here you will see that the right thing to do is use:
Check: xxxxxxx
If the check function returns true the statement is processed.

Inno Setup: Execute a Pascal function in [Run] section

At the end of an installation I need to run a Pascal function that updates a flight simulator .cfg file (called .ini file in Inno Setup). The Pascal function exists in its [Code] section and runs correctly. I would like to run this Pascal function in the [Run] section using StatusMsg to tell the user what is going on.
[Run]
Filename: {code:FsxEditSceneryFile|Add#<scenerySpec>}; StatusMsg: "Add scenery to FSX";
; <scenerySpec> is just a place holder of the actual scenery specification!
All works as expected with the exception that Inno Setup forces me to use a string as return value of the Pascal function. However the Filename statement requires a Boolean as return value to specify if the execution was successful (True) or failed (False). This type mismatch produces an error message box at the end of the execution of the Filename statement saying
CreateProcess failed; Code 87. Wrong Parameter.
Any proposal how this could be solved? I know that event functions exist that I could use e.g. CurStepChanged() but I find the StatusMsg mechanism very nice to tell the user what is done by the installation.
You are abusing Filename parameter resolution to execute some code. It's undocumented when the parameter value is resolved. This makes your approach unreliable. You cannot know that the value is resolved, while the StatusMsg is displaying. And moreover, the value must resolve to an executable path anyway. And Inno Setup will try to execute it (hence the error). What you probably do not want. Do not do that.
Instead, as you already suggested, use the CurStepChanged. You can display the status message from Pascal code by accessing the WizardForm.StatusLabel.
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssPostInstall then
begin
WizardForm.StatusLabel.Caption := 'Installing something...';
{ Install something }
end;
end;
In case somebody else is stumbling across this problem, here's a solution that will allow you to run pascal script procedures "in place" of any statement in the [RUN] section.
I needed to move the execution of a powershell script to the [CODE] section, since the [RUN] section doesn't provide a way of reacting to the exit code of external cmd programs.
As I've got a bunch of sub installers that I need to execute in a particular order within my setup, it's really handy to have the option to control the position of the pascal script within the [RUN] section and not just when entering the [RUN] sections setup step.
It works by using a "dummy" program (like ping) as Filename for the [RUN] statement, then using the build-in BeforeInstall parameter to call the pascal script procedure containing the actual execution logic.
[RUN] section
Filename: "ping"; BeforeInstall: RunSQLSetupScript; Components: "thirds\db"; StatusMsg: "Installing SQL Server ..." ; Flags: runhidden
Pascal script procedure containing the actual execution logic
(Note that {tmp}\{#SQLServerInstallScript} would be the actual path to the script in this example, as it is generally advised to avoid hard coded paths and use constants + the temporary directory instead.)
// runs the SQL setup script and terminates the setup process, if the script returns an exit code that indicates an error
procedure RunSQLSetupScript();
var
runSuccess: Boolean;
retVar: Integer;
msgText: String;
begin
runSuccess := ShellExec('', 'powershell.exe', '-NoProfile -File ' + ExpandConstant('{tmp}\{#SQLServerInstallScript}'), '', SW_SHOW, ewWaitUntilTerminated, retVar);
// the external script will return an exit code > 0 if an error occurred
if (runSuccess = False) or (retVar > 0) then
begin
msgText := 'SQL Server setup script returned error code ' + IntToStr(retVar) + ', indicating an unsuccessful installation of SQL Server. Setup will now terminate.';
MsgBox(msgText, mbCriticalError, MB_OK);
// => further handle error case here, like cancelling the running setup or log the issue etc.
end;
end;

Can I put setup command line parameters in a file which is called during installation instead?

After creating my setup.exe I have to pack it for various software deployment tools. Therefore I can't call the setup.exe with parameters, instead I have placed my own parameters in a setup.ini file next to the setup.exe
[Code]
var
MyIniFile: String;
function InitializeSetup(): Boolean;
var
LoadFromIniFile: String;
begin
Result := true;
MyIniFile := ExpandConstant('{srcexe}'); //writes the full path of the setup.exe in "MyIniFile"
MyIniFile := Copy(MyIniFile, 1, Length(MyIniFile) - Length(ExtractFileExt(MyIniFile))) + '.ini'; //changes the ".exe" into ".ini"
if FileExists(MyIniFile) then LoadFromIniFile := MyIniFile; //checks wether there is a ini-file
if LoadFromIniFile <> '' then begin
MyLogFile := GetIniString('Setup', 'Log', MyLogFile , LoadFromIniFile);
ProductName := GetIniString('Setup', 'ProductName', ProductName, LoadFromIniFile);
end;
end;
Now I want to also place the so called "Setup Command Line Parameters" (listed on the Inno Setup Help site) in my ini-file. I think that there is a way for the /Dir="x:\dirname parameter, which I did not figure out yet. But I also want to have the /SILENT parameter in there, do you think there is a way to do this? If yes, how would you do this? If not, can you please give me a hint why not?
So customize your installer for different products, I'd recommend you to use a pre-processor and automatically build the installer for each product (with different "defines"), instead of using an external INI file.
For example to be able to change application name and resulting executable when building the installer, use a script like:
[Setup]
AppName={#AppName}
OutputBaseFilename={#BaseFilename}
Now you can create two different installers automatically using command-line:
ISCC.exe Example1.iss /dAppName=App1 /dBaseFilename=SetupApp1
ISCC.exe Example1.iss /dAppName=App2 /dBaseFilename=SetupApp2
Regarding the implicit silent installation:
There's no API other than the command-line /SILENT switch to trigger silent installation.
But you can create a near-silent installation by disabling most installer pages:
[Setup]
DisableWelcomePage=true
DisableDirPage=true
DisableProgramGroupPage=true
DisableReadyPage=true
DisableFinishedPage=true
Actually the above example disables all default pages. But Inno Setup compiler will ignore the DisableReadyPage=true, if all other previous pages are disabled.
You may want to choose a different page to show instead. For example a Welcome page (by omitting DisableWelcomePage=true, but keeping the DisableReadyPage=true).
If you do not mind about using external files (as you already use an external INI file), you can of course wrap the installer to a batch file and call the installer with the /SILENT switch.

InnoTools Downloader not working with Inno 5.5

On the recommendation of several posts here on SO, I've been working with the InnoTools Downloader to try to install a 3rd party dependency for our app during the Install script in Inno setup.
Unfortunately the InnoTools Downloader hasn't been updated in a few years, and is starting to look like it's incompatible with the current Inno Tools setup (5.5.2 (u) on my machine at present). The PChar parameters in ITD have been replaced by PAnsiChar parameters, and when I try to run the various ITD_xxx procedures it gives me varying degrees of fail:
ITD_DownloadFiles gives a type mismatch error and won't compile in Inno Setup
ITD_DownloadFile compiles, but the file that shows up is 6KB in length and not runnable.
Has anyone gotten ITP to run with newer Inno (post-5.3.0) unicode versions? Or should I look around for another solution?
EDIT
Just to clarify things a bit, I have tried going into the it_download.iss file and replacing all instances of PChar with PAnsiChar. This got me past the compile errors when I first tried to integrate ITD with my setup script.
Here is a sample section of the Inno script:
[Code]
procedure InitializeWizard();
begin
ITD_Init; // initialize the InnoTools Downloader
// install 3rd party tool (ex. Git) from the internet.
if ITD_DownloadFile('http://git-scm.com/download/win',expandconstant('{tmp}\GitInstaller.exe'))=ITDERR_SUCCESS then begin
MsgBox(expandconstant('{tmp}\GitInstaller.exe'), mbInformation, MB_OK);
Exec(ExpandConstant('{tmp}\GitInstaller.exe'), '', '', SW_SHOW, ewWaitUntilTerminated, tmpResult);
end
end;
When this is run, it will bring up a dialog stating where it "downloaded" and stored the file -- on my machine it's in a subdirectory of c:\Users\\AppData\Local\Temp. This file is 6KB, as opposed to the file downloaded from http://git-scm.com/download/win, which is currently 15,221KB.
The ITP_DownloadAfter method gives a similar result.
Except replacing all PChar type occurrences with PAnsiChar you will need to replace all occurrences of string type with AnsiString in the it_download.iss file. The next problem is the URL you're trying to get. The size of a file differs from expectation, because you are downloading a HTML document instead of the binary file on which that site redirects. So, if you have your ITD ready for Unicode, change the URL in your script to a direct binary URL. Note, that I didn't used HTTPS because ITD doesn't currently support SSL. A code proof may look like this:
[Code]
const
GitSetupURL = 'http://msysgit.googlecode.com/files/Git-1.8.4-preview20130916.exe';
procedure InitializeWizard;
var
Name: string;
Size: Integer;
begin
Name := ExpandConstant('{tmp}\GitInstaller.exe');
ITD_Init;
if ITD_DownloadFile(GitSetupURL, Name) = ITDERR_SUCCESS then
begin
if FileSize(Name, Size) then
MsgBox(Name + #13#10 + 'Size: ' + IntToStr(Size) + ' B',
mbInformation, MB_OK)
else
MsgBox('FileSize function failed!', mbError, MB_OK);
end;
end;

Inno setup : Custom messages

We package few other thirdparty softwares along with our installer,and also install them during
the installation of our product. We install them in silent mode and capture their exit codes,
so sotimes, they get installed successfully and give exitcode as "3010" which is reboot required.
So, in those cases we want to show reboot page at the last but want to give a custom message.
what is the best way to show the custom message on the finish page?
[Messages]
#if FileExists("c:\RebootFile.txt")==0
FinishedRestartLabel=To complete the installation of ConditionalMessageOnWizard, Setup must restart your computer. Would you like to restart now?
#else
FinishedRestartLabel=Reboot Required
#endif
I am using the above code, but i am unable to use the dynamic paths like {sd} or {tmp} for fileexists function.
Can anyone help?
After clarification of your problem, we've found that you actually want to check if a certain file exixts and conditionally change the FinishedLabel caption at runtime.
The #emit, or in short # starting statements are used by preprocessor. And preprocessing runs right before the compilation. It allows you to conditionally modify the script, which is, after this process is done, compiled. So, with your above script you are in fact checking, if the file c:\RebootFile.txt exists on the machine where the setup is being compiled and depending on the result it then chooses value of the FinishedRestartLabel message. But it never compiles both texts into the setup binary.
You can modify the FinishedLabel caption from code e.g. this way. There you can expand constants without any problem:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
function NeedRestart: Boolean;
begin
Result := True;
end;
procedure CurPageChanged(CurPageID: Integer);
begin
if not FileExists(ExpandConstant('{sd}\RebootFile.txt')) then
WizardForm.FinishedLabel.Caption := 'RebootFile NOT found. Restart ?'
else
WizardForm.FinishedLabel.Caption := 'RebootFile WAS found. Restart ?';
end;

Resources