Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Pointers to member functions.

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
fulg0re
Asked:
fulg0re
  • 2
1 Solution
 
DragonSlayerCommented:
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
 
MadshiCommented:
Beware of fulg0re, he bought my components with a fraud credit card. I'm serious.
0
 
DragonSlayerCommented:
oh dear...

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now