home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap05 / ekoala1 / koala.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  2KB  |  120 lines

  1. /*
  2.  * KOALA.CPP
  3.  * Koala Object Independent of DLL/EXE Servers, Chapter 5
  4.  *
  5.  * Implementation of the CKoala object that works in either
  6.  * an EXE or DLL as it only implements IUnknown.
  7.  *
  8.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Microsoft
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #include "koala.h"
  17.  
  18.  
  19. /*
  20.  * CKoala::CKoala
  21.  * CKoala::~CKoala
  22.  *
  23.  * Parameters (Constructor):
  24.  *  pUnkOuter       LPUNKNOWN of a controlling unknown.
  25.  *  pfnDestroy      PFNDESTROYED to call when an object
  26.  *                  is destroyed.
  27.  */
  28.  
  29. CKoala::CKoala(LPUNKNOWN pUnkOuter, PFNDESTROYED pfnDestroy)
  30.     {
  31.     m_cRef=0;
  32.     m_pUnkOuter=pUnkOuter;
  33.     m_pfnDestroy=pfnDestroy;
  34.     return;
  35.     }
  36.  
  37. CKoala::~CKoala(void)
  38.     {
  39.     return;
  40.     }
  41.  
  42.  
  43.  
  44. /*
  45.  * CKoala::Init
  46.  *
  47.  * Purpose:
  48.  *  Performs any intiailization of a CKoala that's prone to failure
  49.  *  that we also use internally before exposing the object outside.
  50.  *
  51.  * Parameters:
  52.  *  None
  53.  *
  54.  * Return Value:
  55.  *  BOOL            TRUE if the function is successful,
  56.  *                  FALSE otherwise.
  57.  */
  58.  
  59. BOOL CKoala::Init(void)
  60.     {
  61.     //Nothing to do.
  62.     return TRUE;
  63.     }
  64.  
  65.  
  66.  
  67.  
  68. /*
  69.  * CKoala::QueryInterface
  70.  * CKoala::AddRef
  71.  * CKoala::Release
  72.  *
  73.  * Purpose:
  74.  *  IUnknown members for CKoala object.
  75.  */
  76.  
  77. STDMETHODIMP CKoala::QueryInterface(REFIID riid, PPVOID ppv)
  78.     {
  79.     *ppv=NULL;
  80.  
  81.     /*
  82.      * The only calls for IUnknown are either in a nonaggregated
  83.      * case or when created in an aggregation, so in either case
  84.      * always return our IUnknown for IID_IUnknown.
  85.      */
  86.     if (IID_IUnknown==riid)
  87.         *ppv=this;
  88.  
  89.     if (NULL!=*ppv)
  90.         {
  91.         ((LPUNKNOWN)*ppv)->AddRef();
  92.         return NOERROR;
  93.         }
  94.  
  95.     return ResultFromScode(E_NOINTERFACE);
  96.     }
  97.  
  98.  
  99. STDMETHODIMP_(ULONG) CKoala::AddRef(void)
  100.     {
  101.     return ++m_cRef;
  102.     }
  103.  
  104.  
  105. STDMETHODIMP_(ULONG) CKoala::Release(void)
  106.     {
  107.     if (0L!=--m_cRef)
  108.         return m_cRef;
  109.  
  110.     /*
  111.      * Tell the housing that an object is going away so it can
  112.      * shut down if appropriate.
  113.      */
  114.     if (NULL!=m_pfnDestroy)
  115.         (*m_pfnDestroy)();
  116.  
  117.     delete this;
  118.     return 0;
  119.     }
  120.