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 / chap06 / ekoala3 / koala.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  6.7 KB  |  325 lines

  1. /*
  2.  * KOALA.CPP
  3.  * Koala Object for EXE Servers, Chapter 6
  4.  *
  5.  * Implementation of an object with IExternalConnection.
  6.  *
  7.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Microsoft
  10.  * Internet  :  kraigb@microsoft.com
  11.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  12.  */
  13.  
  14.  
  15. #include "koala.h"
  16.  
  17.  
  18. /*
  19.  * CKoala::CKoala
  20.  * CKoala::~CKoala
  21.  *
  22.  * Parameters (Constructor):
  23.  *  pUnkOuter       LPUNKNOWN of a controlling unknown.
  24.  *  pfnDestroy      PFNDESTROYED to call when an object
  25.  *                  is destroyed.
  26.  */
  27.  
  28. CKoala::CKoala(LPUNKNOWN pUnkOuter, PFNDESTROYED pfnDestroy)
  29.     {
  30.     m_cRef=0;
  31.     m_pUnkOuter=pUnkOuter;
  32.     m_pfnDestroy=pfnDestroy;
  33.  
  34.     //CHAPTER6MOD
  35.     m_cStrong=0;
  36.     m_cWeak=0;
  37.  
  38.     m_pImpIPersist=NULL;
  39.     //End CHAPTER6MOD
  40.     return;
  41.     }
  42.  
  43. CKoala::~CKoala(void)
  44.     {
  45.     //CHAPTER6MOD
  46.     DeleteInterfaceImp(m_pImpIPersist);
  47.     //End CHAPTER6MOD
  48.     return;
  49.     }
  50.  
  51.  
  52.  
  53. /*
  54.  * CKoala::Init
  55.  *
  56.  * Purpose:
  57.  *  Performs any intiailization of a CKoala that's prone to failure
  58.  *  that we also use internally before exposing the object outside.
  59.  *
  60.  * Parameters:
  61.  *  None
  62.  *
  63.  * Return Value:
  64.  *  BOOL            TRUE if the function is successful,
  65.  *                  FALSE otherwise.
  66.  */
  67.  
  68. BOOL CKoala::Init(void)
  69.     {
  70.     //CHAPTER6MOD
  71.     IUnknown   *pUnk=this;
  72.  
  73.     if (NULL!=m_pUnkOuter)
  74.         pUnk=m_pUnkOuter;
  75.  
  76.     m_pImpIPersist=new CImpIPersist(this, pUnk);
  77.  
  78.     //No failure if m_pImpIPersist is NULL, QueryInterface will fail
  79.  
  80.     return TRUE;
  81.     //End CHAPTER6MOD
  82.     }
  83.  
  84.  
  85.  
  86.  
  87. /*
  88.  * CKoala::QueryInterface
  89.  * CKoala::AddRef
  90.  * CKoala::Release
  91.  *
  92.  * Purpose:
  93.  *  IUnknown members for CKoala object.
  94.  */
  95.  
  96. STDMETHODIMP CKoala::QueryInterface(REFIID riid, PPVOID ppv)
  97.     {
  98.     *ppv=NULL;
  99.  
  100.     //CHAPTER6MOD
  101.     if (IID_IUnknown==riid || IID_IExternalConnection==riid)
  102.         *ppv=this;
  103.  
  104.     if (IID_IPersist==riid && NULL!=m_pImpIPersist)
  105.         *ppv=m_pImpIPersist;
  106.     //End CHAPTER6MOD
  107.  
  108.     if (NULL!=*ppv)
  109.         {
  110.         ((LPUNKNOWN)*ppv)->AddRef();
  111.         return NOERROR;
  112.         }
  113.  
  114.     return ResultFromScode(E_NOINTERFACE);
  115.     }
  116.  
  117.  
  118. STDMETHODIMP_(ULONG) CKoala::AddRef(void)
  119.     {
  120.     return ++m_cRef;
  121.     }
  122.  
  123.  
  124. STDMETHODIMP_(ULONG) CKoala::Release(void)
  125.     {
  126.     if (0L!=--m_cRef)
  127.         return m_cRef;
  128.  
  129.     if (NULL!=m_pfnDestroy)
  130.         (*m_pfnDestroy)();
  131.  
  132.     delete this;
  133.     return 0;
  134.     }
  135.  
  136.  
  137.  
  138.  
  139. //CHAPTER6MOD
  140. /*
  141.  * CKoala::AddConnection
  142.  *
  143.  * Purpose:
  144.  *  Informs the object that a strong connection has been made to it.
  145.  *
  146.  * Parameters:
  147.  *  dwConn          DWORD identifying the type of connection, taken
  148.  *                  from the EXTCONN enumeration.
  149.  *  dwReserved      DWORD reserved.  This is used inside OLE and
  150.  *                  should not be validated.
  151.  *
  152.  * Return Value:
  153.  *  DWORD           The number of connection counts on the
  154.  *                  object, used for debugging purposes only.
  155.  */
  156.  
  157. STDMETHODIMP_(DWORD) CKoala::AddConnection(DWORD dwConn
  158.     , DWORD dwReserved)
  159.     {
  160.     DWORD       dwRet;
  161.     TCHAR       szTemp[80];
  162.  
  163.     if (EXTCONN_STRONG & dwConn)
  164.         {
  165.         dwRet=++m_cStrong;
  166.         wsprintf(szTemp
  167.             , TEXT("AddConnection cStrong=%lu"), m_cStrong);
  168.         }
  169.  
  170.    #ifdef WIN32
  171.     if (EXTCONN_WEAK & dwConn)
  172.         {
  173.         dwRet=++m_cWeak;
  174.         wsprintf(szTemp
  175.             , TEXT("ReleaseConnection cWeak=%lu"), m_cWeak);
  176.         }
  177.    #endif
  178.  
  179.    #ifndef WIN32
  180.     ODS(szTemp);
  181.    #else
  182.     MessageBox(NULL, szTemp
  183.         , TEXT("EKoala3: CKoala::IExternalConnection"), MB_OK);
  184.    #endif
  185.  
  186.     return dwRet;
  187.     }
  188.  
  189.  
  190.  
  191. /*
  192.  * CKoala::ReleaseConnection
  193.  *
  194.  * Purpose:
  195.  *  Informs an object that a connection has been taken away from
  196.  *  it in which case the object may need to shut down.
  197.  *
  198.  * Parameters:
  199.  *  dwConn              DWORD identifying the type of connection,
  200.  *                      taken from the EXTCONN enumeration.
  201.  *  dwReserved          DWORD reserved.  This is used inside OLE and
  202.  *                      should not be validated.
  203.  *  dwRerved            DWORD reserved
  204.  *  fLastReleaseCloses  BOOL indicating if the last call to this
  205.  *                      function should close the object.
  206.  *
  207.  * Return Value:
  208.  *  DWORD           The number of remaining connection counts on
  209.  *                  the object, used for debugging purposes only.
  210.  */
  211.  
  212. STDMETHODIMP_(DWORD) CKoala::ReleaseConnection(DWORD dwConn
  213.     , DWORD dwReserved, BOOL fLastReleaseCloses)
  214.     {
  215.     DWORD       dwRet;
  216.     TCHAR       szTemp[80];
  217.  
  218.     if (EXTCONN_STRONG & dwConn)
  219.         {
  220.         /*
  221.          * Note:  We don't need to close ourselves when the last
  222.          * strong lock is removed; we're just implementing this
  223.          * interface for demonstration.
  224.          */
  225.  
  226.         dwRet=--m_cStrong;
  227.         wsprintf(szTemp
  228.             , TEXT("ReleaseConnection cStrong=%lu"), m_cStrong);
  229.         }
  230.  
  231.    #ifdef WIN32
  232.     if (EXTCONN_WEAK & dwConn)
  233.         {
  234.         dwRet=--m_cWeak;
  235.         wsprintf(szTemp
  236.             , TEXT("ReleaseConnection cWeak=%lu"), m_cWeak);
  237.         }
  238.    #endif
  239.  
  240.    #ifndef WIN32
  241.     ODS(szTemp);
  242.    #else
  243.     MessageBox(NULL, szTemp
  244.         , TEXT("EKoala3: CKoala::IExternalConnection"), MB_OK);
  245.    #endif
  246.  
  247.     return dwRet;
  248.     }
  249.  
  250.  
  251.  
  252.  
  253. ///IPersist implementation
  254.  
  255. /*
  256.  * CImpIPersist:CImpIPersist
  257.  * CImpIPersist::~CImpIPersist
  258.  *
  259.  * Constructor Parameters:
  260.  *  pObj            PCKoala pointing to the object we live in.
  261.  *  pUnkOuter       LPUNKNOWN of the controlling unknown.
  262.  */
  263.  
  264. CImpIPersist::CImpIPersist(PCKoala pObj, LPUNKNOWN pUnkOuter)
  265.     {
  266.     m_cRef=0;
  267.     m_pObj=pObj;
  268.     m_pUnkOuter=pUnkOuter;
  269.     return;
  270.     }
  271.  
  272. CImpIPersist::~CImpIPersist(void)
  273.     {
  274.     return;
  275.     }
  276.  
  277.  
  278.  
  279. /*
  280.  * CImpIPersist::QueryInterface
  281.  * CImpIPersist::AddRef
  282.  * CImpIPersist::Release
  283.  *
  284.  * Purpose:
  285.  *  Delegating IUnknown members for CImpIPersist.
  286.  */
  287.  
  288. STDMETHODIMP CImpIPersist::QueryInterface(REFIID riid
  289.     , LPVOID *ppv)
  290.     {
  291.     return m_pUnkOuter->QueryInterface(riid, ppv);
  292.     }
  293.  
  294. STDMETHODIMP_(ULONG) CImpIPersist::AddRef(void)
  295.     {
  296.     ++m_cRef;
  297.     return m_pUnkOuter->AddRef();
  298.     }
  299.  
  300. STDMETHODIMP_(ULONG) CImpIPersist::Release(void)
  301.     {
  302.     --m_cRef;
  303.     return m_pUnkOuter->Release();
  304.     }
  305.  
  306.  
  307.  
  308. /*
  309.  * CImpIPersist::GetClassID
  310.  *
  311.  * Purpose:
  312.  *  Returns the CLSID of the object represented by this interface.
  313.  *
  314.  * Parameters:
  315.  *  pClsID          LPCLSID in which to store our CLSID.
  316.  */
  317.  
  318. STDMETHODIMP CImpIPersist::GetClassID(LPCLSID pClsID)
  319.     {
  320.     *pClsID=CLSID_Koala;
  321.     return NOERROR;
  322.     }
  323.  
  324. //End CHAPTER6MOD
  325.