Search code examples
c#.netclr

IL (MSIL) And Inside Of dll


I researched about MSIL (IL) and CLR and the other thing like processing path from source code through the OS like this

We can see Intermediate Language in the third step. I want to know are .dll files written exactly with Intermediate Language?

this is a .dll file insider code example, is this code called Intermediate Language?

I searched in the internet for MSIL code example and I saw this image below

enter image description here

these are different and because it i doubted.

thanks


Solution

  • You can actually test this yourself. Create a new Library project, and a single function:

    public class Class1 {
        public static int Add(int a, int b) {
            return a + b;
        }
    }
    

    Compile into a DLL, and open the DLL in ILSpy. Navigate to your Add() function, and change the dropdown from C# to IL. You'll see something like this:

        IL_0001: ldarg.0
        IL_0002: ldarg.1
        IL_0003: add
        IL_0004: stloc.0
    

    These are the human-readable representation of your IL. In ILSpy you can click on each of the instructions (e.g. ldarg.0) to see what they do, and their hexadecimal instruction code. Wikipedia has a full list of them.

    So our Add() function in CIL would be the following:

    opcode instruction description
    0x02 ldarg.0 Load argument 0 onto the stack.
    0x03 ldarg.1 Load argument 1 onto the stack.
    0x58 add Add two values, returning a new value.
    0x0A stloc.0 Pop a value from stack into local variable 0.

    Notice the hexadecimal opcodes there. Now open up the DLL in a hex editor. If you look here, you'll notice that the top parts are not actual code.

    If your hex editor has a search, go look for the hex sequence of our opcodes, and you'll see that in the middle of your file, you would find:

    02 03 58 0a
    

    Which is what your IL would look like inside a DLL!