Pointers to member functions.

Posted on 2001-09-01
Last Modified: 2011-09-20
Well, here goes:
I have a certain component with member functions that should be used as callbacks for a DLL I am wrapping. Now the problem is: when the functions are global, and not members of a class, I can get a pointer to them just by writing @Func.

But how do I get a pointer to a member function?
Question by:fulg0re
  • 2
LVL 14

Accepted Solution

DragonSlayer earned 300 total points
ID: 6449518
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);

    procedure Hello_World(Sender: TObject); // Your routines (that you'll run by name) must be here
    { Private declarations }
    procedure ExecuteRoutine(Instance: TObject; Name: string);
    { Public declarations }

  Form1: TForm1;

   TExecute = procedure (Sender: TObject) of object;

// The most important routine
procedure TForm1.ExecuteRoutine(Instance: TObject; Name: string; Param: TObject);
  Routine: TMethod;
  Execute: TExecute;
  Routine.Data := Pointer(Instance);
  Routine.Code := Instance.MethodAddress(Name); // Returns the address of a published method.
  MethodAddress is used internally by the streaming system.
  When an event property is read from a stream, MethodAddress converts a method name,
  specified by Name, to a pointer containing the method address.
  There should be no need to call MethodAddress directly.

  If Name does not specify a published method for the object, MethodAddress returns nil.
  if Routine.Code = nil then Exit;
  Execute := TExecute(Routine);

procedure TForm1.Button1Click(Sender: TObject);
  ExecuteRoutine(Form1, 'Hello_World', nil);

procedure TForm1.Hello_World(Sender: TObject);
  ShowMessage('This is a test');
LVL 20

Expert Comment

ID: 6450010
Beware of fulg0re, he bought my components with a fraud credit card. I'm serious.
LVL 14

Expert Comment

ID: 6450031
oh dear...

if it's true, fulg0re, then you are being a BAD boy...

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
creating manifest for my dll that called from activex 6 113
Show Listview image from database (String field) 5 123
creating threads in delphi 1 101
Delphi Yen format 3 33
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question