gpt4 book ai didi

c++ - 如何创建两个派生自另一个的 ATL 接口(interface)?

转载 作者:行者123 更新时间:2023-11-28 06:25:38 26 4
gpt4 key购买 nike

我有一个带有 IDL 接口(interface)的 Dll 项目。我想要我的 dll 中的接口(interface),其中一个可以从另一个派生。我使用 ATL 简单对象向导创建了两个接口(interface)。

[
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;
};
};

现在,我在类 View 中右键单击 CZInterface,然后单击 Impelement Interface IBClass。

但在大陆,这是一个 C# 项目:

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 ...

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

最佳答案

如果您想实现这一点,最好不要使用对象向导。首先,定义您的接口(interface):

[
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 : IZInterface // IBClass inherits from IZInterface
{
[id(3)] HRESULT Method11([out,retval] LONG* l);
};

然后定义实现专用接口(interface)的类。在您的图书馆中执行此操作:

library DllStandardLib
{
importlib("stdole2.tlb");

[
uuid(43CE897F-17F2-4D45-9098-26B7AEE6EC23)
]
coclass BClass
{
[default] interface IBClass;
};

// NOTE: No need for ZInterface coclass.
};

请注意,IZInterface 类并不是真正需要的,除非您想提供一个单独的实现(= 另一个类)。

为了使 QueryInterface 调用正常工作,您还应该将接口(interface)添加到 COM 接口(interface)映射中:

class CBClass : 
// ...
{
// ...

BEGIN_COM_MAP(CBClass)
COM_INTERFACE_ENTRY(IBClass)
COM_INTERFACE_ENTRY(IZInterface)
END_COM_MAP()

// ...

// IBClass-Member
STDMETHOD(Method11)(LONG* l); // Implement this in your source file (STDMETHODIMP CBClass::Method11(LONG* l) { return E_NOTIMPL; }
}

要从 .NET 调用您的 IBClass 接口(interface)成员,您必须创建正确类的实例:

DllStandardLib.BClass bc = new DllStandardLib.BClass();
bc.Method11();

关于c++ - 如何创建两个派生自另一个的 ATL 接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28578855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com