【发布时间】:2018-07-10 12:08:31
【问题描述】:
我正在尝试在 DLL 中创建一个 TButton 对象以在 EXE 主窗体中显示它。这是我的代码:
DLL:
library dlltest1;
{$mode objfpc}{$H+}
uses
Classes,sysutils,Forms,Interfaces,StdCtrls,Windows,Dialogs
{ you can add units after this };
function test(hand:TForm):HWND;
var
a:TButton;
begin
a:=TButton.Create(hand);
a.Show;
a.Caption:='a';
result:=a.Handle;
end;
exports test;
begin
end.
EXE:
procedure test(hand:HWND); external 'dlltest1.dll';
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
TIPropertyGrid1: TTIPropertyGrid;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
type TComp=function(Hand:HWND):HWND;
var
comps:array of TComp;
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
procedure TForm1.Button1Click(Sender: TObject);
var
a:HWND;
begin
SetLength(comps,1);
a:=LoadLibrary('dlltest1.dll');
comps[0]:=TComp(GetProcAddress(a,'test'));
(FindControl(comps[0](Form1.Handle)) as TButton).Caption:='A';
end;
它使用命令comps[0](Form1.Handle) 成功创建按钮,但是当我尝试执行此(FindControl(comps[0](Form1.Handle)) as TButton).Caption:='A'; 时,它显示INVALID TYPE CAST。我检查了主exe 中的类名。ClassName 是TButton。我正在Lazarus IDE 下编译这两个项目对于 Windows x86_64。我也尝试使用 RTTI TPropertyEditor Grid。当我将其分配为:TIPropertyEditor1.TIObject:=FindControl(comps[0](Form1.Handle)) TIPropertyEditor1 就像正常的 TButton 一样。但我无法弄清楚为什么 'as TButton' 会导致 Invalid Type Cast.Is there我的问题有什么解决方案吗?
【问题讨论】:
-
类型标识在模块间无法正常工作。使用为此目的设计的软件包。假设 Lazarus 有包支持。
-
Lazarus 有包支持,如果你有什么建议可以告诉我,我会很高兴
-
肯定有文档
-
TIObject 可以感觉到它是 TButton,它可以毫无例外地处理它。所以这意味着类型标识可以跨模块工作。但我正在寻找关于为什么(作为 TButton)导致“无效类型转换”异常的答案以及如何解决它。
-
我也尝试在 Delphi 10.2 Tokyo 下运行此代码,它什么也没做。没有在表单上创建任何按钮。