首页 > 解决方案 > Delphi COM 对象未出现在 .NET 中

问题描述

我正在尝试在 Delphi 中创建一个 COM 对象并在 C# WPF 项目中使用它。

我在 Delphi 10.3 中创建了一个新的 DLL 项目,使用File -> New -> Other,然后是Delphi -> Windows -> ActiveX Library。我在 GUI 编辑器中为我的 *.ridl 文件创建了以下 IDL:

[
  uuid(E999851C-1E08-4C64-B82A-C3A979F96C2F),
  version(1.0)

]
library DelphiCOMService
{

  importlib("stdole2.tlb");

  interface IDelphiCOMService;


  [
    uuid(50749CD6-4BA7-4200-AB87-67D9590EAA1A),
    version(1.0),
    dual,
    oleautomation
  ]
  interface IDelphiCOMService: IDispatch
  {
    [id(0x000000C9)]
    HRESULT _stdcall EmbedWPFWindow([in] unsigned long Pointer, [in] int Width, [in] int Height);
    [id(0x000000CA)]
    HRESULT _stdcall WindowResized([in] int Width, [in] int Height);
  };

};

在设计视图中,我点击Refresh ImplementationRegister Type LibrarySave As Type Library File。它说 ActiveX 服务器注册成功。我在我的项目上点击了构建。没有错误或问题。

我添加了以下单元来实现接口:

unit DelphiCOMServiceImplementation;

interface

uses ComObj, DelphiCOMService_TLB, Winapi.ActiveX;

type
  DelphiCOMService = class(TAutoObject, IDelphiCOMService)
  public
    procedure EmbedWPFWindow(Pointer: LongWord; Width: SYSINT; Height: SYSINT); safecall;
    procedure WindowResized(Width: SYSINT; Height: SYSINT); safecall;
  end;

implementation

procedure DelphiCOMService.EmbedWPFWindow(Pointer: LongWord; Width: SYSINT; Height: SYSINT); safecall;
begin

end;

procedure DelphiCOMService.WindowResized(Width: SYSINT; Height: SYSINT); safecall;
begin

end;

end.

我重建了我的项目,到目前为止没有错误。我去点击Run -> ActiveX Server -> Register。它是成功的。

我希望它现在在我的系统上注册了 COM 对象,还是我错了?在我的 WPF C# 项目中,当我尝试Add Reference...时,它没有显示在COM -> Type Libraries下。我错过了什么吗?

标签: c#.netwpfdelphipascal

解决方案


我在 *.ridl 设计编辑器中重建了 COM 对象的接口;我尝试添加与 DispInterface 相同的接口只是为了踢球,但这不起作用,然后删除 DispInterface 并重新添加原始接口似乎有效(+ 刷新、注册和另存为 TLB)。另外,我注意到为我的界面保留 0.0 版本可能是它起作用的原因。最初,当我第一次构建它时,我将版本调整为 1.0。如果有人能解释为什么会发生这种情况,我也很想知道!


推荐阅读