5,927
社区成员




我写了一个运行时BPL
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TCForm = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
var
CForm: TCForm;
implementation
{$R *.dfm}
initialization
RegisterClass(TCForm);
finalization
UnRegisterClass(TCForm);
end.
然后调用这个BPL
procedure TForm1.Button1Click(Sender: TObject);
var BPLHandle: HMODULE;
frm: TForm;
FormClass: TPersistentClass;
begin
// 加载包,并打开页面
BPLHandle := LoadPackage('cs.bpl');
showmessage(BPLHandle.ToString);
if BPLHandle > 0 then
begin
FormClass := GetClass('CForm');
FormClass := GetClass('TCForm');
FormClass := GetClass('UCFrom');
if FormClass<>nil then
begin
frm := TComponentClass(FormClass).Create(Application) as TForm;
frm.show;
end;
end;
end;
可是我试了几个名字,都无法获取到FormClass,他是nil值。LoadPackage是有值的,如果我把Link with runtime package勾选才可以,运行时BPL不是说可以不用勾选吗?
另外,你这种使用方法是动态加载包,也可以静态加载(在程序启动时自动加载),后者最简单也最常用,只需要uses包中的单元即可,静态链接还是用包在编译/链接时确定,源码不需要做任何修改。
当然要选中Link with runtime package,因为RTL基础包是必用的,只要你使用包模式,这个就必须用包,不能静态链接,同理,如果在包模式下使用了VCL或者FMX功能,也必须使用VCL、FMX包,其他非基础包,包括用户包,则可以自由选择使用静态链接或者运行时包。