InnoSetup with .NET installer x86/x64 sample






4.96/5 (20 votes)
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.
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.
function IsRequiredDotNetDetected(): Boolean;
begin
result := IsDotNetDetected('v4\Full', 0);
end;
If we want, we can also release a message during the setup process:
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.