The section [Code] InnoSetup PDF Print E-mail
User Rating: / 8
PoorBest 
Wednesday, 14 May 2008

I. Introduction
First, if you do not or do not know InnoSetup, you can download for free on the publisher's site or through the pages Forum Tools VB6

The main handicap InnoSetup is, for the Francophone community, to be documented and published exclusively in English.
Some explanations, published below, are obtained either:
-- Codes examples provided with the installation of InnoSetup
-- The software documentation, translated into French by myself

There is no question of detail here all the functions of the VCL InnoSetup, but trying to understand how to use the section [Code].
The functions and procedures of the VCL will be sought in aid, depending on what you want to run.

I offer here a very large Thanks to designers of this software, which I am one of the biggest fan.

I hope this unpretentious little tutorial will help you make the best use of the power of this software.

For further information, I urge you to consult using InnoSetup, very well done, who control the language of Shakespeare.
All that I know of this software, I've learned from it, examples provided during the installation ... (and taken a few headaches!)

Prerequisites include:
A best practice software InnoSetup (knowledge of other sections standard script)
a minimum knowledge of the language Pascal or Delphi (principle and syntax)
II. The section [Code]

The section [Code] a script uses the language InnoSetup Delphi, the software is itself written with this language.

I will not dwell on the syntax of the language, nor on the part of standard script InnoSetup. (cf. Prerequisites)

We do not deal in this tutorial, the extension InnoSetup Preprocessor, which will (later ...) the subject of an upcoming course.
What can be done with the section [Code] InnoSetup?
Almost anything you want when you install a software!
Amend standard pages
Add pre-formatted pages
Creating all parts of personalized pages
Talking to the script
Creating Custom Functions
The following chapters will try to help you get the best part of these features.
III. The functions of events
The section [Code] is known event, it means that it reacts to interruptions events, like any language of this type (VB, Delphi, ...)

The events are 2 types:
The events standards: automatically generated by the script
The custom events: those that you create
III-1. The events standards
Some events are triggered during the course of the script.
Use the functions of events to alter the behavior of your installer.

I am not going to rewrite entirely using InnoSetup, but the list below gives you an idea of what and where to look:

The events standards installation InnoSetup:
InitializeSetup function (): Boolean;: called upon initialization setup (this is the 1st event which starts)
InitializeWizard procedure ();: initialization procedure General
DeinitializeSetup procedure ();: called at the end of installation (this is the last event that triggers)
CurStepChanged procedure (CurStep: TSetupStep): use this event to make your own pre-install and post-install
NextButtonClick function (CurPageID: Integer): Boolean;: activates when you press the Next button
BackButtonClick function (CurPageID: Integer): Boolean;: activates when you press the Back button
CancelButtonClick procedure (CurPageID: Integer; var Cancel, Confirm: Boolean): activates when you press the Cancel button
ShouldSkipPage function (PageID: Integer): Boolean;:
CurPageChanged procedure (CurPageID: Integer): activates when displaying a new page
CheckPassword function (Password: String): Boolean; if present in the code, causing a display box before a password
NeedRestart function (): Boolean;: return True to inform the user that the uninstall requires a restart
UpdateReadyMemo function (Space, NewLine, MemoUserInfoInfo, MemoDirInfo, MemoTypeInfo, MemoComponentsInfo, MemoGroupInfo, MemoTasksInfo: String): String;
RegisterPreviousData procedure (PreviousDataKey: Integer):
CheckSerial function (Serial: String): Boolean; if present in the code, causing a display box with a serial number on the page UserInfo
GetCustomSetupExitCode function: Integer;:
The events standards uninstalling InnoSetup:
InitializeUninstall function (): Boolean;: False returns to abandon the uninstallation, if True
DeinitializeUninstall procedure ();: starts at the end uninstall
CurUninstallStepChanged procedure (CurUninstallStep: TUninstallStep): Same as CurStepChanged, but when uninstalling
UninstallNeedRestart function (): Boolean;: return True to inform the user that the uninstall requires a restart
The draft standard 'CodeExample1.iss' shows how to use most of these events

