Normally, the only way you will "write code" in the CIL computer language will be by using the "Build" function in Visual Studio or by executing the Visual Basic .NET compiler VBC directly. (VBC is the underlying compiler called by Visual Studio.) But understanding more about CIL is the key to really understanding .NET. Since CIL is a computer language just like any other, it's possible to write programs directly in CIL insteady of VB.NET or C# or some other .NET language. Not easy, but possible. Let's see what's involved.
Let's start out by "cheating" and creating most of our CIL program the easy way: using Visual Basic .NET.
Create a simple VB Console Program
Using Notepad, or any ASCII text editor, create a text file with the Visual Basic Source code:
~~~~~~~~~~~~~~~~~
Imports System
Module Module1
Sub Main()
Console.WriteLine("About Visual Basic!")
Console.ReadLine()
End Sub
End Module
~~~~~~~~~~~~~~~~~
Save and compile your program
Save your the file to a convenient location with a .vb file qualifier. I used the name ILASMEx.vb. Then compile it using VBC.exe in a Command Prompt window. As noted in previous articles, you'll probably have to run the .NET "sdkvars.bat" program. See the article about ILDASM for more details. The command is simplicity itself:
vbc ILASMEx.vb
This should write a file named ILASMEx.exe to the same location. The reason we're using the VBC compiler directly instead of "building" the file in Visual Studio is to avoid creating a very long and complex CIL file. At this point, we do have CIL code, but it's in the form of a "PE" exe file or "Portable Executable". We need the raw CIL code.
Create the CIL program file
Fortunately, the ILDASM Framework tool has an option just for this. It's called the Dump function. Open ILASMEx.exe with ILDASM in the Command Prompt window.
ildasm ILASMEx.exe
Under the File menu, select the Dump option. Use the default values in the dialog box that pops up and save to the file ILASMEx.il. This gives you the raw CIL code you need!
Check out the CIL code
Now you can open the ILASMEx.il file in Notepad. To refresh your memory, this program is about as simple as it can get since all it does is display one line of text in a Command Prompt window. The first thing to do is to scan the actual CIL code and see what it looks like!
Comments are marked with double slash marks:
// Microsoft (R) .NET Framework IL Disassembler. Version 1.1.4322.573
// Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.
After that, three external assemblies are referenced.
~~~~~~~~~~~~~~~~~
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 1:0:5000:0
}
.assembly extern Microsoft.VisualBasic
{
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )
.ver 7:0:5000:0
}
.assembly ILASMEx
{
.hash algorithm 0x00008004
.ver 0:0:0:0
}
~~~~~~~~~~~~~~~~~
By studying this example, you can start to pick up the syntax used in CIL. For example, notice that keywords are marked with a leading period. I don't have space here to pick through ALL of the code even in this simple example, but if you want to know more about CIL, I recommend the excellent book, CIL Programming: Under the Hood of .NET by Jason Bock (Apress, 2002).
Skipping down to the Main method, we see the code we actually wrote, now translated to CIL syntax:
~~~~~~~~~~~~~~~~~
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 17 (0x11)
.maxstack 8
IL_0000: ldstr "About Visual Basic!"
IL_0005: call void [mscorlib]System.Console::WriteLine(string)
IL_000a: call string [mscorlib]System.Console::ReadLine()
IL_000f: pop
IL_0010: ret
} // end of method Module1::Main
~~~~~~~~~~~~~~~~~
Let's change this code to pop up a message box with a different message instead. One of the cool things this will demonstrate how easy it is to mix the Command Prompt environment and any other windowed environment. Just use the right objects. We'll see what the right object is on the next page.

