如何使用 VisualPlugin 从 .NET 调用 Delphi 方法?

How to call a Delphi method from .NET using a VisualPlugin?

我创建了一个新的 window 基础 class,它在打开 WPF window 时被调用。 在此基础 class 中,应调用 Delphi 方法。

接口:.NET:

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

namespace MyNameSpace.CrossPlatform
{
[Guid("11111111-2222-3333-4444-555566667777"), ComVisible(true)]
public interface ICrossPlatformInterface : IHYCrossPlatformInterface
{
   void MyMethodToCall(string windowName);
}

Delphi:

ICrossPlatformInterface = interface(IHYCrossPlatformInterface)
['{11111111-2222-3333-4444-555566667777}']
  procedure MyMethodToCall(const windowName: WideString); safecall;
end;

插件看起来像这样:

[Plugin, VisualPlugin, ComVisible(true)]
public partial class Plugin : VisualPlugin, IHYCrossPlatformInterface, ICrossPlatformInterface 
{
   ...
   public void EntryPointCalledFromWinBaseClass(string windowName)
   {
      // What to do?
   }

Delphi:

type
TCrossPlatformFactory = class(THYFakeIDispatch, ICrossPlatformInterface)
public
   procedure MyMethodToCall(const WindowName: WideString); safecall;
end;

如何调用此 Delphi 方法?

嗯,我的解决方案是首先在 class 的 Delphi 中创建一个实例,其中包含我想从 .NET 调用的函数。

FCrossPlatformFactory := TCrossPlatformFactory.Create;

然后我必须调用 .NET 函数并将实例作为参数传递。

Factory.SetCrossPlatformFactory(FCrossPlatformFactory);

.NET:

public static ICrossPlatformInterface CrossPlatformFactory
{
    get { return Factory._getCrossPlatformFactory; }
    set { Factory._getCrossPlatformFactory = value; }
}

public void SetCrossPlatformFactory(ICrossPlatformInterface crossPlatformFactory)
{
    CrossPlatformFactory = crossPlatformFactory;
}

一旦实例在 .NET 中已知,就可以调用该函数。

Factory.CrossPlatformFactory.MyMethodToCall(windowName);