Clr basics (2): clr Basics

Source: Internet
Author: User
Tags mscorlib

Clr basics (2): clr Basics

If you have compiled a Program. cs code file, run the following command in the Visual Studio command prompt tool:
Csc.exe/out: Program.exe/t: exe/r: mscorlib. dll Program. cs
This command line indicates that the csung compiler generates an executable file (/out: Program.exe) named Program.exe ). The generated file belongs to the Win32 console application type (/t [target]: exe ).
A hosted PE file consists of four parts: The PE32 (+) header, CLR header, metadata, and IL. Use ildasm.exe (that is, IL Disassembler, IL anti-assembler) to check the metadata in a hosted PE file. The following describes how to use ildasm.exe.
Find "Visual Studio Tools" in the "Start" menu ",

Open the VS Command Prompt window. Enter the command as follows:
Ildasm D: \ My project \ program test \ ILDasm use test \ ConsoleApplication1 \ bin \ Debug \ ConsoleApplication1.exe
This will start ildasm.exe,

 

To use absolute volume in the script, if no value is set, ildasm.exe cannot find the exe or dll file you want to disassemble. You can also enter only ildasmand only open the ildasm.exe window for any other ildasm.exe:

 

You can also use the ildasm.exe tool more conveniently by using the Visual Studio external tool,

In the "command" column, select the directory where ildasm.exe is located, as shown in the following figure:
C: \ Program Files \ Microsoft SDKs \ Windows \ v8.1A \ bin \ NETFX 4.5.1 Tools \ ildasm.exe
Now we will use an example to explain how to use ildasm.exe. The code for creating a console application is simple:

1 class Program2 {3 static void Main (string [] args) 4 {5 Console. WriteLine ("ILDasm use test"); 6 Console. ReadLine (); 7} 8}

After saving the product, open ildasm.exe and select File to open the exe file generated by the console application,

 

A description of the icons in the ILDasm tree is provided with the following information:

Double-click the child node of the tree chart to see the relevant IL code:

 

A Brief Analysis of the IL Code and various commands:
MANIFEST list: MANIFEST is an additional information list, which mainly contains some attributes of the Assembly, such as the Assembly name, version number, and hash algorithm;
Leleapplication1.program class:

1 .class private auto ansi beforefieldinit ConsoleApplication1.Program2        extends [mscorlib]System.Object3 {4 } // end of class ConsoleApplication1.Program

 

 

1). class, which indicates that Program is a class. And it inherits from the System. Object Class of the Assembly-mscorlib;
2) private indicates the access permission;
3) auto indicates that all program memory loading is controlled by CLR;
4) ansi is designed to achieve seamless conversion between unmanaged code and managed code. C and C ++ code;
5) beforefieldinit indicates that the Runtime Library (CLR) can load the constructor (constructor) at any time after the static field method is generated );

. Otor method (a constructor or constructor ):

1. method public hidebysig specialname rtspecialname 2 instance void. ctor () cel managed3 {4 // code size 7 (0x7) 5. maxstack 86 IL_0000: ldarg.07 IL_0001: call instance void [mscorlib] System. object ::. ctor () 8 IL_0006: ret9} // end of method Program ::. ctor

 

1) Pencil managed: indicates the IL code, which indicates that the compiler compiles the managed code;
2). maxstack: Evaluation Stack during calling constructor );
3) IL_0000: mark the beginning of the code line;
4) ldarg.0: indicates that the first member parameter is reprinted. In the instance method, it indicates the reference of the current instance;
5) call: call is generally used to call static methods, because static methods are determined during compilation. Here, the constructor () is also developed during the compilation period. The other command callvirt indicates that the instance method is called, which is determined at runtime, because, as mentioned above, when the inheritance relationship of the called method is, it is necessary to compare the implementation methods (virtual and new) of functions with the same name of the base class and the derived class to determine the Method Table of the called function;
6) ret: indicates that the execution is completed and the result is returned;

Finally, the Main () method:

1. method private hidebysig static void Main (string [] args) cel managed 2 {3. entrypoint 4 // code size 19 (0x13) 5. maxstack 8 6 IL_0000: nop 7 IL_0001: ldstr bytearray (49 00 4C 00 44 00 61 00 73 00 6D 00 7F 4F 28 75 // I. l. d. a. s. m .. O (u 8 4B 6D D5 8B) // Km .. 9 IL_0006: call void [mscorlib] System. console: WriteLine (string) 10 IL_000b: nop11 IL_000c: call string [mscorlib] System. console: ReadLine () 12 IL_0011: pop13 IL_0012: ret14} // end of method Program: Main

 

1) The entrypoint command indicates that when the CLR loads a program, it starts from. entrypoint, that is, the Main method is used as the program's entry function;
2) ldstr: indicates that the string is pressed to the stack, where "Hello World." is pressed to the stack;
3) hidebysig: indicates that when this class is used as the base class and a derived class exists, this method is not inherited, as is the same as the constructor;
Below is a summary of commonly used IL commands:
1. newobj: used to create an object of the reference type;
2. ldstr: used to create a String object variable;
3. newarr: used to create an array object;
4. box: When the value type is converted to an object of the reference type, allocate memory to the value type Beibei paper storage stack;
Now let's get to know about ILDasm first. Let's continue our journey to CLR. The exe file generated by the console application is actually an assembly. CLR interprets an assembly as a collection of one or more types of definition files and resource files. Add resource files to an assembly. For example, if you commonly Add a Special icon to a Winform desktop program, you can select project "properties" in Visual Studio ", then, add the resource file in the Application tab.

After the embedded icon, the executable file of the application will display its own icon.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.