Obtaining address locations of an overload method

余生颓废 提交于 2019-11-29 04:14:27
TLama

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;

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!