Using ILASM and ILDASM


I wanted to write another small sample to demonstrate how you can use ilasm to modify a compiled assembly. The sample is very simple, but demonstrates the concept well. Let us take App.cs which contains the following code:


 


using System;


public class sample


{


    static void Main(string[] args)


    {


        Console.WriteLine(“Inside main …”);


    }


}


 


Let us compile App.cs to App.exe. This executable now prints Inside main … when it is run.


 


Now let us attempt to change the output of this executable without touching its source files.


 


You run ildasm App.exe /out:App.il to generate the IL code corresponding to the executable. You can now edit this il code in notepad and change the ldstr from “Inside main …” to “Inside main changed …” as shown below


 


IL_0000:  nop


IL_0001:  ldstr      “Inside main …”


IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)


IL_000b:  nop


IL_000c:  ret


 


To


 


IL_0000:  nop


IL_0001:  ldstr      “Inside main changed …”


IL_0006:  call       void mscorlib]System.Console::WriteLine(string)


IL_000b:  nop


IL_000c:  ret


 


Now let us rebuild the executable from this il file by running ilasm App.il. This generates a new App.exe. If you run App.exe you will get the following output “Inside main changed …”


 


 


 


Comments (5)

  1. Alois Kraus says:

    Hi Thottam,

    I have made a simple strong naming took with this technique which works quite well:

    http://geekswithblogs.net/akraus1/archive/2007/01/23/104288.aspx

    One problem that remains is debuggability. If you do such a round trip the VS 2005 debugger is no longer able to set any breakpoints. The Ilasm /Debug option does not help much since I end up either in IL code or if I copy the original pdb at the recompiled exe location it is not loaded for some reason.

    Yours,

      Alois Kraus

  2. thottams@microsoft.com says:

    Thanks for the feedback. I agree. The down side to this is debuggability. I have seen lots of people use this approach to achieve minor tweaks to their assemblies, if their assembly was generated by a tool.

  3. Jack Jones says:

    Hello Microsoft member, I was wondering how I can buy (Don't know if it costs money) Or download ILdasm?

    Can anyone please link me to a download? :}

    Greetz,

    Jack

  4. M.T. says:

    Greetings Jack,

    ILasm and ILdasm come with the .NET Framework free of charge.

    In .NET 4 they are installed in:

    ILAsm

    C:WindowsMicrosoft.NETFrameworkv4.0.21006

    ILdasm

    C:Program FilesMicrosoft SDKsWindowsv7.0Abin

    Best

    M.T.

    MSWindowsSG@aol.com

    ————————————————

    Jack Jones 30 Jun 2010 4:03 AM

    Hello Microsoft member, I was wondering how I can buy (Don't know if it costs money) Or download ILdasm?

    Can anyone please link me to a download? :}

    Greetz,

    Jack

  5. M.T. says:

    Greetings Jack,

    ILasm and ILdasm come with the .NET Framework free of charge.

    In .NET 4 they are installed in:

    ILAsm

    C:WindowsMicrosoft.NETFrameworkv4.0.21006

    ILdasm

    C:Program FilesMicrosoft SDKsWindowsv7.0Abin

    Best

    M.T.

    MSWindowsPS@aol.com

    ————————————————

    Jack Jones 30 Jun 2010 4:03 AM

    Hello Microsoft member, I was wondering how I can buy (Don't know if it costs money) Or download ILdasm?

    Can anyone please link me to a download? :}

    Greetz,

    Jack