Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pointers to member functions.

Posted on 2001-09-01
3
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
DragonSlayer earned 900 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

661 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