HOWTO: Create ATL COM ObjectsLast reviewed: February 20, 1998Article ID: Q181265 |
The information in this article applies to:
SUMMARYATL Component Object Model (COM) objects are new instances of the CComObject, CcomAggObject, or CComPolyObject class. ATL COM objects are not new instances of your class that are multiply inherited from the CComCoClass, CComObjectRoot, and the ATL interface implementation (impl) classes. If you try to create a new instance of your class, you get all the following errors and warnings messages:
MORE INFORMATIONThe following code illustrates how to create an instance of your ATL COM object without using the APIs, as in the COM creation mechanism:
Sample Code
// For CComObject you can use its CreateInstance() member. CComObject<CMyClass>* pMyClass; // Note that at this point that the ref count for the object is 0. HRESULT hRes = CComObject<CMyClass>::CreateInstance(&pMyClass); // DECLARE_AGGREGATABLE or no macro in CMyClass indicates that the COM // object is based on CComAggObject. DECLARE_POLY_AGGREGATABLE macro in // CMyClass indicates that the COM object is based on CComPolyObject. // The ref count of this object will be 1. LPUNKNOWN m_pInnerUnk; // m_pOuterUnk is the IUnknown of the outer object. It can be NULL if // the COM object is based on CcomPolyObject. CMyClass::_CreatorClass::CreateInstance(m_pOuterUnk, IID_IUnknown, (LPVOID*)&m_pInnerUnk);Alternatively you can create an instance of your class using the "new" operator:
Sample Code
// Use CComObject, CComPolyObject, and CComAggObject as appropriate. // In this case, the FinalConstruct() member is not called. // Note that at this point the ref count for the object is 0. // CComAggObject constructor takes outer unknown as arg. // CComPolyObject constructor may or may not take outer unknown as arg // depending on whether it is being aggregated or not. CComObject<CMyClass>* pMyClass = new CComObject<CMyClass>(); HRESULT hr = pMyClass->FinalConstruct();(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Jaganathan Thangavelu, Microsoft Corporation Keywords : AtlControl AtlIss AtlServer kbcode Version : WINDOWS:2.0,2.1 Platform : WINDOWS Issue type : kbinfo |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |