Click here to Skip to main content
Click here to Skip to main content

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

, 8 Apr 2014
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

Fabrizio Stellato
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
Follow on   LinkedIn

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 8 Apr 2014
Article Copyright 2014 by Fabrizio Stellato
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid