Extending event functions via #Include directive - inno-setup

I have a base inno-setup script that I use as a template for several installers. As part of the base script I have a call to the event function, NextButtonClick.
I would now like to add some additional code to the NextButtonClick event that will only be executed by one of my installers. Is there some way to "extend" the NextButtonClick event? I'm thinking of something along the lines of Python's super() function.
Inno-setup uses Pascal as a scripting language, so perhaps a Pascal expert can offer some insight.

Not directly
Remember the #include directive is just a pre-compiler directive which makes the included file to appear in the place the directive is to the inno setup script compiler.
but
To avoid including individual installer code on the template script, you can create a convention to call a procedure in the template.
The only rule you have to follow is that every installer must declare the procedure, even blank. That way, you can customize as per-installer basis while maintaining a neutral template.
Your template may be something like:
function NextButtonClick(CurPageID: Integer): Boolean;
begin
Result := BeforeNextButtonClick(CurPageID);
//if the per-installer code decides not to allow the change,
//this code prevents further execution, but you may want it to run anyway.
if not Result then
Exit;
//your template logic here
Result := Anything and More or Other;
//same here!
if not Result then
Exit;
//calling the per-installer code
Result := AfternextButtonClck(CurPageID);
end;
Then individual installers may look like this:
function BeforeNextButtonClick(CurPageID: Integer): Boolean;
begin
//specific logic here
Result := OtherThing;
end
function AfterNextButtonClick(CurPageID: Integer): Boolean;
begin
//and here, a blank implementation
Result := True;
end;
#include MyCodeTemplate.iss
Maybe it is possible to implement a complex approach, I just can't remember if PascalScript supports procedural types and no time to check with inno.
disclaimer all code written directly here to show you the idea, it may not compile.

I'm using the following workaround, which may make things hard to manage eventually, but using version control I'm able to keep a handle on it for now:
In my individual installers, I have a series of #define directives. For example:
#define IncludeSomeFeature
#define IncludeSomeOtherOption
Then in my base template, I use the #ifdef directives to optionally include different pieces of code within the Pascal scripting events:
function NextButtonClick(CurPageID: Integer): Boolean;
var
ResultCode: Integer;
begin
Result := True;
if CurPageID = wpReady then begin
#ifdef IncludeSomeFeature
... some code ...
#endif
#ifdef IncludeSomeOtherOption
... some more code ...
#endif
... code to run for every installer ...
end;
end;
The one downside to this approach is that code that the base template will slowly fill up with code that really belongs with the individual installer. However, since these are compile time directives, the resulting setup executables should not get bloated.
Really, though, my biggest problem with this approach is that it just doesn't feel like The Right Way™.

Related

Inno Setup ParseVersion is not callable from [Code]

