Introduction
This is an update to my previous article of running ngen while installing a .NET application.
Unlike the older article, this installation will not bring any ngen file within the installer, and make safe that ngen is the same file that is installed in the user system.
Background
In this tip, we'll configure InnoSetup to run ngen directly from the .NET framework working directory.
The main purpose is to locate the right path of the .NET Framework needed using the windows registry.
In this example, I have used .NET 4.0, but this example should work also for 3.5 and 4.5.
Btw, a similar solution might be used also for .NET version < 3.5, because older version probably have the same installation directory and we can use a static path.
Using the Code
As the previous article provided, we already have the code needed to check if .NET Framework required is installed, so let's assume we use the function IsRequiredDotNetDetected
to check whatever our .NET is installed.
Then, we write a function that retrieves .NET install path:
function GetV4NetDir(version: string) : string;
var regkey, regval : string;
begin
regkey := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'
RegQueryStringValue(HKLM, regkey, 'InstallPath', regval);
result := regval;
end;
On the [Run] section, we add this line:
Filename: {code:GetV4NetDir}ngen.exe; Parameters: "install ""{app}\{#MyAppExeName}""";
StatusMsg: Optimizing performance for your system ...; Flags: runhidden; Check: CheckFramework;
The code above will run ngen.exe with install parameter (directly from the framework installation directory) on our main executable.
If we want to do stuff more clean, we can also add this line on the [UninstallRun]
section.
Filename: {code:GetV4NetDir}ngen.exe; Parameters: "uninstall ""{app}\{#MyAppExeName}""";
StatusMsg: Removing native images and dependencies ...; Flags: runhidden; Check: CheckFramework;
In the same way, ngen will remove native images of our application before doing an uninstall.
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