Native Images: Basics, Generation and Usage in MSI Packages
One of the most often cited reasons to not use .NET is that it is initially compiled to an intermediary language (MSIL) and has to be recompiled every time you run it. In many high performance environments this wasted time is simply unacceptable. To combat this Microsoft released a tool with .NET 1.1 called NGen (Native Image Generator) which allows you do pre-compile MSIL to real machine code and in so doing avoid much of the extra run-time overhead.
Overview of Native Image Generator:
The Native Image Generator (ngen.exe) creates a native image from a managed assembly and installs it into the native image cache on the local computer. The native image cache is a reserved area of the global assembly cache. Once you create a native image for an assembly, the runtime automatically uses that native image each time it runs the assembly. You do not have to perform any additional procedures to cause the runtime to use a native image. Running Ngen.exe on an assembly allows the assembly to load and execute faster, because it restores code and data structures from the native image cache rather than generating them dynamically.
How Native Generator Works
Ngen.exe does not use standard assembly probing rules to locate the assemblies you specify on the command line. Ngen.exe looks only in the current directory for assemblies that you specify. Therefore, to allow Ngen.exe to locate your assemblies, you should either set your working directory to the directory that contains the assemblies you want to create native images for or specify exact paths to the assemblies.
This tool can be located at <drive>:\WINNT\Microsoft.NET\Framework\<version>\ngen.exe
This tool is used to create a native Image from a .NET assembly and installs it into the native image cache on that computer. Since assembly image is present on the local machine cache loading of the assembly becomes faster because .NET reads data from the native image than generating them dynamically (JIT). Pre-compiling assemblies with Ngen.exe can improve the startup time for applications, because much of the work required to execute code has been done in advance.
The default usage for NGen is extremely simple: ngen install aclayer.dll
This will generate native images for aclayer.dll and all of its dependencies and create a native image for this dll in C:\Winnt\Assembly\Native Images as aclayer.ni.dll.
This process can be quite slow. For larger applications you may wish to use the /queue option which will queue up aclayer.dll, and all of its dependencies, so that they will be converted by the Native Image Service. This will happen in the background, at the service's earliest convenience. Once the native image is generated it will be stored in the native image cache and used automatically.
Advantages of Using Native Generator:
- Reduced Memory Footprint
- Faster Program Load Time
- Code Sharing Between Processes
Points to Remember:
Do not confuse these mechanisms with the old COM registration mechanism. COM suffers from the infamous DLL Hell, while as the .NET Framework does not suffer from this problem (cf. versioning). Furthermore, the use of the registry for "registration" and "dependency tracking" is a NGen-only thing in the world of .NET, assemblies can be xcopy deployed. Also notice that the assemblies act as a kind of stubs to the native images when such a native image exists. You can't delete the testhelper.dll file in order to run test.exe or test2.exe although it resides in the NGen cache. If you'd like to do this, you'll need to register the assembly in the GAC (thus requiring strong naming). Thus, ngen.exe is not a .NET look-alike for regsvr32.exe.
The NGen install and NGen uninstall actions works fine. It's worth to mention that a fully-qualified assembly name can be used to perform the install action. Notice that such a fully-qualified name in .NET v2.0 has now 5 components: name (test), version (0.0.0.0), culture (neutral), public key token (...) and processor architecture (MSIL).
Two other useful features (flags at the command line) are:
- Scenarios: used to generate native images that can be used by a debugger (/debug) or a profiler (/profile) or to generate a minimum number of native images by not ngen-ing dependencies (/nodependencies).
- Config: /ExeConfig to point to a configuration file (.exe.config) that contains additional information used by ngen and /AppBase to override assembly probing settings by specifying an "appbase directory" to search for assemblies in.
Usage:
While Installing:
Identify the Assembly which creates these native images, mention the same path in these scripts. Example: C:\Program FIles\DWG TrueView 2008\AcLayer.dll. For multiple assemblies use the function mentioned.
Use this script in your MSI Package.
Option Explicit
Dim wshShell, ngen, FSO, windir, assembly,PrgFiles
Set wshShell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
windir = wshShell.ExpandEnvironmentStrings("%Windir%")
PrgFiles = wshShell.ExpandEnvironmentStrings("%ProgramFiles%")
ngen = windir & "\Microsoft.NET\Framework\v2.0.50727\ngen.exe"
if FSO.FileExists(ngen) then
assembly=chr(34) & PrgFiles & "\DWG TrueView 2008\AcLayer.dll" & chr(34)
Generate(assembly)
end if
set FSO = nothing
set wshShell = nothing
Function Generate (Byval file)
Dim strCmd,wshShell1
Set wshShell1 = CreateObject("WScript.Shell")
strCmd= chr(34) & ngen & chr(34) & " install " & file
wshShell1.run strCmd,0
Set WshShell1 = nothing
End function
While Un-Installing:
Option Explicit
Dim wshShell, ngen, FSO, windir, assembly,PrgFiles
Set wshShell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
windir = wshShell.ExpandEnvironmentStrings("%Windir%")
PrgFiles = wshShell.ExpandEnvironmentStrings("%ProgramFiles%")
ngen = windir & "\Microsoft.NET\Framework\v2.0.50727\ngen.exe"
if FSO.FileExists(ngen) then
assembly=chr(34) & PrgFiles & "\DWG TrueView 2008\AcLayer.dll" & chr(34)
DeleteImages(assembly)
end if
Function DeleteImages (Byval file)
Dim strCmd,wshShell1
Set wshShell1 = CreateObject("WScript.Shell")
strCmd= chr(34) & ngen & chr(34) & " uninstall " & file
wshShell1.run strCmd,1
Set WshShell1 = nothing
End function
References
Few MSDN Blogs and Peer discussions

Native Images and Assemblies
In Wise Package Studio, When we double click an assembly in the files section, there is an option present for generating native assemblies. When tried this, the MSI was not able to obtain the dependent assemblies for this.
Hence this procedure of implementing a Custom Action was used.
Cheers'
Vijay
Microsoft MVP [Setup-Deploy]
Weblog: www.msigeek.com
Would you like to reply?
Login or Register to post your comment.