bcw.cpp
上传用户:xhy777
上传日期:2007-02-14
资源大小:24088k
文件大小:3k
源码类别:

系统编程

开发平台:

Visual C++

  1. /****************************************************************************
  2. bcw.cpp
  3.   Owner: Srinik
  4.   Copyright (c) 1995 Microsoft Corporation
  5.   
  6.     This file contains code for BCW class which implements wrappers for IBindCtx
  7.     and IRunningObjectTable. We use this object to trick the moniker binding
  8.     code to create a new instance of the object (that the moniker is
  9.     referring to) instead connecting to already running instance.
  10. ****************************************************************************/
  11. // #include "hlmain.h"
  12. #include "bcw.h"
  13. ASSERTDATA
  14. /****************************************************************************
  15. Implementation of BCW methods.
  16. ****************************************************************************/
  17. BCW::BCW(IBindCtx * pibc)
  18. {
  19.     m_pibc = pibc;
  20.     pibc->AddRef();
  21.     m_cObjRef = 1;
  22.     DllAddRef();
  23. }
  24. BCW::~BCW()
  25. {
  26.     m_pibc->Release();
  27.     DllRelease();
  28. }
  29. IBindCtx * BCW::Create(IBindCtx * pibc)
  30. {
  31.     BCW * pbcw = new BCW(pibc);
  32.     
  33.     if (pbcw == NULL)
  34.         return NULL;
  35.     
  36.     if (! pbcw->m_ROT.FInitROTPointer())
  37.     {
  38.         delete pbcw;
  39.         return NULL;
  40.     }
  41.     
  42.     return pbcw;
  43. }
  44. STDMETHODIMP BCW::QueryInterface(REFIID riid, void **ppvObj)
  45. {   
  46.     if (ppvObj == NULL)
  47.         return E_INVALIDARG;
  48.     
  49.     if (riid == IID_IUnknown || riid == IID_IBindCtx)
  50.     {
  51.         *ppvObj = this;
  52.     }
  53.     else
  54.     {
  55.         *ppvObj = NULL;
  56.         return E_NOINTERFACE;
  57.     }
  58.     
  59.     ((IUnknown *) *ppvObj)->AddRef();
  60.     return NOERROR; 
  61. }
  62. STDMETHODIMP_(ULONG) BCW::AddRef(void)
  63. {   
  64.     return ++m_cObjRef;
  65. }
  66. STDMETHODIMP_(ULONG) BCW::Release(void)
  67. {
  68. /* Decrement refcount, destroy object if refcount goes to zero.
  69.     Return the new refcount. */
  70.     if (!(--m_cObjRef))
  71.     {
  72.         delete this;
  73.         return 0;
  74.     }
  75.     
  76.     return m_cObjRef;
  77. }
  78. /****************************************************************************
  79. Implementation of BCW_ROT methods.
  80. ****************************************************************************/
  81. /****************************************************************************
  82. BCW_ROT is the IRunningObjectTable imlementation of BCW_ROT.
  83. ****************************************************************************/
  84. BCW_ROT::BCW_ROT()
  85. {
  86.     Debug(m_cRef = 0);
  87.     m_piROT = NULL; 
  88. }
  89. BCW_ROT::~BCW_ROT()
  90. {   
  91.     if (m_piROT)
  92.         m_piROT->Release();
  93. }
  94. BOOL_PTR BCW_ROT::FInitROTPointer(void)
  95. {
  96.     if (m_piROT == NULL)
  97.     {
  98.         if (GetRunningObjectTable(NULL/*reserved*/, &m_piROT) == NOERROR)
  99.             m_piROT->AddRef();
  100.     }
  101.     
  102.     return (BOOL_PTR) (m_piROT);
  103. }
  104. inline BCW * BCW_ROT::PBCW()
  105. {
  106.     return BACK_POINTER(this, m_ROT, BCW);
  107. }
  108. STDMETHODIMP BCW_ROT::QueryInterface(REFIID riid, void **ppvObj)
  109. {
  110.     if (riid == IID_IUnknown || riid == IID_IRunningObjectTable)
  111.     {
  112.         *ppvObj = this;
  113.     }
  114.     else
  115.     {
  116.         *ppvObj = NULL;
  117.         return E_NOINTERFACE;
  118.     }
  119.     
  120.     ((IUnknown *) *ppvObj)->AddRef();
  121.     return NOERROR;
  122. }
  123. STDMETHODIMP_(ULONG) BCW_ROT::AddRef(void)
  124. {
  125.     return PBCW()->AddRef();
  126. }
  127. STDMETHODIMP_(ULONG) BCW_ROT::Release(void)
  128. {
  129.     return PBCW()->Release();
  130. }