我如何修补delphi类的私有方法? [英] How I can patch a private method of a delphi class?
问题描述
我已阅读这些问题和答案
但我可以不知道如何修改位于anoher单位的类的私有方法。
检查此示例我想要修补 Bar
程序。
单位ThidParty;
界面
键入
TFoo = Class
private
procedure Bar;
结束
我认为哪个密钥是找到一种方法来获取私有方法的地址。 >
那么,我如何修补delphi类的私有方法?
以下概述的解决方案适用于Delphi Seattle的版本。
您可以使用类助手破解课程:
Unit1
type
TTest = class
private
procedure Foo;
结束
Unit2
type
TMyTestHelper = TTest的类助手
函数GetFooAddress:指针;
结束
函数TMyTestHelper.GetFooAddress:指针;
var
MethodPtr:对象的过程;
begin
MethodPtr:= Self.Foo;
结果:= TMethod(MethodPtr).Code;
结束
函数FooAddress:指针;
begin
结果:= TTest(nil).GetFooAddress; //不需要实例化一个对象
end;
将返回值从 FooAddress
传递给一个的补丁功能,你是金色的。
但是,从Delphi 10.1柏林开始,这不再工作!班级助理员不能再访问严格的受保护,严格的私人或私人会员。这个功能实际上是Embarcadero现在在柏林修复的编译器错误。你没有运气。
I have read these questions and answers
How to change the implementation (detour) of an externally declared function
but i can't figere out how patch a private method of a class located in anoher unit.
Check this sample I want to patch the Bar
procedure.
Unit ThidParty;
Interface
Type
TFoo =Class
private
procedure Bar;
end;
I think which the key is find a way to obtain the address of the private method.
So, How I can patch a private method of a delphi class?
The solution outlined below works for versions up to and including Delphi Seattle. You can use a class helper to crack the class:
Unit1
type
TTest = class
private
procedure Foo;
end;
Unit2
type
TMyTestHelper = class helper for TTest
function GetFooAddress: Pointer;
end;
function TMyTestHelper.GetFooAddress: Pointer;
var
MethodPtr: procedure of object;
begin
MethodPtr := Self.Foo;
Result := TMethod(MethodPtr).Code;
end;
function FooAddress: Pointer;
begin
Result := TTest(nil).GetFooAddress;//don't need to instantiate an object
end;
Pass the return value from FooAddress
to one of your patching functions and you are golden.
However, starting with Delphi 10.1 Berlin, this no longer works! Class helpers can no longer access strict protected, strict private or private members. This "feature" was actually a compiler bug that Embarcadero has now fixed in Berlin. You are out of luck.
这篇关于我如何修补delphi类的私有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!