Click here to Skip to main content
15,867,686 members
Articles / .NET
Tip/Trick

InnoSetup with .NET installer x86/x64 sample

Rate me:
Please Sign up or sign in to vote.
4.96/5 (20 votes)
10 Dec 2012CPOL2 min read 68.7K   30   8
Prepare an installer that verifies the system's architecture and that .NET is installed.

Introduction

The purpose of this intallation script sample is to launch an installer that checks for the .NET version currently installed on the system and install one only if required. The setup will include the web installer provided by Microsoft inside its package. 

The installer will also install some libraries depending on the architecture from where it runs.

Background

For our purposes, we need the InnoSetup compiler and the .NET Framework web installer file downloadable from the Microsoft download site.

Using the code

.NET Installer section

First of all, it's always a good practice to define the working folder where we put the files used by the installer;

Something like this:

#define MyDistFolder "E:\dist\MyApp" 

In the [Code] section, we now define the functions that check for the .NET version installed.

This code is directly taken from Jordan Russell's site.

JavaScript
function IsDotNetDetected(version: string; service: cardinal): boolean;
// Indicates whether the specified version and service pack of the .NET Framework is installed.
//
// version -- Specify one of these strings for the required .NET Framework version:
//    'v1.1.4322'     .NET Framework 1.1
//    'v2.0.50727'    .NET Framework 2.0
//    'v3.0'          .NET Framework 3.0
//    'v3.5'          .NET Framework 3.5
//    'v4\Client'     .NET Framework 4.0 Client Profile
//    'v4\Full'       .NET Framework 4.0 Full Installation
//    'v4.5'          .NET Framework 4.5
//
// service -- Specify any non-negative integer for the required service pack level:
//    0               No service packs required
//    1, 2, etc.      Service pack 1, 2, etc. required
var
    key: string;
    install, release, serviceCount: cardinal;
    check45, success: boolean;
var reqNetVer : string;
begin
    // .NET 4.5 installs as update to .NET 4.0 Full
    if version = 'v4.5' then begin
        version := 'v4\Full';
        check45 := true;
    end else
        check45 := false;

    // installation key group for all .NET versions
    key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\' + version;

    // .NET 3.0 uses value InstallSuccess in subkey Setup
    if Pos('v3.0', version) = 1 then begin
        success := RegQueryDWordValue(HKLM, key + '\Setup', 'InstallSuccess', install);
    end else begin
        success := RegQueryDWordValue(HKLM, key, 'Install', install);
    end;

    // .NET 4.0/4.5 uses value Servicing instead of SP
    if Pos('v4', version) = 1 then begin
        success := success and RegQueryDWordValue(HKLM, key, 'Servicing', serviceCount);
    end else begin
        success := success and RegQueryDWordValue(HKLM, key, 'SP', serviceCount);
    end;

    // .NET 4.5 uses additional value Release
    if check45 then begin
        success := success and RegQueryDWordValue(HKLM, key, 'Release', release);
        success := success and (release >= 378389);
    end;

    result := success and (install = 1) and (serviceCount >= service);
end;

This is a global function that verifies if the .NET Framework version passed as an argument is properly installed. 

Now we should write a simple function that is called by the installer in runtime, to verify if the required .NET version is installed. We'll use 4.0 as an example.

JavaScript
function IsRequiredDotNetDetected(): Boolean;  
begin
    result := IsDotNetDetected('v4\Full', 0);
end;

If we want, we can also release a message during the setup process:

JavaScript
function InitializeSetup(): Boolean;
begin
    if not IsDotNetDetected('v4\Full', 0) then begin
        MsgBox('{#MyAppName} requires Microsoft .NET Framework 4.0 Client Profile.'#13#13
          'The installer will attempt to install it', mbInformation, MB_OK);        
    end
    
    result := true;
end; 

The code section is complete, now we can focus on the [Files] section.

Source: "{#MyDistFolder}\dotNetFx40_Full_setup.exe"; DestDir: {tmp}; Flags: deleteafterinstall; Check: not IsRequiredDotNetDetected

The setup will copy the .NET installer file (which is a smart file of about 850 KB), only if is not already present in the system, to the system temp directory. Once the install finishes, the file will be deleted.

Now that the net web installer is copied, we need to run it if required.

In the [Run] section, write this:

Filename: {tmp}\dotNetFx40_Full_setup.exe; Parameters: "/q:a /c:""install /l 
  /q"""; Check: not IsRequiredDotNetDetected; StatusMsg: Microsoft Framework 4.0 is beïng installed. Please wait... 

And that's all.

x86/x64 options

This sample shows how the installer behaves according to the system architecture.

It will install the native libraries required by the system in order to make a more smart installation.

In the [Setup] section, define this directive:

ArchitecturesInstallIn64BitMode=x64  

These two rows will install a lib folder according to the system:

Source: "{#MyDistFolder}\lib\x64\*"; DestDir: "{app}\lib\x64"; Check: Is64BitInstallMode; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "{#MyDistFolder}\lib\x86\*"; DestDir: "{app}\lib\x86"; Check: not Is64BitInstallMode; Flags: ignoreversion recursesubdirs createallsubdirs 

Points of Interest

With just a few lines of script we are capable of making a smart setup installer, and even more a smart installation, saving space for native architecture libraries.

Sources 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Italy Italy
Creator of:
Impulse Media Player http://impulsemediaplayer.codeplex.com
Audio Pitch & Shift http://audiops.codeplex.com
Ultimate Music Tagger http://umtagger.codeplex.com
Modern Log Viewer http://modernlogviewer.codeplex.com
Pitch Tuner http://pitchtuner.codeplex.com
Modern Audio Tagger http://modernaudiotagger.codeplex.com
Win Log Inspector http://windowsloganalyzer.com/win-log-inspector/
Win Log Analyzer http://windowsloganalyzer.com/win-log-analyzer/

Comments and Discussions

 
GeneralIt uses the correct regkey for the version-check :) Pin
Michael900017-May-14 4:22
Michael900017-May-14 4:22 
QuestionInstallation Types Pin
Yonathan111110-May-14 8:24
professionalYonathan111110-May-14 8:24 
AnswerRe: Installation Types Pin
Michael900017-May-14 3:59
Michael900017-May-14 3:59 
GeneralRe: Installation Types Pin
Yonathan111117-May-14 6:00
professionalYonathan111117-May-14 6:00 
QuestionVery useful Pin
vjedlicka4-Dec-13 22:29
vjedlicka4-Dec-13 22:29 
GeneralMy vote of 5 Pin
İbrahim Ates28-Aug-13 19:18
İbrahim Ates28-Aug-13 19:18 
GeneralMy vote of 5 Pin
Gurdeep Singh Toor20-Aug-13 1:21
Gurdeep Singh Toor20-Aug-13 1:21 
GeneralMy vote of 5 Pin
Nik Todorov17-Apr-13 10:09
Nik Todorov17-Apr-13 10:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.