Defining custom entry points

Explains how to manipulate the IL code to change the behavior of how a .NET program executes.

Please let other users know how useful this tip is by rating it below. Do you have a tip or code of your own you'd

like to share? Submit it here.


In this article, I am going to demonstrate how we can manipulate the IL code to change the behavior of how a .NET program executes. "Main" method is supposed to be the entry point of an assembly. With a bit of manipulation to the IL code, we can set some other method as the entry point. And no, I'm not trying to hack into the IL code; I'm just exploring a few things that we cannot do normally. I am going to use the utility ILDASM utility to output a .il file from the assembly.

A brief explanation about the Main method

When an assembly is compiled, the Main method is fired, as it is the entry point of the assembly. But, in reality, the entry point for an assembly is the "_Main" method generated automatically, which in turn calls the Main method defined by us. This means that if we open up the IL and change the "_Main" method to call some other method, then that method will act as a entry point for the assembly. In fact, we could literally delete the section of the Main method without affecting the assembly.

Let's do it step by step:

Step 1: Creating an assembly simple.vb

 ' Copy this line in a file simple.vb 
  ' Compile it as vbc simple.vb to produce simple.exe 

imports system 
class simple

 shared sub main 
    console.writeline("Hello From Main")
 end sub
 shared sub m2
    console.writeline("Hi from m2") 
 end sub

end class

Step 2 Generating IL code from the assembly

The output of the code will be simple.exe, which when executed would write "Hello From Main" on the console. Now let's open up the assembly and do some post mortem to change the entry point. But first, we will have to generate IL code from the assembly; it is done thru ILDASM utility.

> ILDASM simple.exe /out=simple.il
The result will be simple.il file that we will mainpulate.

Step 3 Modifying simple.il file

Open the simple.il file from any editor and refer to the last section, which looks something like this

.custom instance void 
[Microsoft.VisualBasic]Microsoft.VisualBasic.Globals/Globals$StandardMo
duleAttribute::.ctor() = ( 0100 00 00 )
.method public static void _main(class System.String[] _s) il managed
{
 .entrypoint
  // Code size 6 (0x6)
 .maxstack 8 
 IL_0000: call void t::main()
 IL_0005: ret
} // end of method _vbProject::_main
} // end of class _vbProject

Modify the line IL_0000: call void t::main() to IL_0000: call void t::m2().

.custom instance void 
[Microsoft.VisualBasic]Microsoft.VisualBasic.Globals/Globals$StandardMo
duleAttribute::.ctor() = ( 01 00 00 00 ) 
.method public static void _main(class System.String[] _s) il managed 
{
 .entrypoint
  // Code size 6 (0x6)
 .maxstack 8 
 IL_0000: call void t::m2()
 IL_0005: ret
} // end of method _vbProject::_main
} // end of class _vbProject

Save the file. The next step is to regenerate an executable assembly from modified simple.il.

Step 4: Generating assembly from simple.il

We will use the utility ILASM provided by the .NET to generate an assembly from an .IL file.

Type on command line:
ilasm simple.il

The result will be a simple.exe file, which, when fired, will display "Hi from m2" on the console.


Source: DotNetExtreme.com
This was first published in August 2003

Dig deeper on .NET Workflow

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close