如何修补 delphi 类的私有方法? [英] How I can patch a private method of a delphi class?
问题描述
我已阅读这些问题和答案
I have read these questions and answers
但我不知道如何修补位于另一个单元中的类的私有方法.
but i can't figere out how patch a private method of a class located in anoher unit.
检查此示例我想修补 Bar
过程.
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.
那么,如何修补 delphi 类的私有方法?
So, How I can patch a private method of a delphi class?
推荐答案
下面概述的解决方案适用于包括 Delphi Seattle 在内的版本.您可以使用类助手来破解类:
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;
将 FooAddress
的返回值传递给您的修补函数之一,您就成功了.
Pass the return value from FooAddress
to one of your patching functions and you are golden.
但是,从 Delphi 10.1 Berlin 开始,这不再有效!类助手不能再访问严格保护、严格私有或私有成员.这个功能"实际上是 Embarcadero 现在在柏林修复的一个编译器错误.你运气不好.
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屋!