将GetClass与Delphi运行时包一起使用

时间:2018-02-07 12:21:44

标签: delphi package delphi-10.2-tokyo

我使用Delphi 10.2.2 Toyko开发运行时包,但在我的应用程序中调用GetClass()在我的包中找不到我的Form类。

在包中,我有以下代码(只是一个表单):

procedure CreateForm;
begin
  // Create instance of class TfmFormInPKG in Application
  fmFormInPKG := TfmFormInPKG.Create(Application);
  fmFormInPKG.Show;
end;

procedure DestroyForm;
begin
  // Validate the instance
  if Assigned(fmFormInPKG) then
  begin
    // Destroy the instance if assigned
    fmFormInPKG.Destroy;
  end;
end;

initialization
  RegisterClass(TfmFormInPKG);
  // Call procedure CreateForm when the package is loaded
  CreateForm;

finalization
  // Call procedure DestroyForm when the package is unloaded
  DestroyForm;

在通话应用中,我有以下代码:

procedure TfmTestVCLDPK.btnCreatePKGClick(Sender: TObject);
var
  FormInPKG: TForm;
  FormClass: TPersistentClass;
  HandlePack: HModule;
  theCaption: Variant;
begin
  // try to load the package
  HandlePack := LoadPackage ('pkgTheVCLPKGXE10.bpl');
  if HandlePack > 0 then
  begin
    FormClass := TPersistentClass(GetClass('TfmFormInPKG'));
    if Assigned (FormClass) then
    begin
      with TComponentClass(FormClass).Create (Application) as TCustomForm do
      begin
        ShowModal;
        Free;
      end;
    end
    else
      ShowMessage ('Form class not found');
    UnloadPackage (HandlePack);
  end
  else
    ShowMessage ('Package not found');
end;

虽然看起来我确实得到了包的句柄,但GetClass()总是返回nil。我错过了什么吗?

0 个答案:

没有答案