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

InnoSetup, optimize your application with ngen

5.00/5 (1 vote)
3 Apr 2014CPOL1 min read 14K  
Launch ngen.exe to optimize your application during installation

Introduction

After my previous article about InnoSetup and .NET installer, I have decided to write another tip about ngen (Native Image Generator) for the .NET framework.

Background

What is ngen ? MSDN says:

The Native Image Generator (Ngen.exe) is a tool that improves the performance of managed applications. Ngen.exe creates native images, which are files containing compiled processor-specific machine code, and installs them into the native image cache on the local computer. The runtime can use native images from the cache instead of using the just-in-time (JIT) compiler to compile the original assembly.

For more details, read the original article here

Using the code

Before editing the .iss file, locate ngen.exe in your Microsoft .NET installation directory.

The path should be {win}\Microsoft.NET.

Copy ngen.exe from path

Framework/(.NET ver)/

and place into your InnoSetup distribution folder under

(innosetup_inst_folder)ngen/x86

Now do the same for ngen.exe under Framework64 folder and copy to

(innosetup_inst_folder)ngen/x64.

Innosetup binaries are prepared so it's time to edit innosetup iss file:

Your distribution folder tree should be like:

(innosetup_distribution_folder) 
    \ gen
        \x86
        ngen.exe            <-- x86 version
        \x64
        ngen.exe            <-- x64 version 

Under [Files] section, just write this code:

C++
Source: "{#MyDistFolder}\ngen\x86\ngen.exe"; DestDir: {tmp}; Flags: deleteafterinstall; Check: not Is64BitInstallMode; 
Source: "{#MyDistFolder}\ngen\x64\ngen.exe"; DestDir: {tmp}; Flags: deleteafterinstall; Check: Is64BitInstallMode;   

InnoSetup will copy the right ngen.exe into temp folder, according to the running architecture

Under [Run] section, write this

Filename: {tmp}\ngen.exe; Parameters: "install ""{app}\{#MyAppExeName}"""; StatusMsg: Optimizing performance for your system ...; Flags: runhidden

Installer will run the optimizer during installation AFTER all files (included exe) are copied, and a message "Optimizing performance for your system ..." will be shown.

At the end, we have our application with improved performances (at least Microsoft says so)

Points of Interest

We have explored some point of interests like x64 arch, temp installation directory and other stuff.

License

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