Solved

Pointers to member functions.

Posted on 2001-09-01
3
201 Views
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?
0
Comment
Question by:fulg0re
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
DragonSlayer earned 300 total points
ID: 6449518
type
  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
    { Private declarations }
    procedure ExecuteRoutine(Instance: TObject; Name: string);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

type
   TExecute = procedure (Sender: TObject) of object;

// The most important routine
procedure TForm1.ExecuteRoutine(Instance: TObject; Name: string; Param: TObject);
var
  Routine: TMethod;
  Execute: TExecute;
begin
  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);
  Execute(Param);
end;

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

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

Expert Comment

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

Expert Comment

by:DragonSlayer
ID: 6450031
oh dear...

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now