1. Home
  2. Computing & Technology
  3. Visual Basic

The New CIL Code and Running the Program

By Dan Mabbutt, About.com

Write the code for new functionality in the CIL program

The first thing to do is to add a reference for the .NET Framework Forms assembly, System.Windows.Forms.dll. This can be added directly below the Microsoft assembly references:

~~~~~~~~~~~~~~~~~
.assembly extern System.Windows.Forms
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89)
  .ver 1:0:5000:0
}
~~~~~~~~~~~~~~~~~

You might ask, "Where do I get the magic numbers?" Here's where the Global Assembly Cache, the GAC, comes in. Open the GAC (it's in the C:\WINDOWS\assembly folder) you can copy them directly out of the properties dialog for System.Windows.Forms.dll. In fact, if you have version 2.0 of the .NET Framework, you'll see both versions in the GAC.

Now, the only coding left to do is to replace the existing code with new statements to pop up a message box. Since this is just a "proof of concept" introduction, I'll just give the necessary code to you. It's all in the Main method definition and many of those statements don't actually change. Notice that we also left out the line numbers. These were added by the ILDASM "Dump" function and aren't required.

~~~~~~~~~~~~~~~~~
.method private static void Main(string[] args) cil managed
{
   .entrypoint
   .maxstack 8
    ldstr "About Visual Basic Rocks!"
    call valuetype [System.Windows.Forms]
        System.Windows.Forms.DialogResult
        [System.Windows.Forms]
        System.Windows.Forms.MessageBox::Show(string)
    pop
    ret
} // end of method Module1::Main
~~~~~~~~~~~~~~~~~

Create the new CIL executable

Save the file once the changes have been made and compile it with the .NET Framework tool ILASM.exe in the Command Prompt window. (At last, we have arrived at the finish line promised at the beginning of the article.) You should see messages similar to these:

Microsoft (R) .NET Framework IL Assembler. Version 1.1.4322.2032
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.
Assembling 'ilasmex.il' , no listing file, to EXE --> 'ilasmex.EXE'
Source file is UTF-8

Assembled method Module1::Main
Creating PE file

Emitting members:
Global
Class 1 Methods: 1;
Writing PE file
Operation completed successfully

And when you execute the new program, you get a messagebox that displays the proud banner:

About Visual Basic Rocks!

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. ILASM - The compiler for the language of .NET: CIL

©2009 About.com, a part of The New York Times Company.

All rights reserved.