How to override IUnknown methods in an ATL server (218982)



The information in this article applies to:

  • The Microsoft Active Template Library (ATL) 3.0, when used with:
    • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
    • Microsoft Visual C++, 32-bit Professional Edition 6.0
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
    • Microsoft Visual C++ 2005 Express Edition
    • Microsoft Visual C++ .NET (2003)
    • Microsoft Visual C++ .NET (2002)

This article was previously published under Q218982
Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.

SUMMARY

This article shows how to override the IUnknown methods (AddRef, Release and QueryInterface) in an ATL server.

MORE INFORMATION

When the client calls AddRef, Release or QueryInterface, the following functions that can be overridden are called:
CComObjectRootEx<>::InternalAddRef()
CComObjectRootEx<>::InternalRelease()
CComObjectRootBase::InternalQueryInterface()
To override these functions, copy and paste the following functions in your ATL class:
ULONG InternalAddRef()
{
        // do something
        return CComObjectRootEx<_ThreadModel>::InternalAddRef();
}

ULONG InternalRelease()
{
	// do something
	return CComObjectRootEx<_ThreadModel>::InternalRelease();
}

HRESULT WINAPI InternalQueryInterface(void* pThis, const _ATL_INTMAP_ENTRY* pEntries, REFIID iid, void** ppvObject)
{
	// do something
	return CComObjectRootBase::InternalQueryInterface(pThis, pEntries, iid, ppvObject);
}
				

Modification Type:MajorLast Reviewed:1/10/2006
Keywords:kbArchitecture kbhowto KB218982 kbAudDeveloper