KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q218982: HOWTO: Override IUnknown Methods in an ATL Server

Article: Q218982
Product(s): Microsoft C Compiler
Version(s): 3.0,6.0
Operating System(s): 
Keyword(s): kbCOMt kbVC600 kbATL300 kbGrpDSMFCATL kbArchitecture
Last Modified: 11-JUN-2002

-------------------------------------------------------------------------------
The information in this article applies to:

- The Microsoft Active Template Library (ATL) 3.0, included with:
   - Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0 
   - Microsoft Visual C++, 32-bit Professional Edition, version 6.0 
   - Microsoft Visual C++, 32-bit Learning Edition, version 6.0 
   - Microsoft Visual C++.NET (2002) 
-------------------------------------------------------------------------------

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);
  }

Additional query words:

======================================================================
Keywords          : kbCOMt kbVC600 kbATL300 kbGrpDSMFCATL kbArchitecture 
Technology        : kbVCsearch kbAudDeveloper kbATLsearch
Version           : :3.0,6.0
Issue type        : kbhowto

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

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.

Copyright Microsoft Corporation 1986-2002.