本文介绍了在Delphi中使用dispInterface没有classid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经导入一个类型库到Delphi XE2有很多dispinterfaces。大多数都有一个自动生成的coclass和相应的delphi T接口自动创建。



然而,我需要处理的一些dispinterfaces没有classid;我试过了我可以在网上找到的每个例子,以利用所讨论的dispinterface。接口如下:

  DIID_ITableListener:TGUID ='{83D42EA5-2C18-46EB- 823B-262D62DF8CF1}'; 
...
ITableListener = dispinterface;
...
// ************************************* ******************************** //
// DispIntf:ITableListener
//标记:(4096)Dispatchable
// GUID:{83D42EA5-2C18-46EB-823B-262D62DF8CF1}
// ******************** ************************************************* / /
ITableListener = dispinterface
['{83D42EA5-2C18-46EB-823B-262D62DF8CF1}']
程序添加(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;

以下是使用此dispinterinterface的类型库封装器中另一个接口的示例:

 过程subscribeUpdate(updateType:TableUpdateType; const listenerObj:ITableListener); 

下面是一个VB.NET示例,解释了如何使用dispinterface: / p>

 '如果要使用ITableListener接口的方法,必须创建
'class +实现接口。例如,

public class TableListener implements ITableListener {}

'在您的应用程序中,您必须创建一个实现
'接口的类的实例。例如,

TableListener tableListener = new TableListener();

没有ClassID如何在Delphi中实现?如何做实例化?我认为这将涉及到实现一个IDispatch接口后代,但是没有一个ClassID我找不到一种方法使这项工作。



帮助! / p>

感谢。

解决方案

a href =http://delphi-kb.blogspot.com/2010/12/what-is-dispinterface.html =nofollow>链接,并将您的界面替换为链接,并得出这样:

  ITableListener = dispinterface 
['{83D42EA5-2C18-46EB-823B- 262D62DF8CF1}']
程序添加(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;

IMyTableListener = interface(IDispatch)
['{INSERT ARBITRARY GUID HERE}']
程序添加(const rowID:WideString; const rowDataObj:IRow);
procedure Changed(const rowID:WideString; const rowDataObj:IRow);
procedure Deleted(const rowID:WideString; const rowDataObj:IRow);
procedure StatusChanged(status:TableStatus);
end;


TMyTableListener = class(TAutoObject,IMyTableListener)
public
程序添加(const rowID:WideString; const rowDataObj:IRow);
procedure Changed(const rowID:WideString; const rowDataObj:IRow);
procedure Deleted(const rowID:WideString; const rowDataObj:IRow);
procedure StatusChanged(status:TableStatus);
end;

{...}
var
Disp:IDispatch;
Dispint:ITableListener;
{...}
//这可能需要注册TMyTableListener ...
Disp:= CreateComObject(TMyTableListener)as IDispatch;
//或者,尝试这个(不确定语法):
Disp:= new TMyTableListener as IDispatch;
Disp.AddRef(); //在C ++中,至少通过new()创建类会导致一个0引用,所以你需要AddRef(),以便释放对象正确地销毁对象...
Dispint:= ITableListener );
subscribeUpdate(updateType,Dispint);
{...}


I've imported a type library into Delphi XE2 that has many dispinterfaces. Most of them have had an autocreated coclass and corresponding delphi T interface automatically created.

However, some of the dispinterfaces that I need to work with have no classid; I've tried every example that I could find on the net to utilize the dispinterface(s) in question. Here's what the interface looks like:

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;

Here's an example from another interface in the type library wrapper that uses this dispinterface:

procedure subscribeUpdate(updateType: TableUpdateType; const listenerObj: ITableListener);

Here's a VB.NET example that is provided that explains how the dispinterface is supposed to be used:

' 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();

With no ClassID how do I implement this in Delphi? How do I do an instantiation? I thought it would have something to do with implementing an IDispatch interface descendant, but again without a ClassID I couldn't find a way to make this work.

Help!

Thanks.

解决方案

So I went through the sample provided in the link, and substituted your interfaces for the interfaces in the link, and came up with this:

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;

IMyTableListener = interface(IDispatch)
  ['{INSERT ARBITRARY GUID HERE}']
  procedure Added(const rowID: WideString; const rowDataObj: IRow); 
  procedure Changed(const rowID: WideString; const rowDataObj: IRow); 
  procedure Deleted(const rowID: WideString; const rowDataObj: IRow);
  procedure StatusChanged(status: TableStatus);
end;


TMyTableListener = class (TAutoObject, IMyTableListener)
public
  procedure Added(const rowID: WideString; const rowDataObj: IRow); 
  procedure Changed(const rowID: WideString; const rowDataObj: IRow); 
  procedure Deleted(const rowID: WideString; const rowDataObj: IRow);
  procedure StatusChanged(status: TableStatus);
end;

{ ... }
var
Disp: IDispatch;
Dispint: ITableListener;
{ ... }
// this may require that TMyTableListener is registered...
Disp := CreateComObject(TMyTableListener) as IDispatch;
// alternatively, try this (not sure about syntax):
Disp := new TMyTableListener as IDispatch;
Disp.AddRef();   // in C++, at least, creating the class via new() results in a 0 refcount, so you need to AddRef() so that releasing the object destroys the object appropriately...
Dispint := ITableListener(Disp);
subscribeUpdate(updateType, Dispint);
{ ... }

这篇关于在Delphi中使用dispInterface没有classid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 00:49