The macros ParseVersion and RemoveBackslash, for example, are both declared in ISPPBuiltins.iss. If I attempt to call both from within [Code]:
function InitializeSetup: Boolean;
var
Major, Minor, Rev, Build: Integer;
begin
RemoveBackslash('123\');
ParseVersion('12345', Major, Minor, Rev, Build);
end;
RemoveBackslash compiles fine, but adding ParseVersion causes a compiler error:
Unknown identifier 'ParseVersion'"
When part of another macro declaration, ParseVersion seems to compile fine, just not from [Code]. Should I be able call it like that?
As #Andrew wrote already, the ParseVersion (or actually since Inno Setup 6.1, the GetVersionComponents) is a preprocessor function. So it has to be called using preprocessor directives and its results stored into preprocessor variables.
#define Major
#define Minor
#define Rev
#define Build
#expr GetVersionComponents("C:\path\MyProg.exe", Major, Minor, Rev, Build)
If you need to use the variables in the Pascal Script Code, you again need to use preprocessor syntax. For example:
[Code]
function InitializeSetup: Boolean;
begin
MsgBox('Version is: {#Major}.{#Minor}.{#Rev}.{#Build}.', mbInformation, MB_OK);
Result := True;
end;
The above is true, if you really want to extract the version numbers on the compile-time. If you actually want to do it in the Code section, i.e. on the install-time, you have to use Pascal Script support function GetVersionComponents (yes, the same name, but a different language):
[Code]
function InitializeSetup: Boolean;
var
Major, Minor, Rev, Build: Word;
Msg: string;
begin
GetVersionComponents('C:\path\MyProg.exe', Major, Minor, Rev, Build);
Msg := Format('Version is: %d.%d.%d.%d', [Major, Minor, Rev, Build]);
MsgBox(Msg, mbInformation, MB_OK);
Result := True;
end;
The Pascal Script function GetVersionComponents is available since Inno Setup 6.1 only.
The RemoveBackslash works in both contexts, as there's both Pascal Script RemoveBackslash and Preprocessor RemoveBackslash.
In the Change Log (for 6.1.x) it mentioned:
Support function GetFileVersion and ParseVersion have been renamed to GetVersionNumbersString and GetVersionComponents respectively. The old names are still supported, but it is recommended to update your scripts to the new names and the compiler will issue a warning if you don't.
So be wary of that when you upgrade. But as you rightly say, these are Inno Setup Preprocessor (ISPP) functions. With respects to the Pascal Script section there is nothing listed in the Support Function Reference.
Someone else might be able to shed more insight about this, or offer a workaround, but you might have to request the feature in the Info Setup forum.

Inno Setup Change AppName based on component(s) selected

I need the installer to show different AppName based on (un)selected components. I tried this:
[Setup]
AppName={code:GetAppName}
AppVersion=1.0
AppVerName=Dagon Video Tools
AppId=Dagon Video Tools
DefaultDirName={sd}\Games\Dagon Video Tools
[Code]
function GetAppName(Value: string): string;
var
CurPageID: Integer;
Begin
Result := 'Dagon Video Tools'
if (CurPageID=wpSelectComponents) and IsComponentSelected('Slasher') and not IsComponentSelected('Frankenstein') then
begin
Result := 'Dagon Slasher';
end;
if (CurPageID=wpSelectComponents) and IsComponentSelected('Frankenstein') and not IsComponentSelected('Slasher') then
begin
Result := 'Dagon Frankenstein';
end;
if (CurPageID=wpSelectComponents) and IsComponentSelected('Slasher') and IsComponentSelected('Frankenstein') then
begin
Result := 'Dagon Video Tools';
end;
End;
But, as you can guess, this doesn't work. Is this script incomplete or should it be done in a different way altogether?
The AppName directive value is resolved (= your GetAppName is called) immediately after the InitializeSetup (if any) finishes. That is a long before the user is able to change the components.
So you cannot make AppName depend on the selected components.
Some uses of the AppName could be overridden with a custom value though, but not all. See below.
Though, as I know that your question is actually about a setup type, you can do this:
Create custom "type" page (like a menu) as the very first one.
Once the user selects the "type", restart the installer with a custom switch (e.g. /APPTYPE=slasher) and exit.
Once the installer is (re-)run with the /APPTYPE, you know from the beginning, what component/type you are installing and hence you can set the AppName normally.
Of course, you skip the custom "type" page.
This is actually a way simpler to implement. The only drawback is that the setup window is "recreated" after the user selects the "type".
This is the original response in case you do not want to use the above solution.
First, your implementation of the GetAppName is wrong. You are using an uninitialized variable CurPageID. And anyway, as mentioned already, the GetAppName is called even before the wizard window is created, so "current page" is irrelevant here.
The correct implementation would be like:
function GetAppName(Value: string): string;
begin
if IsComponentSelected('Slasher') and not IsComponentSelected('Frankenstein') then
begin
Result := 'Dagon Slasher';
end
else
if IsComponentSelected('Frankenstein') and not IsComponentSelected('Slasher') then
begin
Result := 'Dagon Frankenstein';
end
else
begin
Result := 'Dagon Video Tools';
end;
end;
But this still won't make it working in the AppName directive. We will use it in other contexts though later.
Also note that for your specific installer, you should better use the WizardSetupType(false) function instead of the IsComponentSelected.
FinishedLabel
Just override the Inno Setup default text in CurPageChanged(wpFinished):
procedure CurPageChanged(CurPageID: Integer);
var
S: string;
begin
if CurPageID = wpFinished then
begin
S := SetupMessage(msgFinishedHeadingLabel);
StringChange(S, '[name]', GetAppName(''));
WizardForm.FinishedHeadingLabel.Caption := S;
WizardForm.AdjustLabelHeight(WizardForm.FinishedHeadingLabel);
{ Ideally we should shift the FinishedLabel up or down here, }
{ if the height of the header changed. }
{ Note that other messages (msgFinishedLabelNoIcons or msgFinishedRestartLabel) }
{ are used in special situations, so this is not a complete solution. }
S := SetupMessage(msgFinishedLabel);
StringChange(S, '[name]', GetAppName(''));
WizardForm.FinishedLabel.Caption := S;
WizardForm.AdjustLabelHeight(WizardForm.FinishedLabel);
end;
end;
Add/Remove Programs
That's easy. There's the UninstallDisplayName directive for this, which is resolved only during the actual installation, when we already know the selected components. So we can use your (fixed) GetAppName here:
[Setup]
UninstallDisplayName={code:GetAppName}
Are you sure you want to completely remove AppName and all of its components?
You cannot change that. You better use some generic name in the AppName so that this message works for any component.
Or make the message not mention the application name at all:
[Messages]
ConfirmUninstall=Are you sure you want to completely remove this game?
Alternatively remove the message completely:
Replace or customize modal uninstallation windows in Inno Setup
Please wait while AppName is removed from your computer
The same solution as for the WizardForm.FinishedLabel. Just use the UninstallProgressForm.PageDescriptionLabel from the InitializeUninstallProgressForm.
AppName was successfully removed from your computer
Similar as with the "Are you sure you want to completely remove AppName and all of its components?"
Either make the AppName generic. Or disable the message with "silent" mode and implement your own message in the CurUninstallStepChanged(usPostUninstall).
Again, see Replace or customize modal uninstallation windows in Inno Setup.
For a similar discussion, see also Changing AppName and AppDir depending on language in Inno Setup.

Can you define a function prototype in Inno Setup

I would like to be able to structure my code for my Inno Setup project but I am forced to move code around because you can't call a function unless it is defined first.
Is there a way to declare a prototype at the top so that I don't get the "Unknown identifier" error and so that I can structure my code in logical blocks.
In Pascal (including a Pascal Script used in Inno Setup), you can define a function prototype (aka forward declaration) using a forward keyword:
procedure ProcA(ParamA: Integer); forward;
procedure ProcB;
begin
ProcA(1);
end;
procedure ProcA(ParamA: Integer);
begin
{ some code }
end;
See Forward declared functions.

How to use AObject in the AddCheckBox function when using the componentslist?

I am trying to understand the AddCheckBox function in inno pascal script. The documentation is limited:
http://www.jrsoftware.org/ishelp/index.php?topic=scriptclasses
function AddCheckBox(const ACaption, ASubItem: String; ALevel: Byte; AChecked, AEnabled, AHasInternalChildren, ACheckWhenParentChecked: Boolean; AObject: TObject): Integer;
How to use the "AObject" argument? Of course I can just use "nil" but this causes an error message about a memory once the user clicks on the added checkbox.
It may be relevant that I want to add a checkbox to the componentslist (dynamically) of the WizardForm. So it is something like:
WizardForm.ComponentsList.AddCheckbox(..please help me with the correct arguments).
The function seems useful but to use it, I need to know what arguments to use and what they mean.

Is it possible to test Pascal Script functions without compiling the installer?

I am wondering if it is somehow possible to test the functions in my [Code] section without compiling the whole installer each time and run it. This would make the development and testing of the functions much easier.
Many thanks!
Sören
The direct answer to the question "Is it possible to test Pascal Script functions without compiling the installer?" is "no." Compilation is required to run the code. The real issue, I think, is not that compilation is required but rather that large projects can be time-consuming to recompile when you are performing code testing.
One workaround for time-consuming recompiles is to create a "stub" setup for code testing. Simple example:
[Setup]
AppName=TestCode
ArchitecturesInstallIn64BitMode=x64
AppVerName=TestCode
UsePreviousAppDir=false
DefaultDirName={commonpf}\TestCode
Uninstallable=false
OutputDir=.
OutputBaseFilename=TestCode
PrivilegesRequired=none
[Messages]
SetupAppTitle=TestCode
[Code]
function BoolToStr(const B: Boolean): string;
begin
if B then
result := 'true'
else
result := 'false';
end;
procedure Test();
begin
MsgBox(BoolToStr(true), mbInformation, MB_OK);
end;
function InitializeSetup(): Boolean;
begin
result := false;
Test();
end;
In the InitializeSetup event function we set result to false so nothing actually gets "installed"; the whole purpose is simply to run the Test procedure and then terminate. The Test procedure tests the sample BoolToStr function to make sure it works as expected.
When you are certain your code works as expected, you can copy it to your main project. (Don't forget to copy all dependencies such as global variables, DLL import statements, etc.)
I think yes
For compiling:
Use 'Ctrl+F9'
For testing
use only 'F9' (_without '' in inno setup).
Thanks.
EDIT:
Sorry You cant use this Processes.Look into Tlama comment.

Resources