【发布时间】:2014-02-12 23:57:29
【问题描述】:
我已经将一个类型库导入到具有许多调度接口的 Delphi XE2 中。他们中的大多数都有一个自动创建的 coclass 和相应的 delphi T 接口。
但是,我需要使用的一些调度接口没有 classid;我已经尝试了可以在网上找到的每个示例来使用相关的调度接口。界面如下:
DIID_ITableListener: TGUID = '{83D42EA5-2C18-46EB-823B-262D62DF8CF1}';
...
ITableListener = dispinterface;
...
// *********************************************************************//
// DispIntf: ITableListener
// Flags: (4096) Dispatchable
// GUID: {83D42EA5-2C18-46EB-823B-262D62DF8CF1}
// *********************************************************************//
ITableListener = dispinterface
['{83D42EA5-2C18-46EB-823B-262D62DF8CF1}']
procedure Added(const rowID: WideString; const rowDataObj: IRow); dispid 1610743809;
procedure Changed(const rowID: WideString; const rowDataObj: IRow); dispid 1610743810;
procedure Deleted(const rowID: WideString; const rowDataObj: IRow); dispid 1610743811;
procedure StatusChanged(status: TableStatus); dispid 1610743812;
end;
下面是类型库包装器中另一个接口的示例,它使用了这个分派接口:
procedure subscribeUpdate(updateType: TableUpdateType; const listenerObj: ITableListener);
这是一个提供的 VB.NET 示例,它解释了应该如何使用调度接口:
' If you want to use methods of the ITableListener interface, you must create a
' class that+ implements the interface. For example,
public class TableListener implements ITableListener { }
' In your application you must create an instance of the class that implements
' the interface. For example,
TableListener tableListener = new TableListener();
没有 ClassID 我如何在 Delphi 中实现它?如何进行实例化?我认为这与实现 IDispatch 接口后代有关,但再次没有 ClassID,我无法找到使其工作的方法。
救命!
谢谢。
【问题讨论】:
-
你应该实现这个接口,所以你可以使用任何你喜欢的ClassID。
-
为了详细说明 Eric 的评论,
ITableListener是一个事件接收器接口,从它被声明为DIID的事实可以看出。您应该在自己的代码中编写一个实现该接口的类(您不需要 ClassID),然后实例化该类并通过其subscribeUpdate()方法将其传递给另一个 COM 对象。这允许 COM 对象在需要时使用您的ITableListener向您发送事件。 -
感谢 Eric 和 Remy 的回复。我确实创建了一个类,但是我无法通过在帮助和在线中找到的示例来弄清楚如何做到这一点并让它发挥作用。我确实创建了一个类作为 TObject 的后代,我相信我也尝试过从 TInterfacedObject 下降。但是,接口调用特别需要 ITableListener 类型的参数。当我尝试通过我的课程时,它不会接受它,因为它是错误的类型。
-
仅供参考,这是一个 .NET 后端;我不知道这会如何影响细节,但我认为我应该指出这一点。有人有例子吗?我尝试了很多不同的东西,但花了大约 8 个小时却无济于事......谢谢。
-
到目前为止,在我能够找到的所有示例中,没有任何效果。底线是,传递的参数需要 dispinterface 类型;它不会采用任何其他类型。所以我再次问,你如何在 delphi 中实现/实例化一个调度接口?请举例。