In this article, we would see how to detect whether the .NET Framework is present or not, and how to install the .NET Framework automatically from the internet (Microsoft Website), if its already not present. The installation won’t continue, until the .NET Framework is installed. This article is basically a modification of the script at http://www.blackhillsoftware.com/blog/2006/06/26/using-innosetup-with-the-dotnet-framework/ and uses information from the Microsoft .NET Framework 3.5 Deployment Guide for Application Developers, to detect the .NET Framework 3.5 version.
Below is the INNO Setup script to detect and install the .NET 3.5 Framework. If you want some other version of .NET Framework, to be detected and installed automatically, just change the registry values to search and the dotnetRedistURL value to download that .NET Framework. To know about the exact registry values and keys to be used for detection of a particular version of .NET, you can read the article: Using managed code to detect what .NET Framework versions and service packs are installed.
| Framework Version | Registry Key |
|---|---|
| .NET Framework v1.1 | HKLM\Software\Microsoft\NET Framework Setup\NDP\v1.1.4322\Install |
| .NET Framework v2.0 | HKLM\Software\Microsoft\NET Framework Setup\NDP\v2.0.50727\Install |
| .NET Framework v3.5 | HKLM\Software\Microsoft\NET Framework Setup\NDP\v3.5\Install |
All of these values are a DWord value, so if it is present and set to 1, then that version of the Framework is installed.
| Framework Version | Download URL |
|---|---|
| .NET Framework v1.1 | http://download.microsoft.com/download/a/a/c/aac39226-8825-44ce-90e3-bf8203e74006/dotnetfx.exe |
| .NET Framework v2.0 | http://download.microsoft.com/download/5/6/7/567758a3-759e-473e-bf8f-52154438565a/dotnetfx.exe |
| .NET Framework v3.5 | http://download.microsoft.com/download/7/0/3/703455ee-a747-4cc8-bd3e-98a615c3aedb/dotNetFx35setup.exe |
Please note that these are temporary download URL and subject to change by Microsoft, also these download are for normal Windows 32 bit architecture.
[_ISTool]
EnableISX=true
[Files]
Source: C:\Program Files\ISTool\isxdl.dll; Flags: dontcopy
[Code]
var
dotnetRedistPath: string;
downloadNeeded: boolean;
dotNetNeeded: boolean;
memoDependenciesNeeded: string;
procedure isxdl_AddFile(URL, Filename: PChar);
external 'isxdl_AddFile@files:isxdl.dll stdcall';
function isxdl_DownloadFiles(hWnd: Integer): Integer;
external 'isxdl_DownloadFiles@files:isxdl.dll stdcall';
function isxdl_SetOption(Option, Value: PChar): Integer;
external 'isxdl_SetOption@files:isxdl.dll stdcall';
const
dotnetRedistURL = 'http://download.microsoft.com/download/7/0/3/703455ee-a747-4cc8-bd3e-98a615c3aedb/dotNetFx35setup.exe';
function InitializeSetup(): Boolean;
var
IsInstalled: Cardinal;
begin
Result := true;
dotNetNeeded := true;
// Check for required netfx installation
if(Is64BitInstallMode()) then begin
if (RegValueExists(HKLM, 'SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v3.5', 'Install')) then begin
RegQueryDWordValue(HKLM, 'SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v3.5', 'Install', IsInstalled);
if(IsInstalled = 1) then begin
dotNetNeeded := false;
downloadNeeded := false;
end;
end;
end
else begin
if (RegValueExists(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5', 'Install')) then begin
RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5', 'Install', IsInstalled);
if(IsInstalled = 1) then begin
dotNetNeeded := false;
downloadNeeded := false;
end;
end;
end;
if(dotNetNeeded) then begin
if (not IsAdminLoggedOn()) then begin
MsgBox('My Application needs the Microsoft .NET 3.5 Framework to be installed by an Administrator.', mbError, MB_OK);
Result := false;
end
else begin
dotnetRedistPath := ExpandConstant('{src}\dotnetfx.exe');
if not FileExists(dotnetRedistPath) then begin
dotnetRedistPath := ExpandConstant('{tmp}\dotnetfx.exe');
if not FileExists(dotnetRedistPath) then begin
isxdl_AddFile(dotnetRedistURL, dotnetRedistPath);
downloadNeeded := true;
end;
end;
end;
end;
end;
function NextButtonClick(CurPage: Integer): Boolean;
var
hWnd: Integer;
ResultCode: Integer;
begin
Result := true;
if CurPage = wpReady then begin
hWnd := StrToInt(ExpandConstant('{wizardhwnd}'));
// don't try to init isxdl if it's not needed because it will error on < ie 3
if (downloadNeeded) then begin
isxdl_SetOption('label', 'Downloading Microsoft .NET 3.5 Framework');
isxdl_SetOption('description', 'My Application needs to install the Microsoft .NET 3.5 Framework. Please wait while setup is downloading extra files to your computer.');
if isxdl_DownloadFiles(hWnd) = 0 then Result := false;
end;
if (Result = true) and (dotNetNeeded = true) then begin
if Exec(ExpandConstant(dotnetRedistPath), '/q /norestart', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then begin
// handle success if necessary; ResultCode contains the exit code
if not (ResultCode = 0) then begin
Result := false;
end;
end
else begin
// handle failure if necessary; ResultCode contains the error code
Result := false;
end;
end;
end;
end;