本文介绍了继承,两个COM接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个带有IDL接口的DLL项目。

我希望在我的dll中有两个接口,以便其中一个可以从另一个接口派生。

我用ATL简单对象向导创建了两个接口。

Hi,
I have a DLL Project with IDL interfaces.
I want to have two interfaces in my dll so that one of them can be derived from another.
I created two interfaces with ATL Simple Object Wizard.

[
    object,
    uuid(7359AF6C-6E90-4372-991F-556602CB3977),
    dual,
    nonextensible,
    pointer_default(unique)
]
interface IZInterface : IDispatch{
    [id(1)] HRESULT ZGetStr([out,retval] BSTR* str);
    [id(2)] HRESULT GetSize([in,out] LONG* nSize);
};
[
	object,
	uuid(8CA6DBF2-E402-464D-96AE-3D6642D91E14),
	pointer_default(unique)
]
interface IBClass : IUnknown{
	[] HRESULT Method11([out,retval] LONG* l);
};
library DllStandardLib
{
	importlib("stdole2.tlb");
	[
		uuid(491DF659-012F-4C20-90AA-0CBC5BDE5A68)
	]
	coclass ZInterface
	{
		[default] interface IZInterface;
	};
        [
		uuid(43CE897F-17F2-4D45-9098-26B7AEE6EC23)
	]
	coclass BClass
	{
		[default] interface IBClass;
	};
};





现在,我在类视图中右键单击CZInterface,然后选择Impelement Interface IBClass 。



但是在一个C#项目的容器中:



Now, I right-click on the CZInterface in the Class View, then I select "Impelement Interface IBClass".

But in a container that is a C# project:

DllStandardLib.ZInterface dd = new DllStandardLib.ZInterface();
dd.Method11();//---> Error: DllStandardLib.ZInterface' does not contain a definition for 'Method11' and no extension method 'Method11' accepting a first argument of type ...





我的项目有什么问题?我希望第二个(派生的)接口知道基本接口的所有方法和属性。



请帮助我!



What is the problem with my project? I want that the second (derived) interface would know all the methods and properties of the base interface.

Please, help me!

推荐答案

[
	object,
	uuid(8CA6DBF2-E402-464D-96AE-3D6642D91E14),
	pointer_default(unique)
]
interface IBClass : IDispatch{
	[] HRESULT Method11([out,retval] LONG* l);
};
[
    object,
    uuid(7359AF6C-6E90-4372-991F-556602CB3977),
    dual,
    nonextensible,
    pointer_default(unique)
]
interface IZInterface : IBClass{//it's derived from IBClass, see?
    [id(1)] HRESULT ZGetStr([out,retval] BSTR* str);
    [id(2)] HRESULT GetSize([in,out] LONG* nSize);
};
library DllStandardLib
{
	importlib("stdole2.tlb");
	[
		uuid(491DF659-012F-4C20-90AA-0CBC5BDE5A68)
	]
	coclass ZInterface
	{
		[default] interface IZInterface;
	};
        [
		uuid(43CE897F-17F2-4D45-9098-26B7AEE6EC23)
	]
	coclass BClass
	{
		[default] interface IBClass;
	};
};



感谢Microsoft 。因此,您的IBClass可以从 IDispatch 继承,而不是从 IUnknown 继承。但是您可以尝试通过在C ++中编写一些类来模拟多重继承。例如,以下是OAIdl.h中 IDispatch 的定义


Thanks to Microsoft "The current implementation of MIDL does not handle function overloading or multiple inheritance". So your IBClass can be inherited from IDispatch rather than from IUnknown. But you can try to emulate multiple inheritance by writing some of your classes in C++. For example, here is the definition of IDispatch from OAIdl.h

MIDL_INTERFACE("00020400-0000-0000-C000-000000000046")
IDispatch : public IUnknown
{
public:
    virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(
        /* [out] */ __RPC__out UINT *pctinfo) = 0;

    virtual HRESULT STDMETHODCALLTYPE GetTypeInfo(
        /* [in] */ UINT iTInfo,
        /* [in] */ LCID lcid,
        /* [out] */ __RPC__deref_out_opt ITypeInfo **ppTInfo) = 0;

    virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames(
        /* [in] */ __RPC__in REFIID riid,
        /* [size_is][in] */ __RPC__in_ecount_full(cNames) LPOLESTR *rgszNames,
        /* [range][in] */ __RPC__in_range(0,16384) UINT cNames,
        /* [in] */ LCID lcid,
        /* [size_is][out] */ __RPC__out_ecount_full(cNames) DISPID *rgDispId) = 0;

    virtual /* [local] */ HRESULT STDMETHODCALLTYPE Invoke(
        /* [annotation][in] */
        _In_  DISPID dispIdMember,
        /* [annotation][in] */
        _In_  REFIID riid,
        /* [annotation][in] */
        _In_  LCID lcid,
        /* [annotation][in] */
        _In_  WORD wFlags,
        /* [annotation][out][in] */
        _In_  DISPPARAMS *pDispParams,
        /* [annotation][out] */
        _Out_opt_  VARIANT *pVarResult,
        /* [annotation][out] */
        _Out_opt_  EXCEPINFO *pExcepInfo,
        /* [annotation][out] */
        _Out_opt_  UINT *puArgErr) = 0;

};



然后只需在IDL中包含您的标题。


Then just include your header in your IDL.


这篇关于继承,两个COM接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:06