Jump to content

Tricks for Inno Setup


Rekkio

Recommended Posts

 

Tricks for Inno Setup

 

1. Check for Windows 32-Bit

 

Inno Setup allows you to only install files / registry keys on 64-Bit Windows installs, but do not allow you to do so for 32-Bit ones.

That means if you were making a Setup that install drivers you would have Inno Setup try to install both driver versions on 64-Bit.

 

To prevent that, you can use that piece of Pascal code; It's basically a 'NOT IsWin64' but properly named:

 

function IsWin32(): Boolean;
begin
  Result := not IsWin64;
end;

 

It cans now for e.g. be used that way:

 

Source: "source\32-Bit Driver.sys"; DestDir: "{app}\Drivers"; Flags: ignoreversion; Check: IsWin32

 

2. Check if the Install directory is writable

 

Inno Setup by default doesn't check if the directory cans be written to, so it lets the user do all his Setup customizations, then fails during the install process and quits. This frustrates the user who has to relaunch the Setup and redo all his customization, without knowing in advance if the Setup won't fail again.

 

To prevent that, you can check if the directory cans be written to when the user clicks on the Next button of the Install directory page:

 

function NextButtonClick(PageId: Integer): Boolean;
begin
    Result := True;
    if (PageId = wpSelectDir) and not IsDirWritable(ExpandConstant('{app}')) then begin
        MsgBox('Cannot write in this directory !', mbError, MB_OK);
        Result := False;
        exit;
    end;
end;


You can also use a constant or a localized string (for multi-language installer interface):

 

function NextButtonClick(PageId: Integer): Boolean;
begin
    Result := True;
    if (PageId = wpSelectDir) and not IsDirWritable(ExpandConstant('{app}')) then begin
        MsgBox(ExpandConstant('{cm:LabelErrorNoWriteAccess}'), mbError, MB_OK);
        Result := False;
        exit;
    end;
end;

 

For that function to work, you need to add this code as well:

 

Spoiler

// Checks whether the specified directory can be created and written to
// by creating all intermediate directories and a temporary file.
function IsDirWritable(DirName:String):Boolean;
var
    FirstExistingDir,FirstDirToCreate,FileName:String;
begin
    Result:=True;

    // We cannot use ForceDirectories here as we need to track the first directory to be created.
    FirstExistingDir:=ExpandFileName(DirName);
    while not DirExists(FirstExistingDir) do begin
        FirstDirToCreate:=FirstExistingDir;
        FirstExistingDir:=ExtractFileDir(FirstDirToCreate);

        if FirstExistingDir=FirstDirToCreate then begin
            Result:=False;
            Exit;
        end;
    end;
    Log('Line {#__LINE__}: First directory in hierarchy that already exists is "' + FirstExistingDir + '".')

    if Length(FirstDirToCreate)>0 then begin
        Log('Line {#__LINE__}: First directory in hierarchy needs to be created is "' + FirstDirToCreate + '".')

        if ForceDirectories(DirName) then begin
            FileName:=GenerateUniqueName(DirName,'.txt');
            Log('Line {#__LINE__}: Trying to write to temporary file "' + Filename + '".')

            if SaveStringToFile(FileName,'This file is writable.',False) then begin
                if not DeleteFile(FileName) then begin
                    Result:=False;
                end;
            end else begin
                Result:=False;
            end;
        end else begin
            Result:=False;
        end;

        if not DelTree(FirstDirToCreate,True,False,True) then begin
            Result:=False;
        end;
    end;
end;

 

Spoiler

Write_Access_Check.png

 

3. Display a formatted license agreement and readme

 

You might not know it, but Inno Setup supports Rich Text files. They can be embed the same way as a .txt file:

 

LicenseFile=text\license.rtf

 

The default font for the License Agreement is Tahoma at 8.5 text size. You should edit the .rtf file with WordPad.

 

Spoiler

RTF_License.png

 

4. Display a version number at the bottom left corner

 

You might like the '2.9' text in the screens, so here's how you can do it with this piece of Pascal code:

 

procedure InitializeWizard();

var
  VersionLabel: TNewStaticText;
begin
  VersionLabel := TNewStaticText.Create(WizardForm);
  VersionLabel.Caption := Format('%s', ['any text']);
  VersionLabel.Parent := WizardForm;
  VersionLabel.Left := ScaleX(16);
  VersionLabel.Top :=
    WizardForm.BackButton.Top +
    (WizardForm.BackButton.Height div 2) -
    (VersionLabel.Height div 2)
end;

 

5. Use Inno Script Studio

 

If you don't already use it, you should. Inno Script Studio is by far the best Inno authoring software you'll find.

It has full graphical authoring support and a very neat syntax highlightning.

 

The learning curve is almost non-existent since you already use Inno Setup to begin with.

 

https://www.kymoto.org/products/inno-script-studio

 

Spoiler

Inno_Script_Studio.Main_Window.png

 

(The Pascal codes are taken from StackOverflow, slightly modified).

 

Link to comment
Share on other sites


  • Views 1.2k
  • Created
  • Last Reply

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...