Monday, July 09, 2007

The Common Language Runtime (CLR) is the virtual machine component of Microsoft's .NET initiative. It is Microsoft's implementation of the Common Language Infrastructure (CLI) standard, which defines an execution environment for program code. The CLR runs a form of bytecode called the Microsoft Intermediate Language (MSIL), Microsoft's implementation of the Common Intermediate Language.

Developers using the CLR write code in a high level language such as C# or VB.Net. At compile-time, a .NET compiler converts such code into MSIL (Microsoft Intermediate Language) code. At runtime, the CLR's just-in-time compiler (JIT compiler) converts the MSIL code into code native to the operating system. Alternatively, the MSIL code can be compiled to native code in a separate step prior to runtime. This speeds up all later runs of the software as the MSIL-to-native compilation is no longer necessary

Some important services provided by CLR:
Memory management
Thread management
Exception handling
Garbage collection
Security
Prior to runtime Is it possible to convert IL code to the native machine code ? yes... .NET has the options for it.
How is it possible ?
you have to do following:

first disassemble your code using ILDASM.exe

DLL : ildasm YourCode.dll /output:YourCode.il
EXE: ildasm YourCode.exe /output:YourCode.il

this creates an il file and a res file

then you have to assemble this il and res file with ILASM.exe

DLL: ilasm YourCode.il /dll /resource:YourCode.res /output:YourCode.dll
EXE :ilasm YourCode.il /exe /resource:YourCode.res /output:YourCode.exe

After those two steps you have an PE in native code
You find a more detail instruction on those two tools in msdn - "ILDASM.exe" and "ILASM.exe"

or do the following :
It is possible to compile the IL code in a .NET assembly into Native Code using the command line tool ngen.exe. (see the Framework documentation
for more info on it). The tool also installs the compiled code into the native image cache.

what is ngen.exe ?
Native Image Generator (Ngen.exe)

The Native Image Generator creates a native image from a managed assembly and installs it into the native image cache on the local computer.
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.(This implies the IL code is not converted to machine code everytime)
ngen [options] [assemblyName |assemblyPath ]
A native image is a file containing compiled processor-specific machine code.

No comments: