Microsoft Intermediate Language [MSIL] Tools

I have been a .Net developer since long time and I have developed fairly complex and large scale applications using different .Net framework technologies like C#, ASP.Net, Windows Forms, WCF etc. I have conducted numerous training sessions on these technologies for my team members and colleagues as well. Most of the time my focus was on to explain the language features like ‘Abstract classes’, ‘Interfaces’, ‘Virtual methods’, ‘LINQ’ etc. However I did not pay much attention on the compiler side. That is, how C# compiler compiles source code, how Intermediate Language [IL] looks like, how can I disassemble the code of a third party library; make few modifications to it and assemble it again? Are there any tools available to do such things?

So I thought to take some time out and dissect first program that almost all the developers write when they try to adopt new language, which is Hello World.

Pre-requisite

If you want to perform the steps mentioned in this article, you would need .Net framework [any version, I have 4.5] installed on your machine. Optionally you can install Visual Studio to build the program. Since we are building simple Hello World application, I assume that you have basic understanding of .Net framework and C# language.

Writing Hello World

Okay, so easy part first. Assuming you have .Net framework and Visual Studio [optionally] installed on your machine, create a C# Console Application which displays Hello World output on the desktop. Again, this is fairly basic requirement, so I am not going into any details of it.

Now you might have heard it many times, .Net is a language neutral framework. That means you can develop an application using different .Net framework compatible languages like C#, VB.Net, F#, Iron Python and many more. As a part of program execution, Common Language Runtime [CLR] first converts C# program into Microsoft Intermediate Language [MSIL] and then it converts it into machine instruction set using Just-In-Time [JIT] compiler.

Below diagram shows how language compilers, CLR and Just-In-Time compiler participates during .Net program compilation and execution.

.Net program compilation

There are useful tools available to view MSIL code like ILSpy, .Net Reflector, ILDASM, ILASM etc. While ILSpy, ILDASM and ILASM are free tools, .Net Reflector is a commercial product from Red-gate.  In this article, I will explain the IPSpy, ILDASM and ILASM tools.

ILSpy

ILSpy is open source .Net assembly browser and de-compiler.  Assuming you have Hello World program assembly built now, open it using ILSpy editor. Expand the tree structure on the left hand side and double click on ‘Main’ method; it should display the method definition on the right side panel.

IL View

Note the language dropdown in the toolbar; you can select VB or C# to display the ‘Main’ method source code in VB or IL.

ILSpy C# View

ILSpy VB View

Please note that we have implemented ‘Hello World’ program using C#, however we can disassemble it to VB or IL with the help of ILSpy.

ILDASM

Intermediate Language Disassembler [ILDASM] is a tool which gets installed along with Visual Studio and Windows SDK. As the name suggests, it is used to disassemble a portable executable file [.exe / .dll] which contains MSIL. To view the intermediate language of our Hello World program, open Visual Studio Command prompt and navigate to assembly [HelloWorld.exe] path and type below mentioned command.

ILDASM HelloWorld.exe

This should open ILDASM tool as shown in below screenshot.

ILDASM View

To view the MSIL for the Main method, simply double click on the Main function icon and it will display the metadata of Main function as shown below

ILDASM View Main Method

Note, ILDASM tool does not contain all the features of ILSpy, however it allows you to export MSIL code into a text file. Simply click on File -> Dump menu option, select ‘Dump IL Code’ option and click Ok. Open the file in your favorite text editor and it should contain assembly information, .Net framework version information and the class members declaration.

ILASM

Intermediate Language Assembler [ILASM] is used to generate portable executable from Microsoft Intermediate Language [MSIL]. Similar to ILDASM, this tool gets installed along with Visual Studio and Windows SDK.

Assuming that you have created an intermediate language file using ILDASM tool earlier, now we will make changes to IL file and generates portable executable using ILASM tool.

Open IL file using your favorite editor and change ‘Hello World’ input string to ‘Hello MSIL World’ and save it. Open Visual Studio Command Prompt and navigate to HelloWorld.IL file. Type ILASM command as shown below

Ilasm HelloWorld.il /output:HelloMSILWorld.exe

This should create a HelloMSILWorld.exe file at the same location where HelloWorld.IL file exists. Double click on HelloMSILWorld.exe and you should get ‘Hello MSIL World’ output on command prompt.

Summary

In this article, we have covered different tools available to view MSIL code and how you can assemble / disassemble portable executable using these tools.

I hope it was useful to you.

Thanks.

7 Comments

  1. sudip · February 20, 2015 Reply

    can we convert vb.net code to c# code using msil??

    • Prasad Honrao · February 24, 2015 Reply

      Why do you want to get into MSIL to convert VB.Net code into C#. MSIL, as the name suggests is an intermediarly laguage, so one program writtern in both C# and VB language gets converted into same MSIL, however I am not sure how you can use it to convert code from one laguage into another. Depending on complexity of your application, you may try online tools like Code Converter from Telerik.

  2. sudip · March 7, 2015 Reply

    i dnt want to use any tool. i want to make my own. is there any algorithm to convert code from one language to other.

    • Prasad Honrao · March 9, 2015 Reply

      I suggest you to look into the Roslyn project, which is open sourced as well. All the language conversion tools are based on it. Hope that helps :)

  3. sudip · March 7, 2015 Reply

    or any open source code??

  4. Ranjan Sharma · August 5, 2016 Reply

    sir could you tell me,
    how can add dot net compiler (Like as Visual Studio )in my website.Anyone can run programs online through my website.

  5. Marat Braliyev · November 17, 2016 Reply

    ILSPY is great utility, thanks!

Leave a Reply