WinForms Visual Plugins (.NET 5+)

This article describes how to create a new .NET 5 WinForms visual plugin.

Getting Started

The Hydra plugin project consists of three parts:

  • Plugin Module - A Class Library project that acts as a container for the module controller and can hold a set of plugins.
  • Module Controller - Entry point of the module. The module controller supplies the host with information about stored plugins and provides methods that allow the host to instantiate and work with plugins.
  • Plugins - Special classes that are stored inside the plugin module.

Creating a project

First a new Class Library project should be createsd using the default Visual Studio Class Library template.

The newly created project requires some fine-tuning to enable features required by the Hydra framework.

Right-click the project file in the Solution Explorer and select option Edit Project File

The default project file is defined as

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

</Project>

The project file needs to be changed to

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <PlatformTarget>x86</PlatformTarget>
    <EnableComHosting>true</EnableComHosting>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>

</Project>

The changes above do the following:

  • <TargetFramework>net5.0-windows</TargetFramework> - sets the target platform to Windows
  • <PlatformTarget>x86</PlatformTarget> - sets the target platform to 32 bit. It should match the corrsponding setting of the host application
  • <UseWindowsForms>true</UseWindowsForms> - adds support of Windows Forms to the project
  • <EnableComHosting>true</EnableComHosting> - enables support of COM interop in the resulting assembly
  • <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> - Forces NuGet references to be copied to the build output. Similar to the Copy Local option afailable for referenced assemblies.

As the last step add reference to the Hydra NuGet package to the project:

Please note that Hydra uses custom package source named 'RemObjects Software'

Module Controller

Module controller is the root of the plugin library. Add a new Hydra Module Controller to the project:

Add Guid and ComVisible attributes to the Module Controller class. These attributes will allow plugins to be loaded by Delphi COM host application:

using System.Runtime.InteropServices;
using RemObjects.Hydra;

namespace PluginSample_WinForms
{
    [ModuleController]
    [Guid("AC35EF6B-98C3-4E51-BFD3-388AA168D4CA")]
    [ComVisible(true)]
    public partial class ModuleController1 : ModuleController
    {
        public ModuleController1()
        {
            InitializeComponent();
        }
    }
}

Plugin

Add a new Plugin to the project:

And that's it. No additional registration is required.

Registering The Plugin

Build the project

The resulting folder should contain plugin assembly, corresponding comhost dll and Hydra assemblies.

The last step is to register the plugin as COM server in the COM infrastrucure.

This can be done using the regsvr32 command:

regsvr32 .\PluginSample_WinForms.comhost.dll

Now this .NET 5-based visual plugin can be loaded by Delphi Hydra host application.