Solved

Pointers to member functions.

Posted on 2001-09-01
3
212 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

734 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