Problems creating a form in a jvPlugin dll and displaying it as a child of a control on the mail form

StackOverflow https://stackoverflow.com/questions/7850723

سؤال

I'm trying to write a plugin system for my application based on jvPlugin. I create forms in the plugin dll, then reparent them into DevExpress docking controls. At first sight, it seems to work. The problem is that none of the controls on the dll forms ever receive focus. Also, when clicking on controls like TSplitter, a "Control 'xxx' has no parent window" exception is raised.

Here's how I'm doing it (condensed version).

The plugin host implements an IPluginHost interface

  IPluginHost = interface
  ['{C0416F76-6824-45E7-8819-414AB8F39E19}']
    function AddDockingForm(AForm: TForm): TObject;
    function GetParentApplicationHandle: THandle;
  end;

The plugin implments an IMyPlugin interface

  IMyPlugin = interface
  ['{E5574F27-3130-4EB8-A8F4-F709422BB549}']
    procedure AddUIComponents;
  end;

The following event is called when the plugin is initialised:

procedure TMyPlugin.JvPlugInInitialize(Sender: TObject; var AllowLoad: Boolean);
var
  RealApplicationHandle: THandle;
begin
  if Supports(HostApplication.MainForm, IPluginHost, FPluginHost) then
  begin
    RealApplicationHandle := Application.Handle;
    Application.Handle := FPluginHost.GetParentApplicationHandle; // Returns Application.Handle from the host application
    try
      FMyPluginForm:= TMyPluginForm.Create(Application); // Plugin host app owns the form
    finally
      Application.Handle := RealApplicationHandle;
    end;
  end;
end;

When the plugin host has loaded I call IMyPlugin.AddUIComponents in my plugin. It's implemented like this:

procedure TMyPlugin.AddUIComponents;
begin
  // Add the docking form
  FPluginHost.AddDockingForm(FMyPluginForm);
end;

AddDockingForm is implemented in the host like this:

function TfrmMyPluginHost.AddDockingForm(AForm: TForm): TObject;
var
  DockPanel: TdxDockPanel;
begin
  // Create a new dockpanel
  DockPanel := TdxDockPanel.Create(Self);
  DockPanel.Name := DPName;

  DockPanel.Height := AForm.Height;
  DockPanel.DockTo(dxDockSite1, dtBottom, 0);
  DockPanel.AutoHide := TRUE;

  // Rename the dock panel and parent the plugin
  DockPanel.Caption := AForm.Caption;
  DockPanel.Tag := Integer(AForm);

  AForm.Parent := DockPanel;
  AForm.BorderStyle := bsNone;
  AForm.Align := alClient;
  AForm.Show;

  FDockedPluginFormList.Add(AForm);

  Result := DockPanel;
end;

If I run the following function on any of the controls on the plugin form I see a list going all the way back to my host's main form. Yet TSplitters tell me they have no parent window. How can this be?

function TfrmMyPlugin.GetParents(WC: TWinControl): String;
begin
  if (WC <> nil) and (WC is TWinControl) then
    Result := WC.Name + ' [' + WC.ClassName + '] - ' + GetParents(WC.Parent);
end;

I must be missing something somewhere. Anybody got any good ideas?

هل كانت مفيدة؟

المحلول

Build both the plugin and the host application with runtime packages.

Without using runtime packages, the DLL uses a distinct copy of the RTL, VCL and any used units. For example, the DLL's TForm class is not the same as the host's TForm class (is operator fails across the host/DLL boundary and therefore a DLL control will not recognize host parent form as a valid instance of TForm), global variables like Application, Mouse, Screen are separate instances, you have two copies of RTTI, etc, etc.

The VCL was simply not designed to be used like this.

With runtime packages on, all the problems are solved and the plugin (either a runtime package itself or a DLL built with runtime packages) can integrate seamlessly with the host application using runtime packages.

نصائح أخرى

We made it work using "runtime packages" adding "vcl,rtl,ourownpckg" Where ourownpckg is a our-own-package created with all the DX dependencies and some other that we use across exe-plugins, including JVCL ones.

The three packages must be shiped along the exe

Hope it helps

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top