我应该如何在ATL项目中创建类?
我正在编写一个 ATL 项目,我想知道应该如何在这里创建类。 现在我有一个由Add/Class/ATL Simple Object创建的类。我想将其划分为较小的类,但此类中的方法应使用 CComPtr 并以 CComPtr 作为参数。我无法创建“简单”C++ 类,因为那里没有 CComPtr
。
我应该通过ATL简单对象向导创建ATL类,然后使用该类的接口来调用方法吗?像这里一样:
CComPtr<ITestAtlClass> tptr;
tptr.CoCreateInstance(CLSID_TestAtlClass);
tptr->test();
我应该通过Class View/ITestAtlClass/Add/Add Method添加所有公共方法吗? 那么构造函数呢?我是否必须仅通过属性初始化我的类(并通过Class View/ITestAtlClass/Add/Add Property添加它们)?并通过 IUnknown 接口传递每个 com 对象?
有人能告诉我在 ATL 项目中应该如何完成吗?我将在内部使用这个较小的类(没有人会在我的 DLL 之外创建此类)只是为了使我的代码更具可读性。
I'm writing an ATL project and I wonder how should I create classes here.
Right now I have one class created by Add/Class/ATL Simple Object. I want to divide it to smaller classes but method from this classes should use CComPtr
and have CComPtr
as an argument. I can't create 'simple' c++ class because I don't have CComPtr
there.
Should I create ATL classes by ATL Simple Object Wizard and then use interface for this class to call methods. Like here:
CComPtr<ITestAtlClass> tptr;
tptr.CoCreateInstance(CLSID_TestAtlClass);
tptr->test();
And should I add all public methods by Class View/ITestAtlClass/Add/Add Method?
What about constructors? Do I must initialize my class only by properties (and add them by Class View/ITestAtlClass/Add/Add Property)? And pass every com object by IUnknown interface?
Can somebody tell me how it should be done in ATL project. I will use this smaller classes internally (nobody will create this classes outside my DLL) just to make my code more readable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白你的评论,即你不能从简单的 C++ 类中使用
CComPtr
。你能澄清一下吗?我看到两种策略:
CComObject
和衍生物来实例化和维护它们,而无需 CoCreateInstance 的开销和仅使用公共接口的限制。第一个通常要好得多,但如果您正在构建数据密集的对象模型,则第二个可能是一种有用的技术。
如果您有一个名为
CVehicle
的 ATL COM 类,它派生自CComObjectRootEx<>
及其朋友,您可以像这样实例化它;CComObject<>
也有一些变体,例如使用不同分配和引用计数策略的CComObjectStack
。如您所见,这非常混乱。如果您可以解释您对无法使用
CComPtr
的评论的含义,也许我可以对此进行扩展。I don't understand your comment that you can't use
CComPtr
from a simple C++ class. Can you please clarify?I see two strategies:
CComObject<>
and derivatives to instantiate and maintain these without the overhead ofCoCreateInstance
and the limitations of only using public interfaces.The first one is usually much nicer, but if you're building a data-heavy object model, the second can be a useful technique.
If you have an ATL COM class called
CVehicle
, that derives fromCComObjectRootEx<>
and friends, you can instantiate it like so;There's also variations on
CComObject<>
, e.g.CComObjectStack<>
that use different allocation and reference counting strategies.As you can see, this is pretty messy. If you can explain what you mean by your comment on not being able to use
CComPtr
, maybe I can expand on that.