加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

我如何修补delphi类的私有方法?

发布时间:2020-12-15 06:09:50 所属栏目:大数据 来源:网络整理
导读:我已经阅读了这些问题和答案 How to change the implementation (detour) of an externally declared function Patch routine call in delphi 但我不能想像如何修补一个类的私人方法,位于不安的单位. 检查这个样本我想修补Bar程序. Unit ThidParty;Interface
我已经阅读了这些问题和答案

How to change the implementation (detour) of an externally declared function

Patch routine call in delphi

但我不能想像如何修补一个类的私人方法,位于不安的单位.

检查这个样本我想修补Bar程序.

Unit ThidParty;
Interface
   Type
      TFoo =Class
        private
           procedure Bar;
       end;

我认为哪个关键是找到一种方式来获取私有方法的地址.

那么,我如何修补delphi类的私有方法?

解决方法

下面列出的解决方案适用于Delphi Seattle的版本.
您可以使用 class helper来破解课程:

单元1

type
  TTest = class
  private
    procedure Foo;
  end;

单元2

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的返回值传递给您的修补功能之一,您是金色的.

不过,从Delphi 10.1柏林开始,这不再奏效了!班级助理员不能再访问严格的受保护,严格的私人或私人会员.这个“功能”实际上是Embarcadero现在在柏林修复的编译器错误.你没有运气.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读