【发布时间】:2019-11-09 21:44:18
【问题描述】:
我正在尝试在 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);
};
};
在设计视图中,我点击了刷新实现、注册类型库和另存为类型库文件。它说 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 下。我错过了什么吗?
【问题讨论】:
-
Delphi 是原生的,因此可能存在 32 位与 64 位的问题。尝试将delphi lib编译为64位?
-
@MarcovandeVoort Delphi 和 WPF 方案都是 32 位的,所以两边都应该编译为 32 位。
-
如果您使用 IDE 的主菜单并选择 Component->Import Component->Import ActiveX 控件,您的 ActiveX 是否会显示在要导入的列表中?
-
@KenWhite 不,它没有。奇怪...
-
如果您尝试使用 Delphi 的 TRegSvr 实用程序注册您的 dll,您会得到什么结果 + msg?