How I can patch a private method of a delphi class?

我与影子孤独终老i 提交于 2019-12-17 18:37:25

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/10156430/how-i-can-patch-a-private-method-of-a-delphi-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!