Delphi .NET 中未显示 COM 对象
Delphi COM object not showing up in .NET
我正在尝试在 Delphi 中创建一个 COM 对象并在 C# WPF 项目中使用它。
我在 Delphi 10.3 中创建了一个新的 DLL 项目,使用 File -> New -> Other,然后 Delphi -> Windows -> ActiveX 库。我在 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);
};
};
在设计视图中,我点击了刷新实现、注册类型库和另存为类型库文件。它表示 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.
我重建了我的项目,到目前为止没有错误。我去点击 运行 -> ActiveX 服务器 -> 注册 。成功了。
我希望它现在已经在我的系统上注册了 COM 对象,还是我错了?在我的 WPF C# 项目中,当我尝试 添加引用... 时,它没有显示在 COM -> 类型库 下。我错过了什么吗?
我在 *.ridl 设计编辑器中重建了 COM 对象的界面;我尝试添加与 DispInterface 相同的接口只是为了好玩,但没有用,然后删除 DispInterface 并添加回原始接口似乎有效(+ 刷新、注册和另存为 TLB)。另外,我注意到为我的界面保留 0.0 版本可能是它起作用的原因。最初,我在第一次构建它时将版本调整为 1.0。如果有人能阐明为什么会发生这种情况,我也很想知道!
我正在尝试在 Delphi 中创建一个 COM 对象并在 C# WPF 项目中使用它。
我在 Delphi 10.3 中创建了一个新的 DLL 项目,使用 File -> New -> Other,然后 Delphi -> Windows -> ActiveX 库。我在 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);
};
};
在设计视图中,我点击了刷新实现、注册类型库和另存为类型库文件。它表示 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.
我重建了我的项目,到目前为止没有错误。我去点击 运行 -> ActiveX 服务器 -> 注册 。成功了。
我希望它现在已经在我的系统上注册了 COM 对象,还是我错了?在我的 WPF C# 项目中,当我尝试 添加引用... 时,它没有显示在 COM -> 类型库 下。我错过了什么吗?
我在 *.ridl 设计编辑器中重建了 COM 对象的界面;我尝试添加与 DispInterface 相同的接口只是为了好玩,但没有用,然后删除 DispInterface 并添加回原始接口似乎有效(+ 刷新、注册和另存为 TLB)。另外,我注意到为我的界面保留 0.0 版本可能是它起作用的原因。最初,我在第一次构建它时将版本调整为 1.0。如果有人能阐明为什么会发生这种情况,我也很想知道!