简体   繁体   English

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

[英]How should I create classes in ATL project?

I'm writing an ATL project and I wonder how should I create classes here. 我正在写一个ATL项目,我想知道如何在这里创建类。 Right now I have one class created by Add/Class/ATL Simple Object . 现在我有一个由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. 我想把它分成更小的类,但这个类的方法应该使用CComPtr并将CComPtr作为参数。 I can't create 'simple' c++ class because I don't have CComPtr there. 我不能创建'简单'的c ++类,因为我没有CComPtr

Should I create ATL classes by ATL Simple Object Wizard and then use interface for this class to call methods. 我应该通过ATL简单对象向导创建ATL类,然后使用此类的接口来调用方法。 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 ? 我应该通过Class View / ITestAtlClass / Add / Add方法添加所有公共方法吗? What about constructors? 那么施工人员呢? Do I must initialize my class only by properties (and add them by Class View/ITestAtlClass/Add/Add Property )? 我是否必须仅通过属性初始化我的类(并通过Class View / ITestAtlClass / Add / Add Property添加它们)? And pass every com object by IUnknown interface? 并通过IUnknown接口传递每个com对象?

Can somebody tell me how it should be done in ATL project. 有人能告诉我在ATL项目中应该怎么做。 I will use this smaller classes internally (nobody will create this classes outside my DLL) just to make my code more readable. 我将在内部使用这个较小的类(没有人会在我的DLL之外创建这个类)只是为了让我的代码更具可读性。

I don't understand your comment that you can't use CComPtr from a simple C++ class. 我不明白你的评论,你不能从一个简单的C ++类使用CComPtr 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 构建一个解决问题的干净的C ++对象模型,然后将其包装在一个或多个COM对象的瘦外观层中
  • 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. 始终使用ATL类,并使用CComObject<>和衍生物来实例化和维护这些类,而不需要CoCreateInstance的开销和仅使用公共接口的限制。

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; 如果你有一个名为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();

There's also variations on CComObject<> , eg CComObjectStack<> that use different allocation and reference counting strategies. CComObject<>上也存在变体,例如CComObjectStack<> ,它们使用不同的分配和引用计数策略。

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. 如果您可以解释您对无法使用CComPtr评论意味着什么,也许我可以扩展它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM