Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / installer

InnoSetup, Optimize your .NET Application with ngen - Part II

5.00/5 (5 votes)
8 Apr 2014CPOL1 min read 19.4K  
Run ngen to optimize your application during the installation process

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:

C++
function GetV4NetDir(version: string) : string;

var regkey, regval  : string;

begin

    // in case the target is 3.5, replace 'v4' with 'v3.5'
    // for other info, check out this link 
    // http://stackoverflow.com/questions/199080/how-to-detect-what-net-framework-versions-and-service-packs-are-installed
    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

License

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