Skip to content

Instantly share code, notes, and snippets.

@canton7
Last active February 1, 2023 21:32
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save canton7/72104b1fb154442d5a9ba937fd3ee781 to your computer and use it in GitHub Desktop.
Save canton7/72104b1fb154442d5a9ba937fd3ee781 to your computer and use it in GitHub Desktop.
Installing .NET Framework 4.6.1 with Inno Setup
#define DotNetRuntimeExe "NDP461-KB3102436-x86-x64-AllOS-ENU.exe"
[CustomMessages]
InstallingDotNetFramework=Installing .NET Framework. This might take a few minutes...
DotNetFrameworkFailedToLaunch=Failed to launch .NET Framework Installer with error "%1". Please fix the error then run this installer again.
DotNetFrameworkFailed1602=.NET Framework installation was cancelled. This installation can continue, but be aware that this application may not run unless the .NET Framework installation is completed successfully.
DotNetFrameworkFailed1603=A fatal error occurred while installing the .NET Framework. Please fix the error, then run the installer again.
DotNetFrameworkFailed5100=Your computer does not meet the requirements of the .NET Framework. Please consult the documentation.
DotNetFrameworkFailedOther=The .NET Framework installer exited with an unexpected status code "%1". Please review any other messages shown by the installer to determine whether the installation completed successfully, and abort this installation and fix the problem if it did not.
[Files]
Source: "{#DotNetRuntimeExe}"; DestDir: "{tmp}"; Flags: dontcopy nocompression noencryption
[Code]
var
requiresRestart: boolean;
// Detect .NET framework 4.6.1 is missing
// See https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx
function DotNetIsMissing(): Boolean;
var
readVal: cardinal;
success: Boolean;
begin
success := RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full', 'Release', readVal);
success := success and ((readVal = 394254) or (readVal = 394271));
Result := not success;
end;
// Adapted from https://blogs.msdn.microsoft.com/davidrickard/2015/07/17/installing-net-framework-4-5-automatically-with-inno-setup/
function InstallDotNet(): String;
var
statusText: string;
resultCode: Integer;
begin
statusText := WizardForm.StatusLabel.Caption;
WizardForm.StatusLabel.Caption := CustomMessage('InstallingDotNetFramework');
WizardForm.ProgressGauge.Style := npbstMarquee;
try
ExtractTemporaryFile('{#DotNetRuntimeExe}');
if not Exec(ExpandConstant('{tmp}\{#DotNetRuntimeExe}'), '/passive /norestart /showrmui /showfinalerror', '', SW_SHOW, ewWaitUntilTerminated, resultCode) then
begin
Result := FmtMessage(CustomMessage('DotNetFrameworkFailedToLaunch'), [SysErrorMessage(resultCode)]);
end
else
begin
// See https://msdn.microsoft.com/en-us/library/ee942965(v=vs.110).aspx#return_codes
case resultCode of
0: begin
// Successful
end;
1602 : begin
MsgBox(CustomMessage('DotNetFrameworkFailed1602'), mbInformation, MB_OK);
end;
1603: begin
Result := CustomMessage('DotNetFrameworkFailed1603');
end;
1641: begin
requiresRestart := True;
end;
3010: begin
requiresRestart := True;
end;
5100: begin
Result := CustomMessage('DotNetFrameworkFailed5100');
end;
else begin
MsgBox(FmtMessage(CustomMessage('DotNetFrameworkFailedOther'), [IntToStr(resultCode)]), mbError, MB_OK);
end;
end;
end;
finally
WizardForm.StatusLabel.Caption := statusText;
WizardForm.ProgressGauge.Style := npbstNormal;
end;
end;
function PrepareToInstall(var NeedsRestart: Boolean): String;
begin
// 'NeedsRestart' only has an effect if we return a non-empty string, thus aborting the installation.
// If the installers indicate that they want a restart, this should be done at the end of installation.
// Therefore we set the global 'restartRequired' if a restart is needed, and return this from NeedRestart()
if DotNetIsMissing() then
begin
Result := InstallDotNet();
end;
end;
function NeedRestart(): Boolean;
begin
Result := requiresRestart;
end
@greeshmakr
Copy link

I am getting invalid prototype for 'InstallDotNet'. Please share your suggestion. Thanks.

@gabrielmarcussi
Copy link

When I try to install Net Framework via Inno Setup it gives me a Code error 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment