我使用MethodAddress方法获得了另一个窗体中的某一过程的指针,但该过程带有一个字符串型的参数,我发现调用该过程时,这个参数出错,请问大家有知道问题出在哪里吗?代码如下:var
  _Load: procedure(AText: string);
begin
  @_Load := _Form.MethodAddress('Sys_HotKey');         //_Form为某一窗体
      if @_Load <> nil then
        _Load('123');                                  //传入字符串‘123’,但事实上传不过去。
    end;
  end;

解决方案 »

  1.   

    参考 TMethod 的结构定义
      

  2.   


      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      published
         procedure test(str: string);
        { Public declarations }
      end;procedure TForm1.test(str: string);
    begin
      ShowMessage(str);
    end;procedure TForm1.Button1Click(Sender: TObject);
    type
      Test = procedure(AObj : TObject; str: string);
    var
      TAddress: Pointer;
      T1: Test;
    begin
      TAddress := Form1.MethodAddress('test');
      T1 := Test(TAddress);
      T1(nil, '123');
    end;
      

  3.   

    那个函数必须是published下边
    procedure test(str: pointer);
    其他参数定义成指针就可以了 
      

  4.   


    // unit1unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button3: TButton;
        procedure Button3Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
    implementation{$R *.dfm}uses unit2;type
      TExec = procedure(str: string) of object;procedure TForm1.Button3Click(Sender: TObject);
    var
       R: TMethod;
       Exec: TExec;
    begin
       R.Data := Pointer(Form2);
       R.Code := Form2.MethodAddress('ss') ;
       if NOT Assigned(R.Code) then
       begin
        showmessage('exit');
        exit;
       end;
       Exec := TExec(R);
       Exec('abc');
    end;end.// unit2unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm2 = class(TForm)
        Button1: TButton;
        procedure ss(str: string);    
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementation{$R *.dfm}procedure TForm2.ss(str: string);
    begin
      showmessage(str);
    end;end.
      

  5.   

    同理,如多个就定义为  TExec = procedure(str1: string; str2: string) of object;当然,也可以传其它结构,只要两个UNIT能识别就行(都定义或uses上定义单元)