我应该如何在ATL项目中创建类?

发布于 2024-10-06 09:42:34 字数 617 浏览 5 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半仙 2024-10-13 09:42:34

我不明白你的评论,即你不能从简单的 C++ 类中使用 CComPtr 。你能澄清一下吗?

我看到两种策略:

  • 构建一个解决问题的干净的 C++ 对象模型,然后将其包装在一个或多个 COM 对象的薄外观层中
  • 始终使用 ATL 类,并使用 CComObject 和衍生物来实例化和维护它们,而无需 CoCreateInstance 的开销和仅使用公共接口的限制。

第一个通常要好得多,但如果您正在构建数据密集的对象模型,则第二个可能是一种有用的技术。

如果您有一个名为 CVehicle 的 ATL COM 类,它派生自 CComObjectRootEx<> 及其朋友,您可以像这样实例化它;

   CComObject<CVehicle>* vehicle = NULL;
   CComObject<CVehicle>::CreateInstance(&vehicle);

   vehicle->AddRef();

   // To get at any of its interfaces, use:
   CComPtr<ICar> car = 0;
   vehicle->QueryInterface(&car);

   // And to delete object, use:
   vehicle->Release();

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:

  • build a clean C++ object model that solves the problem, and then wrap it in a thin facade layer of one or more COM objects
  • Use ATL classes throughout, and use CComObject<> and derivatives to instantiate and maintain these without the overhead of CoCreateInstance 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 from CComObjectRootEx<> and friends, you can instantiate it like so;

   CComObject<CVehicle>* vehicle = NULL;
   CComObject<CVehicle>::CreateInstance(&vehicle);

   vehicle->AddRef();

   // To get at any of its interfaces, use:
   CComPtr<ICar> car = 0;
   vehicle->QueryInterface(&car);

   // And to delete object, use:
   vehicle->Release();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文