III-2. The events personalized
It is of course possible to create its own procedures or functions of event to affect the control of personalized pages that we will build in the future.
Example procedural event for the click of a button:
[Code]
ButtonOnClick procedure (Sender: TObject);
Begin
   MsgBox ( 'You click the button! "MbInformation, mb_Ok);
end;

The binding of control and the event will take place through a property event and the operator @
Linking the procedure for event control:
Button.OnClick: = @ ButtonOnClick;

Here are examples full use of events personalized functions.

For the attention of "Delphistes" (suggested by my fellow sjrd):
The procedures and functions defined in section [Code] are actually methods (and non-routines): procedure of object or function of object.
That's why you can assign to the events of standard components.

However, Self is not available: it is used internally to contain special data, you do not want to know the nature, believe me ... ^ ^

IV. Using the code in the script
In this chapter we will see how to use the [Code] to change the pages generated by script.
IV-1. Example 1: Add a link on every page of the script
This example is adapted from the draft standard "CodeClass.iss."

Purpose:
Add a clickable link on every page.

Explanation:
Create a label which will give the appearance of a link with the Caption properties, Cursor, Font.Style, Font.Color
Link to this Label a proceeding event (URLLabelOnClick) using the property OnClick
Procedures of events
[Code]
Procedure URLLabelOnClick (Sender: TObject);
var
   ErrorCode: Integer;
Begin
   ShellExec ( 'open', 'http://www.developpez.com','','', SW_SHOWNORMAL, ewNoWait, ErrorCode);
end;

The standard procedure will be placed InitializeWizard preferably at the end of code, and anyway after all functions or procedures which it uses:
Procedure initialization
[Code]
*** *** INITIALIZING ()
Procedure InitializeWizard;
var
   URLLabel: TNewStaticText;
Begin
   URLLabel: = TNewStaticText.Create (WizardForm);
   URLLabel.Caption: = 'www.developpez.com';
   URLLabel.Cursor: = crHand;
   URLLabel.OnClick: = @ URLLabelOnClick;
   URLLabel.Parent: = WizardForm;
   Alter Font (* * after setting Parent so the correct defaults are inherited first)
   URLLabel.Font.Style: = URLLabel.Font.Style + [fsUnderline];
   URLLabel.Font.Color: = clBlue;
   URLLabel.Top: = WizardForm.ClientHeight - URLLabel.Height - 15;
   URLLabel.Left: = ScaleX (20);
end;

IV-2. Example 2: Add button 'action' on every page of the script
A Info button on every page ...
[Code]
InfosButtonOnClick procedure (Sender: TObject);
Begin
   MsgBox ( 'You click on the Info button ...', mbInformation, mb_Ok);
end;

InitializeWizard procedure ();
var
   InfosButton, CancelButton: TButton;
Begin

   CancelButton: = WizardForm.CancelButton;

   InfosButton: = TButton.Create (WizardForm);
   InfosButton.Left: = WizardForm.ClientWidth - CancelButton.Left - CancelButton.Width;
   InfosButton.Top: = CancelButton.Top;
   InfosButton.Width: = CancelButton.Width;
   InfosButton.Height: = CancelButton.Height;
   InfosButton.Caption: = '& News ...';
   InfosButton.OnClick: = @ InfosButtonOnClick;
   InfosButton.Parent: = WizardForm;
end;

V. Creating personalized pages
In this chapter we will see how to use the [Code] in order to create additional pages to be included among the pages generated by script.
V-1. The pre-formatted pages of InnoSetup
InnoSetup can generate pre-formatted pages, ie a planned acquisition of specific information:

Non-exhaustive list of these pages of preformatted InnoSetup:
TInputDirWizardPage: containing textboxs and buttons "Browse" to select directories
TInputFileWizardPage: containing textboxs and buttons "Browse" to select files
TInputOptionWizardPage: containing "Checkboxs" or "Radio buttons"
TInputQueryWizardPage: containing textboxs for entering information texts
TWizardPage: fully personalized

You can not change the shape of these pages, only add fields, corresponding to the type of information sought on each page.

V-1-1. Example 1: Insert a selection page directories
This example is extracted from a personal project.

To conduct this exercise we will use the CreateInputDirPage function

The function CreateInputDirPage:

CreateInputDirPage function (const AfterID: Integer; const ACaption, ADescription, ASubCaption: String; AAppendDir: Boolean; ANewFolderName: String): TInputDirWizardPage;
parameter AfterID, most important, specifies the page after which you want to insert page created
List of ID standards InnoSetup pages:
wpWelcome: page 'Home'
wpLicense: page info Licensing
wpPassword: page entry of a password
wpInfoBefore: page information before installation
wpUserInfo: page capture user information
wpSelectDir: page selection installation directory
wpSelectComponents: page selection of components to install
wpSelectProgramGroup: page group selection program start menu
wpSelectTasks: pages selection of additional tasks
wpReady: page info 'ready to install' (ReadyToInstall)
wpPreparing: page info 'preparation underway'
wpInstalling: page installation underway
wpInfoAfter: page information after installation
wpFinished: page 'End'


All these pages will not necessarily present in your setup. It depends on the options chosen in your script.

To insert a page after another page already created by the code, specify the ID of your previous page.
Example insertion in the wake of a standard page
[Code]
/ / The page will be inserted after the info page Licensing
PageParam: = CreateInputDirPage (wpLicense, ....);

Example insertion in the wake of another personal page
[Code]
/ / The page will be inserted after the personal page PageInfos
PageParam: = CreateInputDirPage (PageInfo.ID, ....);

Use the Add method to add textbox with a "Browse" button

NOTE: The layout is automatic, defined by the layout of the page, pre-formatted

Use Property Values to define or access values.
Procedure for setting up personalized pages
[Code]
/ / Global Variables
var
   PageParam: TInputDirWizardPage;

/ / Create Customized Pages
CreateTheWizardPages procedure;
Begin
   PageParam: = CreateInputDirPage (PageInfo.ID,
'Directories installation'
'Set registers installation of the following programs: "
'The data will be stored in the directories defined at this stage:' # 13 # 10 # 13 # 10 +
'Click on "Next" to continue. To set a different directory, click on "Browse". "
False, 'New Folder');

/ / Add an element (with a value unladen)
PageParam.Add ( 'Directory installation of NumTools');
PageParam.Add ( 'Directory installation of notepad ++');

/ / Initialize defaults (optional)
PageParam.Values [0] = 'C: \ Numtool2';
PageParam.Values [1]: = ExpandConstant ( '(pf }')+' \ notepad + +';

end;

The function UpdateReadyMemo:

In addition to this exercise, see the function UpdateReadyMemo to update the summary page standard 'ReadyToInstall':

UpdateReadyMemo function (Space, NewLine, MemoUserInfoInfo, MemoDirInfo, MemoTypeInfo, MemoComponentsInfo, MemoGroupInfo, MemoTasksInfo: String): String;

If present in the section [Code], this function is called automatically by the standard page 'ReadyToInstall'
The text must be a single chain
Using the parameter Space to insert spaces standards.
Using the parameter NewLine to define a new line.
Update Memo of the page 'Ready'
[Code]
/ / Update ReadyMemo
UpdateReadyMemo function (Space, NewLine, MemoUserInfoInfo, MemoDirInfo, MemoTypeInfo,
   MemoComponentsInfo, MemoGroupInfo, MemoTasksInfo: String): String;
var
   S, S1: String;
Begin
   (Fill the 'Ready Memo' with the normal settings and the custom settings)
   S: ='';
   S: = S + 'Install x_Edit32_mod.exe in' + NewLine;
   S: = S + + Space GetDirPath1 (S1) + + NewLine NewLine;
   S: = S + 'notepad + + is installed in' + NewLine;
   S: = S + + Space GetDirPath2 (S1) + NewLine;

   S: = S + + MemoDirInfo NewLine;
   S: = S + + MemoTasksInfo NewLine;

   Result: = S;
end;

Initialization Wizard:
Initialization
[Code]
*** *** INITIALIZING ()
Procedure InitializeWizard;
Begin
   CreateTheWizardPages;
end;

V-1-2. Example 2: Insert a page selection options
This example is taken from the project staff.

The function CreateInputOptionPage:

CreateInputOptionPage function (const AfterID: Integer; const ACaption, ADescription, ASubCaption: String; Exclusive, ListBox: Boolean): TInputOptionWizardPage;

If the Exclusive = True, will CheckBoxs replaced by Radio Buttons
If the parameter = True Listbox, CheckBoxs or Radio Buttons are placed in a ListBox
V-1-3. The personalized pages (custom page)
To create a custom page, we use the function CreateCustomPage type TWizardPage.

The function CreateCustomPage:

CreateCustomPage function (const AfterID: Integer; const ACaption, ADescription: String): TWizardPage;

This example is adapted from the draft standard "CodeClass.iss."


The elements that we want to make it accessible to the script must be declared Public

Public Statements
[Code]
/ / Global Variables
var
   Edit: TEdit;
   CheckBox1, CheckBox2: TCheckBox;
   PasswordEdit: TPasswordEdit;
   Memo: TMemo;

The controls are dynamically created and positioned on the page using their properties: Top, Left, Width and Height
Construction of the CustomPage:
[Code]
/ / Functions and procedures of events
ButtonOnClick procedure (Sender: TObject);
Begin
   MsgBox ( 'You clicked the button! "MbInformation, mb_Ok);
end;

/ / Procedure construction personal pages
CreateTheWizardPages procedure;
/ / Local variables
var
   Page: TWizardPage;
   Button: TButton;
   Lbl: TLabel;
Begin
   TButton and others ()

   Page: CreateCustomPage = (wpWelcome, 'Custom wizard page controls',' TButton and others');

   Button: TButton.Create = (Page);
   Button.Width: = ScaleX (75);
   Button.Height: = ScaleY (23);
   Button.Caption: = 'TButton';
   Button.OnClick: = @ ButtonOnClick / / affects on the procedure for the event button
   Button.Parent: = Page.Surface;

   CheckBox1: = TCheckBox.Create (Page);
   CheckBox1.Top: Button.Top = Button.Height + + ScaleY (8);
   CheckBox1.Width: = Page.SurfaceWidth;
   CheckBox1.Height: = ScaleY (17);
   CheckBox1.Caption: = 'TCheckBox 1';
   CheckBox1.Checked: = True; / / checkbox is checked by default
   CheckBox1.Parent: = Page.Surface;

   CheckBox2: = TCheckBox.Create (Page);
   CheckBox2.Top: CheckBox1.Top = CheckBox1.Height + + ScaleY (8);
   CheckBox2.Width: = Page.SurfaceWidth;
   CheckBox2.Height: = ScaleY (17);
   CheckBox2.Caption: = 'TCheckBox 2';
   CheckBox2.Checked: = False; / / checkbox is unchecked by default
   CheckBox2.Parent: = Page.Surface;

   Edit: TEdit.Create = (Page);
   Edit.Top: CheckBox2.Top = CheckBox2.Height + + ScaleY (8);
   Edit.Width: = Page.SurfaceWidth div 2 - ScaleX (8);
   Edit.Text: = 'TEdit';
   Edit.Parent: = Page.Surface;

   PasswordEdit: = TPasswordEdit.Create (Page);
   PasswordEdit.Left: = Page.SurfaceWidth - Edit.Width;
   PasswordEdit.Top: CheckBox2.Top = CheckBox2.Height + + ScaleY (8);
   PasswordEdit.Width: = Edit.Width;
   PasswordEdit.Text: = 'TPasswordEdit';
   PasswordEdit.Parent: = Page.Surface;

   Memo: TMemo.Create = (Page);
   Memo.Top: Edit.Top = Edit.Height + + ScaleY (8);
   Memo.Width: = Page.SurfaceWidth;
   Memo.Height: = ScaleY (89);
   Memo.ScrollBars: = ssVertical;
   Memo.Text: = 'TMemo';
   Memo.Parent: = Page.Surface;

   Lbl: = TLabel.Create (Page);
   Lbl.Top: Memo.Top = Memo.Height + + ScaleY (8);
   Lbl.Caption: = 'TLabel';
   Lbl.AutoSize: = True;
   Lbl.Parent: = Page.Surface;
end;

It does not forget to generate the appropriate page (s):
Initialization
[Code]
/ / Initialize
InitializeWizard procedure ();
Begin
   CreateTheWizardPages;
end;

VI. Return information to the script: functions return
To gain access, either by the code, either by the script, the values of the page created we must write the function return.

A function of return must be defined for each value to exploit.

VI-1. Example 1: Feedback type String
On one page, pre-formatted:
Functions return info type String:
[Code]
/ / Functions return
GetDirPath1 function (Param: String): String;
Begin
     Result: = PageParam.Values [0] + '\ bin';
end;
GetDirPath2 function (Param: String): String;
Begin
     Result: = PageParam.Values [1];
end;

In a personalized page:
Functions return info type String:
[Code]
/ / Functions return
GetParam1 function (Param: String): String;
Begin
   Result: = Edit.Text;
end;

In the script, use the syntax: (code: LeNomDeMaFonction)
Examples:
[Files]
Source: MonFichier.txt; Destdir: (code: GetDirPath1); Flags: promptifolder

[INI]
Filename: app) (\ istest.ini Section: InstallSettings; Flags: uninsdeletesection
Filename: app) (\ istest.ini Section: InstallSettings; Key: Param1; String: (code: GetParam1)

VI-2. Example 2: Back information type Boolean
Function return info type Boolean
[Code]
/ / Functions return
GetParam2 function: Boolean;
Begin
   Result: = CheckBox1.Checked;
end;

GetParam3 function: Boolean;
Begin
   Result: = CheckBox2.Checked;
end;

In the script, use the syntax: Check: LeNomDeMaFonction
Example:
[INI]
Filename: app) (\ MyProgram.ini Section: InstallSettings; Flags: uninsdeletesection
Filename: app) (\ MyProgram.ini Section: InstallSettings; Key: Param2; String: True; Check: GetParam2
Filename: app) (\ MyProgram.ini Section: InstallSettings; Key: Param2; String: False; Check: NOT GetParam2

You can combine the values back with all the logical operators!

An example:
[INI]
Filename: app) (\ MyProgram.ini Section: InstallSettings; Key: Param1; String: True; Check: GetParam2 AND (NOT GetParam3)

VII. Using information from the script in the code

There is a bit easier.

The information in the script, if available, can be read in the code using the ExpandConstant
ExpandConstant function (const S: String): String;

Examples:
(pf): the 'Program File'
userdata (): the 'data' of the user logged
(cm :...}: personalized messages (custom message)
....
See using InnoSetup for predefined constants

Example of use for a multilingual installer:
In the script, define:
Section [Languages]:
[Languages]
Name:; MessagesFile: compile: Default.isl;
Name: fr; MessagesFile: compile: Languages \ French.isl;

The list of languages pre-formatted is contained in the installation folder-Inno Setup; normally your directory "C: \ Program Files \ Inno Setup 5 \ Languages"
The default language is English, joined the roster default.isl
Section [CustomMessages]:
[CustomMessages]
; French
fr.whoareyou = Who are you?
fr.titleinfos = Personal Information
fr.whoareyou = Who are you?
fr.name = Name
fr.company = Company
fr.whodescription = Enter your name and your company, and click Next.

; English
en.whoareyou = Who are you?
en.titleinfos = Personal Information
en.whoareyou = Who are you?
en.name = Name:
en.company = Company:
en.whodescription = Please specify your name and the company for whom you work, then click Next.

In the code, 'custom messages' (cm) are interpreted as constants.

We will use the function ExpandConstant, cited previously, in order to exploit them:
In section [Code]:
[Code]
/ / Global Variables
var
   PageParam: TInputQueryWizardPage;

/ / Create Customized Pages
CreateTheWizardPages procedure;

Begin
/ / Create the page
PageParam: = CreateInputQueryPage (wpWelcome,
   ExpandConstant ( '(cm: titleinfos)'), ExpandConstant ( '(cm: whoareyou)'),
   ExpandConstant ( '(cm: whodescription }'));

/ / Add items (False means it's not a password edit)
PageParam.Add (ExpandConstant ( '(cm: name)'), False);
PageParam.Add (ExpandConstant ( '(cm: company)'), False);
PageParam.Values [0] = 'Valeur_1';
PageParam.Values [1] = 'Valeur_2';
end;

/ / Functions return
GetParam1 function (Param: String): String;
Begin
     Result: = PageParam.Values [0];
end;
GetParam2 function (Param: String): String;
Begin
     Result: = PageParam.Values [1];
end;

*** *** INITIALIZING ()
InitializeWizard procedure;
Begin
   CreateTheWizardPages;
end;

To use predefined constants in InnoSetup, use their name, as described above:
[Code]
TempVar: = ExpandConstant ( 'userappdata ()') + ExpandConstant ( '(username)');

VIII. Finally ...
This is a glimpse of a tiny fraction of what can be done with the section [Code] InnoSetup, but you will start in customizing your installation programs.

I invite you to consider the examples provided during installation, usually located in the directory. "\ Progam Files \ Inno Setup 5 \ Examples"
VIII-1. Links
Download the latest version of Inno-Setup
Get the latest IsTools (the site is in English, but the software offers a package in French language)

In the same series, see also: Distribute your VB6 applications with InnoSetup
VIII-2. Acknowledgments
A big THANK YOU to all those who took a few minutes of their precious time to read me and advise me, and particularly bbil and sjrd.
 
< Prev   Next >
School Joomla Templates and Joomla Tutorials