Mixed-Mode Sample

The Mixed Mode sample is a set of 13 projects that shows all available host applications (WinForms, VCL, and FireMonkey) that are able to load all existing visual plugins (VCL, WinForms, WPF, Silverlight, and FireMonkey).

Please note that you will need Hydra for both Delphi and .NET installed in order to build and run these projects.

In this topic we will review only a couple of projects from this sample.

FireMonkey Host

This simple host application is able to load both Delphi and .NET plugins. Fore more information on how to create a FireMonkey host, please refer to this article.

Let's review the code:

procedure TMainForm.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  ModuleManager.LoadModules('*plugin*.dll', false);
  ModuleManager.LoadModules('*plugin*.xap');
  for I := 0 to ModuleManager.PluginDescriptorCount -1 do
    PluginBox.Items.Add(ModuleManager.PluginDescriptors[i].Name);
end;

procedure TMainForm.PluginBoxDblClick(Sender: TObject);
begin
  if PluginBox.Selected <> nil then begin
    Label1.Visible := false;
    ModuleManager.ReleaseInstance(Instance);
    ModuleManager.CreateVisualPlugin(PluginBox.Selected.Text, Instance, PluginContainer);
  end;
end;
  • OnCreate event handler - This methods loads all plugins by a search pattern. Please note that the first LoadModules method uses two parameters. The second parameter tells the module manager to use a single AppDomain for all .NET plugin modules.
  • PluginBoxDblClick method - Creates a new instance of the selected plugin and shows it in a plugin container.

WinForms Host

This host application works like the previous host, and also allows to use all available plugins . Please take a look at the following article that describes how to create a WinForms host application: WinForms Host.

  private void LoadModule(string FileName)
  {
    try
    {
      moduleManager1.LoadModule(FileName);
    }
    catch (System.Reflection.ReflectionTypeLoadException Ex)
    {
      lb_Plugins.Items.Add(Path.GetFileName(FileName) + " failed to load (" + Ex.LoaderExceptions[0].Message + ")");
    }
    catch (Exception Ex)
    {
      lb_Plugins.Items.Add(Path.GetFileName(FileName) + " failed to load (" + Ex.Message + ")");
    }
  }

  private void MainForm_Load(object sender, System.EventArgs e)
  {
    String[] lFiles = Directory.GetFiles(Path.GetDirectoryName(typeof(MainForm).Assembly.Location), "*PluginModule.dll");
    foreach (string f in lFiles)
    {
      LoadModule(f);
    }

    LoadModule("SilverlightPluginModule.xap");

    foreach (PluginDescriptor p in moduleManager1.Plugins)
    {
      lb_Plugins.Items.Add(p);
    }
  }

  private void lb_Plugins_DoubleClick(object sender, System.EventArgs e)
  {
    try
    {
      if (lb_Plugins.SelectedIndex == -1)
        return;
      if (lb_Plugins.SelectedItem is PluginDescriptor)
      {
        fCurrentPlugin = null;
        pnl_Host.UnhostPlugin();

        fCurrentPlugin = moduleManager1.CreateInstance(lb_Plugins.SelectedItem as PluginDescriptor);
        pnl_Host.HostPlugin(fCurrentPlugin as IBasePlugin);

        if (fCurrentPlugin is IHYCrossPlatformNonVisualPlugin)
          (fCurrentPlugin as IHYCrossPlatformNonVisualPlugin).Start();
      }
    }
    catch (Exception Ex)
    {
      MessageBox.Show(Ex.Message);
    }
  }
  • LoadModule method - A helper method that loads a specified module with the module manager and performs exception handling.
  • MainForm_Load - Loads all plugins in an application folder and displays the plugin descriptors.
  • lb_Plugins_DoubleClick method - Creates an instance of a selected plugin and displays its content using the HostPanel component.

Plugins

This sample includes a whole set of projects that represents all available type of plugins. Please refer to following articles to get more details about each specific plugin type:

WinForms plugin: A plugin that shows a simple calculator:

WPF plugin that shows 3D object:

A Silverlight plugin that shows animation with scrolling text:

VCL plugin that shows simple converter tool:

Putting it All Together

Now that we've examined all major parts of the sample, we can build both plugins and hosts. After we launch the sample, we should get the following results:

FireMonkey host with WPF plugin:

Delphi host with Silverlight plugin:

WinForms host with VCL plugin:

Concepts Covered