看到 《常见COM问题解答》这篇文章的时候,里面有讲个问题,关于在COM里实现多重接口的问题。
描述如下:如何实现多重接口 其实非常非常简单,比如想建立一个COM对象,它已经支持IFooBar接口了,我们还想实现两个外部接口IFoo和IBar。IFoo和IBar 接口定义如下: IFoo = interface procedure Foo; //隐含返回HRESULT end; IBar = interface procedure Bar; end; 实现部分: type TFooBar = class (TAutoObject, IFooBar, IFoo, IBar) Protected //IfooBar ... IFooBar methods here ... //IFoo methods procedure Foo; //IBar methods procedure Bar; ... end; procedure TFooBar.Foo; begin end; procedure TFooBar.Bar; begin end; 是不是很简单啊,要注意的是如果IfooBar、IFoo和IBar都是基于IDispatch接口的,TAutoObject 将只会为IFooBar实现IDispatch,基于脚本的客户端只能看到IFooBar接口方法。
现在有个问题, 客户端调用的时候只能返回IFooBar 这个接口,但是如果我想用 IFoo 和IBar接口,怎么办呢?
最近在看OPC服务器开发的相关资料,在OPC基金会下载到了个delphi开发的OPC Server (FirstServ) 里面就不类似的问题。unit ServIMPL;{$IFDEF VER150}
{$WARN UNSAFE_TYPE OFF}
{$ENDIF}interfaceuses Windows,ComObj,ActiveX,Axctrls,MRD_TLB,OPCDA,SysUtils,Dialogs,Classes,
OPCCOMN,StdVCL,enumstring,ItemPropIMPL,Globals,OpcError,OPCErrorStrings,
EnumUnknown,OPCTypes;type
TDA2 = class(TAutoObject,IDA2,IOPCServer,IOPCCommon,IOPCServerPublicGroups,
IOPCBrowseServerAddressSpace,IPersist,IPersistFile,
IConnectionPointContainer,IOPCItemProperties)
private
FIOPCItemProperties:TOPCItemProp;
FIConnectionPoints:TConnectionPoints;
protected
.....
其中 IDA2 是这个服务器的COM接口
class function CoDA2.Create: IDA2;
begin
Result := CreateComObject(CLASS_DA2) as IDA2;
end;class function CoDA2.CreateRemote(const MachineName: string): IDA2;
begin
Result := CreateRemoteComObject(MachineName, CLASS_DA2) as IDA2;
end;class function CoOPCGroup.Create: IOPCGroup;
begin
Result := CreateComObject(CLASS_OPCGroup) as IOPCGroup;
end;class function CoOPCGroup.CreateRemote(const MachineName: string): IOPCGroup;
begin
Result := CreateRemoteComObject(MachineName, CLASS_OPCGroup) as IOPCGroup;
end;这些返回的都是IDA2 但是我想使用的是IOPCServer 等接口, 不知道如何调用。请赐教!
描述如下:如何实现多重接口 其实非常非常简单,比如想建立一个COM对象,它已经支持IFooBar接口了,我们还想实现两个外部接口IFoo和IBar。IFoo和IBar 接口定义如下: IFoo = interface procedure Foo; //隐含返回HRESULT end; IBar = interface procedure Bar; end; 实现部分: type TFooBar = class (TAutoObject, IFooBar, IFoo, IBar) Protected //IfooBar ... IFooBar methods here ... //IFoo methods procedure Foo; //IBar methods procedure Bar; ... end; procedure TFooBar.Foo; begin end; procedure TFooBar.Bar; begin end; 是不是很简单啊,要注意的是如果IfooBar、IFoo和IBar都是基于IDispatch接口的,TAutoObject 将只会为IFooBar实现IDispatch,基于脚本的客户端只能看到IFooBar接口方法。
现在有个问题, 客户端调用的时候只能返回IFooBar 这个接口,但是如果我想用 IFoo 和IBar接口,怎么办呢?
最近在看OPC服务器开发的相关资料,在OPC基金会下载到了个delphi开发的OPC Server (FirstServ) 里面就不类似的问题。unit ServIMPL;{$IFDEF VER150}
{$WARN UNSAFE_TYPE OFF}
{$ENDIF}interfaceuses Windows,ComObj,ActiveX,Axctrls,MRD_TLB,OPCDA,SysUtils,Dialogs,Classes,
OPCCOMN,StdVCL,enumstring,ItemPropIMPL,Globals,OpcError,OPCErrorStrings,
EnumUnknown,OPCTypes;type
TDA2 = class(TAutoObject,IDA2,IOPCServer,IOPCCommon,IOPCServerPublicGroups,
IOPCBrowseServerAddressSpace,IPersist,IPersistFile,
IConnectionPointContainer,IOPCItemProperties)
private
FIOPCItemProperties:TOPCItemProp;
FIConnectionPoints:TConnectionPoints;
protected
.....
其中 IDA2 是这个服务器的COM接口
class function CoDA2.Create: IDA2;
begin
Result := CreateComObject(CLASS_DA2) as IDA2;
end;class function CoDA2.CreateRemote(const MachineName: string): IDA2;
begin
Result := CreateRemoteComObject(MachineName, CLASS_DA2) as IDA2;
end;class function CoOPCGroup.Create: IOPCGroup;
begin
Result := CreateComObject(CLASS_OPCGroup) as IOPCGroup;
end;class function CoOPCGroup.CreateRemote(const MachineName: string): IOPCGroup;
begin
Result := CreateRemoteComObject(MachineName, CLASS_OPCGroup) as IOPCGroup;
end;这些返回的都是IDA2 但是我想使用的是IOPCServer 等接口, 不知道如何调用。请赐教!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货