从类引用(Delphi)创建的表单中执行一个方法 [英] Execute a method from a form created by class reference (Delphi)

查看:184
本文介绍了从类引用(Delphi)创建的表单中执行一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单(form2),我实现了以下PUBLIC方法:

I have a form (form2) and I implemented the following PUBLIC method:

function ShowInterface(i:integer):boolean;

此表单在一个将被动态加载的包中。现在我要实例化这个表单(form2)并执行上面的方法。

This form is in a package that will be DYNAMIC LOADED. Now I want to instantiate this form (form2) and execute the method above.

重要提示:我不能在form1中引用form2的单位。

Important: I can't reference form2's unit in form1.

我尝试了这段代码,但是从来没有找到ShowInterface指针(返回nil)。

I tryed this code, but it never finds "ShowInterface" pointer (returns nil).

procedure TfrmForm1.Button1Click(Sender: TObject);
var
  PackageModule: HModule;
  AClass: TPersistentClass;
  ShowInterface: function (i:integer):boolean;
  frm: TCustomForm;
begin
  PackageModule := LoadPackage('form2.bpl');
  if PackageModule <> 0 then
  begin
    AClass := GetClass('TfrmForm2');
    if AClass <> nil then // <<-- FINE!! IT FINDS OUT 'TfrmForm2' in 'form2.bpl')
    begin
      frm := TComponentClass(AClass).Create(Self) as TCustomForm;
      ShowInterface := frm.MethodAddress('ShowInterface'); // <<-- HERE!! ALLWAYS RETURNS "NIL"
      if @ShowInterface <> nil then
        ShowInterface(1);
      // but if I call frm.Show, it works fine. frm is "loaded"!!!

      frm.Free;
    end;
    DoUnloadPackage(PackageModule);
  end;
end;

提前感谢

推荐答案

MethodAddress只适用于已发布的方法。

MethodAddress only works for published methods. Move it to the published section and it should work.

或者,如果您有Delphi 2010,扩展RTTI提供了一种方法来查找 public 方法。 (或其他可见性级别,如果将其从默认值更改。)

Or, if you have Delphi 2010, the extended RTTI offers a way to find public methods by name. (Or other visibility levels, if you change it from the default.)

这篇关于从类引用(Delphi)创建的表单中执行一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