Obtaining address locations of an overload method
Asked Answered
A

2

8

How do I get all the address locations for functions/procedures/methods that is overloaded?

For example, Dialogs.MessageDlgPosHelp is overloaded having two different versions of it - one without a default button and one with. How would I obtain the address locations for the two functions?

Arizona answered 25/6, 2012 at 3:42 Comment(5)
Getting address of overloaded class method is discussed here. By the way, in my Delphi 7, MessageDlgPosHelp is not overloaded, and it is just a normal function, not method. The term "Method" refers to function or procedure of a class.Unprovided
The term 'method' for me means a function or procedure, possibly of a class. I have edited the question to reflect your meaning of the term 'method'.Arizona
I like TLama's reply (and have ticked accordingly). I would have preferred something like the RTTI way (as suggested by Hendra's link) which would work with the older version of Delphi (that do not have extended RTTI).Arizona
@Nicholas, with the newer version of Delphi, you mean?Unprovided
I have the Embarcadero special versions - Delphi 7, 2007, 2009, 2010, XE and XE2. Preferred Delphi 7, XE (as I use it at work) and XE2.Arizona
T
15

Based on this thread and what Thomas Mueller pointed there, you might define types with the same signatures as methods whose addresses you want to obtain (for each overload). If you then declare the variables of those types and assign method pointers to them you will make sure that compiler chooses the right overload to your known variable type and moreover that it won't ignore them if they wouldn't be used anywhere in the code (some overloads might not get linked in your binary).

So based on his idea it might looks for the MessageDlgPosHelp function overloads like this:

type
  TMessageDlgPosHelp1 = function(const Msg: string; DlgType: TMsgDlgType;
    Buttons: TMsgDlgButtons; HelpCtx: Longint; X, Y: Integer;
    const HelpFileName: string): Integer;
  TMessageDlgPosHelp2 = function(const Msg: string; DlgType: TMsgDlgType;
    Buttons: TMsgDlgButtons; HelpCtx: Longint; X, Y: Integer;
    const HelpFileName: string; DefaultButton: TMsgDlgBtn): Integer;

procedure TForm1.Button1Click(Sender: TObject);
var
  MessageDlgPosHelp1: TMessageDlgPosHelp1;
  MessageDlgPosHelp2: TMessageDlgPosHelp2;
begin
  MessageDlgPosHelp1 := MessageDlgPosHelp;
  MessageDlgPosHelp2 := MessageDlgPosHelp;
  ShowMessage(Format('%p; %p', [@MessageDlgPosHelp1, @MessageDlgPosHelp2]));
end;
Talaria answered 25/6, 2012 at 5:14 Comment(0)
S
0

Also You can create derived class that will expose this overload methods as simple methods with different names, cast any instance of that class to new class and easily use address of your wrapper methods.

Somatic answered 15/3, 2017 at 11:15 Comment(3)
In this case, the overloaded methods are 'global' methondsArizona
When You say "global method" did you mean a procedure or a function of a unit, and not method of any class or record? For this case You can just add own wrapper procedure or function with own name and just call that "global method" inside them. Without ever any casting tricks.Somatic
Yes - A "global method" is a function/procedure of a unit and not a class/record. Since this item is close to half a decade old, I think I was after a solution that would redirect all (in this case) MessageDlg methods to a custom method, without having to touch a lot of files in the code base. In the end, we have gone this way, changing the calls as we find them.Arizona

© 2022 - 2024 — McMap. All rights reserved.