[fpc-devel] comparing methods

Desmond Coertzen patrolliekaptein at gmail.com
Thu Sep 10 17:27:33 CEST 2009


Thank you Peter.

TMethod would have been handy if i knew about this back then. For delphi
compatibility, I had to do this:

program methodpointer;

uses
  Classes, sysutils;

type
  TMyEvent = procedure of object;

  TMyClass = class(TObject)
    procedure MyMethod;
  end;

{ TMyClass }

procedure TMyClass.MyMethod;
begin
end;

var
  MyEvent : TMyEvent;
  Addr    : ^TMethod;
  MyClass: TMyClass = nil;

begin
  writeln(hexStr(TMethod(TMyEvent(MyClass.MyMethod)).Code));
  writeln(hexStr(TMethod(TMyEvent(MyClass.MyMethod)).Data));
  MyClass := TMyClass.Create;
  writeln(hexStr(TMethod(TMyEvent(MyClass.MyMethod)).Code));
  writeln(hexStr(TMethod(TMyEvent(MyClass.MyMethod)).Data));
  FreeAndNil(MyClass);
end.

On Thu, Sep 10, 2009 at 2:56 PM, Peter Vreman <peter at cooldown.xs4all.nl>wrote:

> On Thu, 10 Sep 2009 14:25:26 +0000, Desmond Coertzen
> <patrolliekaptein at gmail.com> wrote:
> > I have done some horrible code where I needed to know the following:
> >
> > If TSomeProc = procedure(....), then is it easy to determine or set
> entry
> > vector of ThatProc: TSomeProc by stating ThatProc :=
> > @ProcWhereTheCodeLives_InTheCodeSegment;
> >
> > It gets more difficult when you work with TSomeClassProc =
> procedure(....)
> > of object, because two parts are referenced here: 1) The entry vector
> where
> > the code lives, 2) The heap address where the object struct lives.
> (Known
> > as
> > the Self variable in a procedure of object)
> >
> > I have worked out which pointer of the two pointers belongs to the code
> and
> > which to the instance with a trial. With a move operation, I was able to
> > manipulate the two pointers, specifically to determine the address of
> the
> > instance.
> >
> > The @ operator only gives you access to the procedure part. Is there, or
> > will there be, another operator to give you access to the instance
> address?
> >
>
> You can use the TMethod record to access the fields:
>
> {$mode objfpc}
> type
>  tc=class
>    procedure p;
>  end;
> procedure tc.p;
> begin
> end;
> var
>  c : tc;
> begin
>  writeln(hexstr(tmethod(@c.p).data));
>  writeln(hexstr(tmethod(@c.p).code));
>
>  c:=tc.create;
>  writeln(hexstr(tmethod(@c.p).data));
>  writeln(hexstr(tmethod(@c.p).code));
> end.
>
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20090910/bba9df71/attachment.html>


More information about the fpc-devel mailing list