This article introduces some techniques for Delphi to penetrate OOP constraints.
Access protected
(Protected)
Variable
If it is a protection variable of a class, you can access it anywhere by using the following methods:
Type
Tsomeclassaccess =
Class(Tsomeclass );
Begin
Tsomeclassaccess (object1). protected_bool: = false;
Tsomeclassaccess (object1). protected_int: = 0;
...
End;
Access private(Private)VariableIf it is a private variable of a class, we need to calculate the offset of the variable in VMT. At the same time, because the class declaration may be different in different Delphi versions, it is best to first check the source code. Example: access the second internal member variable fhandle of tmenuitemType
Thackmenuitem =Class(Tcomponent)
Protected// <-- Change to protected
Fxxxxcaption: ansistring;
Fhandle: hmenu; // <-- the property you want to access
Begin
Thackmenuitem (amenuitem). fhandle: = 0;
...
End;
Access some private(Private)FunctionIt is much more difficult to access private functions. As far as I know, only private functions that are defined as virtual, override, dynamic, and message can be accessed or replaced. The implementation principle is similar to accessing private variables: Calculate the offset of the function in VMT/DMT, and then replace the memory address with the memory address of the new function. For more information, see tntsystem. PAS in tntcontrols to install system patches or fastcode control packages.
Friendly reminder: (1) Generally, private functions involve accessing other private functions/variables. To access a private function, you need to modify more private functions/variables. Relatively complex and unreliable. (2) Improper modification of the memory address may cause software conflicts, such as aqtime. NOTE: If no keyword strict is set during variable definition, internal variables/functions of all classes in the same unit can directly access each other. This is a small backdoor opened by Delphi.Add private(Private)Function()Do we really need it? Sometimes! For example, when I write a dunit unit test, I need to accept the wm_copydata message. However, this message is sent to the guitestrunner. I must enter the guitestrunner to get the corresponding message. The implementation method is similar to accessing private functions. We calculate the length of VMT/DMT and add a new function pointer at the end. I found an article on the Internet for more details. Click here to view details. This method has a limit: this modification is not completed during the compilation period. You must modify it only after the class is instantiated. This is obviously troublesome if this class is frequently used. If you can put the modified Code in the class destructor, the problem will be solved. I am imagining: Can we use the method introduced in section 3 to implement this ......
Add private(Private)Function(B)There is also a more clean modification method, we define a class with the same nameSpoofingCompiler. For example, create a new unit named guitestrunnerpatch. Pas.Type
Thackguitestrunner =Class(Guitestrunner. tguitestrunner)
Private
Procedure wmcopydata (var msg: twmcopydata); message wm_copydata; // <-- the method you want to append
End;
Add the new guitestrunnerpatch where you need to use guitestrunner. Note: It must be later. Otherwise, the compiler will not call the class you modified, but call the original one. Summary:In the internal world of Delphi, we should consider code portability and versatility as much as possible, and replace the best effect with a few changes. If the repair and makeup mentioned above cannot solve your problem, you can also directly modify the control source code of Delphi. After the modification, check "use DEBUG dcus" in the compilation option, compile the program, and save the compiled DCU file to the compilation directory. I usually create two directories: patchedvcls and precompiled to put the modified source code and the compiled version. Then define the two directories into the environment variables so that the changed code can be used as long as these variables are added to the path settings of each project. If this article is incorrect, click it.