KOALA.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:7k
源码类别:

Windows编程

开发平台:

Visual C++

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