How to modify. NET Framework

Source: Internet
Author: User
Tags mscorlib reflector ultraedit

During this time, the bug was tracked and finally found to be a framework issue, which was somewhat desperate. So I posted a post on the Microsoft Forum, hoping to get some help. Although the Forum intelligently determines whether the poster is an msdn subscriber user, it can solve the problem as soon as possible (it is said that the question of the msdn subscriber user can be replied within two days, at that time, I was very proud of the msdn subscription account that the company purchased for us. But the reply was "could you file a bug report for this issue through connect? ", Again after despair.

After seeing the payeasy advertisement, you will have the following content and how to modify the. NET Framework:

Here we take modifying the guid class as an example. We will modify the internal structure of this class so that it can always construct an object with a value of 0 (00000000-0000-0000-000000000000 ).

1. Know who you want to modify(DLL name)
This is very simple. By turning over msdn, you should be able to find out which class library you are calling, or use reflector to get more detailed information. According to msdn, The GUID class is in mscorlib. dll.

2,Find the DLL you want to modify, And copy it for modification.
Mscorlib. dll is located in GAC. Unfortunately, Windows only allows you to enjoy it in a quiet way (although anxious)

Now we need to find the actual path of the DLL in the operating system.
2.1 Download Filemon, which is used to monitor file access. We can find the file path through it.
2.2 write a smallProgramTo allow the program to access (reference) the file you need to find the path, which is our mscorlib

Static Void Main ( String [] ARGs)
{
Guid = guid. New GUID ();

Console. writeline (guid );

Console. Read ();
}

CodeBoth the guid and console in will access mscorlib. dll.
2.3 run Filemon and let him monitor mscorlib access. Because we don't know the specific path of mscorlib (nonsense), we should at least know that it is in C: \ windows \ assembly (at least on the system disk), we can monitor this folder and all its files:

2.4. Run our applet. Filemon will monitor all accesses to c: \ windows \ assembly, highlight mscorlib, and double-click the entries in the list, it will automatically open the folder where the file is located:

OK. Now we have found the DLL file and copied it for modification, and recorded its path for future use (C: \ windows \ Assembly \ gac_32 \ mscorlib \ 2.0.0.0 _ b77a5c561934e089 ). In addition, we recommend that you back up the copied DLL.

3. decompile the DLL using ildasm to generate an intermediate language (IL) file., We will modify the Il File
Ildasm is a decompilation tool that comes with. net. It can be found in the SDK, but can be used through the vs Console (For details, refer to msdn)

Then you can obtain the Il file of the Assembly.

It is actually a text file, so it can be directly opened for editing, but we recommend using notepad ++ or ultraedit text editor, but it seems a bit messy after opening, how can we find the location of the Code to be modified?

4. Use reflector to view the definition of the class or method to be searched
Open reflector, find the class or method to be searched, and view its definition. Assume that we need the guid. newguid () method:

Switch to the Il View:

OK, with this il fragment, it's easy to find this method in the ocean of mscorlib. dll. Il (CTRL-f)

5. Modify the Il code.
Find the corresponding method in Notepad ++ or ultraedit. We found that newguid () actually calls its GUID (bool) method. We can replace it with the default constructor (The GUID constructed by the default constructor is 00000000-0000-0000-000000000000) in this way, the value of 0 is always returned when the newguid () method is called. You can also input true when the guid (bool) method is called. The latter is used:

. Method Public Hidebysig Static valuetype system. guid
Newguid () Cel Managed
{
// Code size 7 (0x7)
. Maxstack 8
Il_0000: LDC. i4.0
Il_0001: Newobj Instance Void System. guid:. ctor ( Bool )
Il_0006: RET
} // End of method guid: newguid

In the above CodeIl_0000:LDC. i4.00 (false) is used as a 4-byte integer into the stack. We change 0 to 1:

. Method Public Hidebysig Static valuetype system. guid
Newguid () Cel Managed
{
// Code size 7 (0x7)
. Maxstack 8
Il_0000: LDC. i4.1
Il_0001: Newobj Instance Void System. guid:. ctor ( Bool )
Il_0006: RET
} // End of method guid: newguid

(Note: The operation here is very simple, so it is very simple to modify. If you are familiar with complicated operations, learn the relevant knowledge of il first)
Save your changes.

6. Compile the Il code to generate a new DLL.
Using ilasm provided by MS, You can compile the Il file into a DLL:

(Do not forget to close the text editor before compilation. For example, ultraedit will exclusively occupy the file and thus cannot be accessed)

7. Put the modified DLL back to GAC
You may think of the method mentioned in msdn to install it to GAC just like installing your own Common Assembly. It is probably possible to guess that this is not feasible, otherwise it is "insecure". Or did we record the path of mscorlib just now? simply copy and paste the file and overwrite it. Maybe you can, maybe not, so you can say no, there may be two reasons: one is that access pages are not allowed to be overwritten, and the other is programs. net will check the assembly version when running the program. Let's try:
Direct access to the specified path does not work, as shown in:

But it doesn't matter. We can access this directory through a third-party tool. You can double-click the Filemon entry to open the corresponding file directory, another more common and convenient method is totalcommander, which can easily access various hidden paths in windows.

Drag and Drop the file to the corresponding directory to overwrite it.

8. Delete the local image of the Assembly.(Native image)
Back to monitoring mscorlib access with Filemon, you may find that our applet is not directly accessing mscorlib. DLL, but a file named mscorlib. ni. DLL file (in the path c: \ windows \ Assembly \ nativeimages_v2.0.50727_32 \ mscorlib \ 9adb89fa22fd5b4ce433b5aca7fb1b07 \), this is mscorlib. native image of DLL. net optimized friends should know that we can use ngen to generate a local image of the Assembly to improve the running speed. the program will directly access the image instead of the modified mscorlib. DLL, which will make our modifications ineffective. Therefore, we need to delete the image. Why is it deleted, instead of using ngen to generate a new image and overwrite the modified mscorlib. dll? The reason is simple. Let's think about it.
First, use the ngen uninstall command to detach the local image from the local image cache, and then use totalcommander to delete the local image. (Note: First, close the application that can be referenced in the overwritten assembly, it is best to restart your computer, so you don't have to try to delete it in safe mode)

9. Verify the result:

Class Program
{
Static Void Main ( String [] ARGs)
{
Guid guid1 = guid. newguid ();
Guid guid2 = guid. newguid ();

Console. writeline ("The first guid: {0}", Guid1 );
Console. writeline ("The second guid: {0}", Guid2 );

Console. Read ();
}

}
If Ms's original mscorlib. dll is used, the output is similar to the following:

Use the modified mscorlib. dll:

(In addition, it is worth noting that Visual Studio references some assemblies from c: \ Program Files \ reference assemblies. Therefore, if an assembly has an impact on vs reference, it should overwrite the corresponding files in c: \ Program Files \ reference assemblies. Mscorlib is not required here)

 

Address: http://www.cnblogs.com/zhouyinhui/archive/2009/06/04/1496485.html

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.