HOWTO: Create ATL COM Objects

Last reviewed: February 20, 1998
Article ID: Q181265
The information in this article applies to:
  • The Microsoft Active Template Library (ATL), versions 2.0, 2.1 included with:

        - Microsoft Visual C++, 32-bit Editions, version 5.0
    

SUMMARY

ATL 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:

  • error C2259: 'CMyClass' : cannot instantiate abstract class due to following members:
  • warning C4259: 'long __stdcall IUnknown::QueryInterface(const struct _GUID &,void ** )' : pure virtual function was not defined
  • warning C4259: 'unsigned long __stdcall IUnknown::AddRef(void)' : pure virtual function was not defined
  • warning C4259: 'unsigned long __stdcall IUnknown::Release(void)' : pure virtual function was not defined

MORE INFORMATION

The 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


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 20, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.