有没有办法在知道一个字符串的情况下,在同一程序里找到以这个字符串命名的函数地址?就像得到DLL里函数地址的WINAPI:GetProcAddress一样的?

解决方案 »

  1.   

    如果是导出函数, 那是可以的
    但如果是非导出函数, 编译后的结果并不包含函数名称信息, 则无法单纯通过exe或dll文件获得函数名称对应的地址信息
    但如果你编译时生成了map文件, 则使用map文件可以知道函数名称对应的地址信息注: Delphi中Project Options对话框的Linker页可以设定Map File选项
      

  2.   

    如果你的函数是tobject类中且为published,可用MethodAddress来得到它的地址
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
       published
        procedure test;
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    type
       padd=procedure;
    procedure TForm1.Test;
    begin
      ShowMessage('ok');
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
       p:padd;
       t:pointer;
    begin
      t:=MethodAddress('Test');
      if t<>nil then
         padd(t);
    end;end.如果是其它函数,一般情况下是不可能得到它的地址的,因为delph是编译程序,一般情况下,函数编译后,名字不再写入exe中,所以无法得到
    当然,如果函数是导出的,可通过GetProcAddress得到它的地址:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
       published
        procedure test;
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    type
       padd=procedure;
    procedure testt;
    begin
      ShowMessage('ok1');
    end;
    procedure TForm1.Test;
    begin
      ShowMessage('ok');
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var   t:pointer;
    begin
      t:=MethodAddress('Test');
      if t<>nil then
         padd(t);
      t:= GetProcAddress( MainInstance,'testt');
        if t<>nil then
         padd(t);
    end;
    exports testt;
    end.