Click here to Skip to main content
15,881,559 members
Articles / Installer
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
8 Apr 2014CPOL1 min read 18.8K   12   2
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)


Written By
Software Developer (Senior)
Italy Italy
Creator of:
Impulse Media Player http://impulsemediaplayer.codeplex.com
Audio Pitch & Shift http://audiops.codeplex.com
Ultimate Music Tagger http://umtagger.codeplex.com
Modern Log Viewer http://modernlogviewer.codeplex.com
Pitch Tuner http://pitchtuner.codeplex.com
Modern Audio Tagger http://modernaudiotagger.codeplex.com
Win Log Inspector http://windowsloganalyzer.com/win-log-inspector/
Win Log Analyzer http://windowsloganalyzer.com/win-log-analyzer/

Comments and Discussions

 
QuestionInnoSetup OracleXCopySetup.msi Pin
fikri18-Apr-14 3:12
fikri18-Apr-14 3:12 
Suggestion64-bit optimization and config files Pin
Werner van Deventer8-Apr-14 20:15
Werner van Deventer8-Apr-14 20:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.