Article ID: 181265
Article Last Modified on 6/10/2005
// For CComObject you can use its CreateInstance() member.
CComObject<CMyClass>* pMyClass;
// Note that at this point the ref count for the object is 0 (zero).
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: // 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 (zero). // The CComAggObject constructor takes outer unknown as an argument. // The CComPolyObject constructor may or may not take outer unknown as // an argument depending on whether it is being aggregated or not. CComObject<CMyClass>* pMyClass = new CComObject<CMyClass>(); HRESULT hr = pMyClass->FinalConstruct();
CComAggObject<CMyClass>* pMyClass; // pUnkOuter is IUnknown of the aggregate object, FinalConstruct() is // called and the ref count is 0 (zero). HRESULT hRes = CComAggObject<CMyClass>::CreateInstance(m_pUnkOuter, &pMyClass);In ATL 3.0, the _CreateClass:CreateInstace() function is wrapped by the CreateInstance() method of CcomCoClass:
// The following calls increment the ref count to 1 and call // FinalConstruct. // If CMyClass is aggregateable, either DECLARE_AGGREGATABLE or // DECLARE_POLY_AGGREGATABLE. // m_pOuterUnk, the outer unknown can be NULL for the poly // aggregateable case. LPUNKNOWN m_pInnerUnk; CComCoClass<CMyClass>::CreateInstance(m_pOuterUnk, &m_pInnerUnk); // If CMyClass uses the DECLARE_NOT_AGGREGATABLE macro, use following // version: CComCoClass<CMyClass>::CreateInstance(&m_pInnerUnk);(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Jaganathan Thangavelu, Microsoft Corporation.
Keywords: kbhowto kbctrl kbfaq KB181265