I am trying to create a TButton object in DLL to show it up in EXE Main Form.Here is My Code:
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;
It creates button succesfuly with command comps[0](Form1.Handle)
but when i try to execute this (FindControl(comps[0](Form1.Handle)) as TButton).Caption:='A';
,it says INVALID TYPE CAST
.I checked the class name in main exe.ClassName was TButton.I am compiling both projects under Lazarus IDE for Windows x86_64.I also tried to use RTTI TPropertyEditor Grid.When i assign it like:TIPropertyEditor1.TIObject:=FindControl(comps[0](Form1.Handle))
TIPropertyEditor1 acts like it as TButton as normal.But I can't figure out why 'as TButton ' causes Invalid Type Cast.Is there any solution for my problem?