HOWTO: Aggregate a COM Object with ATLLast reviewed: February 3, 1998Article ID: Q173823 |
The information in this article applies to:
SUMMARYThe following steps demonstrate how to aggregate a COM object in an ATL project:
MORE INFORMATIONIf you use and release an interface from the aggregate during FinalConstruct, you should add the DECLARE_PROTECT_FINAL_CONSTRUCT macro to the definition of your class object. Here is a sample with all four of the steps done in the class definition. This is a simple object, COutObj, which aggregates another simple object, CInnObj.
Sample Code
class ATL_NO_VTABLE COutObj :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<COutObj, &CLSID_OutObj>,
public IDispatchImpl<IOutObj, &IID_IOutObj, &LIBID_OUTEROBJLib>
{
public:
COutObj() : m_pInnerUnk(NULL) // Step 1
{
}
DECLARE_GET_CONTROLLING_UNKNOWN()
DECLARE_REGISTRY_RESOURCEID(IDR_OUTOBJ)
DECLARE_PROTECT_FINAL_CONSTRUCT() // Step 2
BEGIN_COM_MAP(COutObj)
COM_INTERFACE_ENTRY(IOutObj)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY_AGGREGATE(IID_IInnObj, m_pInnerUnk) // Step 3
END_COM_MAP()
// Start of step 4
HRESULT FinalConstruct()
{
HRESULT hr;
CLSID clsidInner;
hr = CLSIDFromProgID(L"InnObj.InnObj.1", &clsidInner);
if (hr == S_OK)
hr = CoCreateInstance(clsidInner, GetControllingUnknown(),
CLSCTX_INPROC_SERVER, IID_IUnknown,
(void**)&m_pInnerUnk);
return hr;
} // End of step 4
void FinalRelease(){m_pInnerUnk->Release();} // Step 5
// IOutObj
public:
STDMETHOD(Test)();
private:
LPUNKNOWN m_pInnerUnk; // Step 1
};
If the inner object is in the same ATL server, then you can use the
following code to create the inner object without CoCreateInstance:
HRESULT FinalConstruct()
{
return CInnObj::_CreatorClass::CreateInstance(
GetControllingUnknown(), IID_IUnknown,
(void**)&m_pInnerUnk);
}
REFERENCESVisual C++ Books Online: Visual C++ Books; C/C++ Language and C++ Library; Active Template Library; Articles; Introduction to COM and ATL; Introduction to COM; Aggregation Visual C++ Books Online: Visual C++ Books; C/C++ Language and C++ Library; Active Template Library; Articles; Fundamentals of ATL COM Objects; Creating an Aggregate (c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Chuck Bell, Microsoft Corporation
|
Additional query words: derive override
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |