Tip

Defining custom entry points

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.