home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / numega / sc501.exe / data1.cab / Examples / IUNKNOWN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-25  |  6.0 KB  |  203 lines

  1. /*
  2.  * IUnknown.cpp
  3.  * $Header: /bcsample/IFACEDLL/IUNKNOWN.CPP 1     5/28/96 1:12p Dave $
  4.  *
  5.  * Description:
  6.  *  The implementation of the CIUnknown interface base class.
  7.  *
  8.  * Notes:
  9.  *  <implementation notes go here>
  10.  *
  11.  ***********************************************************************
  12.  *
  13.  * NuMega Technologies, Inc.
  14.  * P.O. Box 7780
  15.  * Nashua, NH 03060
  16.  *
  17.  * (c) Copyright 1994, 1995, 1996 NuMega Technologies, Inc.
  18.  * ALL RIGHTS RESERVED.
  19.  *
  20.  ***********************************************************************
  21.  *
  22.  **********************************************************************/
  23. #include "IFT_OLE.h"
  24. #include "IUnknown.h"
  25. // For each class that is an interface, include it here.
  26. // IOleWindow
  27. #include "OleWnd.h"
  28. // IOleControl
  29. #include "OleCntrl.h"
  30. // IDataObject
  31. #include "DataObj.h"
  32. // IViewObject
  33. #include "VwObj.h"
  34.  
  35. /*----------------------------------------------------------------------
  36. FUNCTION    :   CIUnknown :: CIUnknown
  37. DISCUSSION  :
  38.     Constructor for the main IUnknown interface.
  39. PARAMETERS  :
  40.     pUnkOuter  - LPUKNOWN of controlling unknown.
  41. RETURN      :
  42.     None.
  43. ----------------------------------------------------------------------*/
  44. CIUnknown :: CIUnknown ( LPUNKNOWN    pUnkOuter  ,
  45.                          PFNDESTROYED pfnDestroy  )
  46.  
  47. {
  48.     m_cRef = 0 ;
  49.     m_pUnkOuter = pUnkOuter ;
  50.     m_pfnDestroy = pfnDestroy ;
  51.     for ( int i = 0 ; i < 100 ; i++ )
  52.     {
  53.         m_pInterfaceTable[ i ] = NULL ;
  54.     }
  55. }
  56.  
  57. /*----------------------------------------------------------------------
  58. FUNCTION    :   CIUnknown :: ~CIUnknown
  59. DISCUSSION  :
  60.     Destructor for the main IUnknown interface.
  61. PARAMETERS  :
  62.     None.
  63. RETURN      :
  64.     None.
  65. ----------------------------------------------------------------------*/
  66. CIUnknown :: ~CIUnknown ( void )
  67. {
  68.     delete m_pInterfaceTable[0] ;
  69.     delete m_pInterfaceTable[1] ;
  70.     delete m_pInterfaceTable[2] ;
  71.     delete m_pInterfaceTable[3] ;
  72. }
  73.  
  74. /*----------------------------------------------------------------------
  75. FUNCTION    :   CIUnknown :: Init
  76. DISCUSSION  :
  77.     The initialization function for the class.  This is where we need to
  78. do work that could fail.  This is called by the class factory when the
  79. class is created.
  80. PARAMETERS  :
  81.     None.
  82. RETURN      :
  83.     TRUE  - The initialization was completed.
  84.     FALSE - There was a problem.
  85. ----------------------------------------------------------------------*/
  86. BOOL CIUnknown :: Init ( void )
  87. {
  88.     // Here is where we allocate all the interfaces that we know about.
  89.     // IOleWindow
  90.     m_pInterfaceTable[ 0 ] =
  91.                 (PCInternalQuery)new CIOleWindow        ( m_pUnkOuter ,
  92.                                                           this        );
  93.     // IOleControl
  94.     m_pInterfaceTable[ 1 ] =
  95.                 (PCInternalQuery)new CIOleControl       ( m_pUnkOuter ,
  96.                                                           this        );
  97.     // IDataObject
  98.     m_pInterfaceTable[ 2 ] =
  99.                 (PCInternalQuery)new CIDataObject       ( m_pUnkOuter ,
  100.                                                           this        );
  101.     // IViewObject
  102.     m_pInterfaceTable[ 3 ] =
  103.                 (PCInternalQuery)new CIViewObject       ( m_pUnkOuter ,
  104.                                                           this        );
  105.  
  106.     return ( TRUE ) ;
  107. }
  108.  
  109. /*----------------------------------------------------------------------
  110. FUNCTION    :   CIUnknown :: QueryInterface
  111. DISCUSSION  :
  112.     The implementation of the IUnknown::QueryInterface function.
  113. PARAMETERS  :
  114.     riid - The IID of the interface requested.
  115.     ppv  - Receives the returned interface.
  116. RETURN      :
  117.     NOERROR       - The requested interface is in ppv.
  118.     E_NOINTERFACE - The requested interface is not implemented.
  119. ----------------------------------------------------------------------*/
  120. STDMETHODIMP CIUnknown :: QueryInterface ( REFIID riid , PPVOID ppv )
  121. {
  122.     if ( NULL == ppv )
  123.     {
  124.         return ( E_INVALIDARG ) ;
  125.     }
  126.     // Always set the interface memory location to NULL.
  127.     *ppv = NULL ;
  128.  
  129.     // If it is just the IUnknown, return ourselves.
  130.     if ( IID_IUnknown == riid )
  131.     {
  132.         *ppv = (IUnknown*)this ;
  133.     }
  134.     else
  135.     {
  136.         // For any other interface, pound through the table of other
  137.         //  interfaces.
  138.         int i = 0 ;
  139.         do
  140.         {
  141.             PCInternalQuery pClass =
  142.                                (PCInternalQuery)m_pInterfaceTable[ i ] ;
  143.             if ( NOERROR == pClass->InternalQueryInterface ( riid ,
  144.                                                              ppv   ) )
  145.             {
  146.                 break ;
  147.             }
  148.             i++ ;
  149.         } while ( NULL != m_pInterfaceTable[ i ] ) ;
  150.     }
  151.  
  152.     if ( NULL != *ppv )
  153.     {
  154.         ((LPUNKNOWN)*ppv)->AddRef ( ) ;
  155.         return ( NOERROR ) ;
  156.     }
  157.  
  158.     return ( E_NOINTERFACE ) ;
  159. }
  160.  
  161. /*----------------------------------------------------------------------
  162. FUNCTION    :   CIUnknown :: AddRef
  163. DISCUSSION  :
  164.     The implementation of the IUnknown::AddRef function.
  165. PARAMETERS  :
  166.     None.
  167. RETURN      :
  168.     The new reference count for diagnostic purposes only.
  169. ----------------------------------------------------------------------*/
  170. STDMETHODIMP_( ULONG ) CIUnknown :: AddRef ( void )
  171. {
  172.     m_cRef++ ;
  173.     return ( m_cRef ) ;
  174. }
  175.  
  176.  
  177. /*----------------------------------------------------------------------
  178. FUNCTION    :   CIUnknown :: Release
  179. DISCUSSION  :
  180.     The implementation of the IUnknown::Release function.
  181. PARAMETERS  :
  182.     None.
  183. RETURN      :
  184.     The new reference count for diagnostic purposes only.
  185. ----------------------------------------------------------------------*/
  186. STDMETHODIMP_( ULONG ) CIUnknown :: Release ( void )
  187. {
  188.     m_cRef-- ;
  189.     if ( 0L != m_cRef )
  190.     {
  191.         return ( m_cRef ) ;
  192.     }
  193.  
  194.     // If we have a notification function to call, do so now.
  195.     if ( NULL != m_pfnDestroy )
  196.     {
  197.         (*m_pfnDestroy ) ( ) ;
  198.     }
  199.  
  200.     delete this ;
  201.     return ( 0 ) ;
  202. }
  203.