我如何修补delphi类的私有方法? [英] How I can patch a private method of a delphi class?

查看:125
本文介绍了我如何修补delphi类的私有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读这些问题和答案



如何更改外部声明的函数的实现(绕行)



在delphi中调用例行程序



但我可以不知道如何修改位于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

Patch routine call in delphi

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