Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.0 KiB

  1. /*****************************************************************************
  2. * (C) COPYRIGHT MICROSOFT CORPORATION, 2002
  3. *
  4. * AUTHOR: ByronC
  5. *
  6. * DATE: 4/1/2002
  7. *
  8. * @doc INTERNAL
  9. *
  10. * @module RegistrationCookie.cpp - Declarations for <c RegistrationCookie> |
  11. *
  12. * This file contains the implementation for the <c RegistrationCookie> class.
  13. *
  14. *****************************************************************************/
  15. #include "cplusinc.h"
  16. #include "coredbg.h"
  17. /*****************************************************************************
  18. * @doc INTERNAL
  19. *
  20. * @mfunc | RegistrationCookie | RegistrationCookie |
  21. *
  22. * We initialize all member variables. In general, this sets the values to 0,
  23. * except:
  24. * <nl><md RegistrationCookie::m_ulSig> is set to be RegistrationCookie_INIT_SIG.
  25. * <nl><md RegistrationCookie::m_cRef> is set to be 1.
  26. *
  27. * We also AddRef <md RegistrationCookie::m_pClientEventRegistration> if it is not NULL.
  28. *
  29. *****************************************************************************/
  30. RegistrationCookie::RegistrationCookie(
  31. WiaEventReceiver *pWiaEventReceiver,
  32. ClientEventRegistrationInfo *pClientEventRegistration) :
  33. m_ulSig(RegistrationCookie_INIT_SIG),
  34. m_cRef(1),
  35. m_pWiaEventReceiver(pWiaEventReceiver),
  36. m_pClientEventRegistration(pClientEventRegistration)
  37. {
  38. DBG_FN(RegistrationCookie constructor);
  39. if (m_pClientEventRegistration)
  40. {
  41. m_pClientEventRegistration->AddRef();
  42. }
  43. }
  44. /*****************************************************************************
  45. * @doc INTERNAL
  46. *
  47. * @mfunc | RegistrationCookie | ~RegistrationCookie |
  48. *
  49. * Do any cleanup that is not already done. Specifically we:
  50. * <nl>- Request <md RegistrationCookie::m_pWiaEventReceiver> to unregister
  51. * <md RegistrationCookie::m_pClientEventRegistration>.
  52. * <nl>- Release our ref count on <md RegistrationCookie::m_pClientEventRegistration>.
  53. *
  54. * Also:
  55. * <nl><md RegistrationCookie::m_ulSig> is set to be RegistrationCookie_DEL_SIG.
  56. *
  57. *****************************************************************************/
  58. RegistrationCookie::~RegistrationCookie()
  59. {
  60. DBG_FN(~RegistrationCookie);
  61. m_ulSig = RegistrationCookie_DEL_SIG;
  62. m_cRef = 0;
  63. if (m_pClientEventRegistration)
  64. {
  65. if (m_pWiaEventReceiver)
  66. {
  67. //
  68. // Change this registration to an unregistration before sending off the
  69. // request.
  70. //
  71. m_pClientEventRegistration->setToUnregister();
  72. HRESULT hr = m_pWiaEventReceiver->SendRegisterUnregisterInfo(m_pClientEventRegistration);
  73. if (FAILED(hr))
  74. {
  75. DBG_ERR(("Failed to unregister event notification"));
  76. }
  77. }
  78. m_pClientEventRegistration->Release();
  79. m_pClientEventRegistration = NULL;
  80. }
  81. }
  82. /*****************************************************************************
  83. * @doc INTERNAL
  84. *
  85. * @mfunc HRESULT | RegistrationCookie | QueryInterface |
  86. *
  87. * Typical QueryInterface. We only respond to IID_IUnknown.
  88. *
  89. * @rvalue S_OK |
  90. * The method succeeded. This class has been AddRef'd.
  91. * @rvalue E_NOINTERFACE |
  92. * We do not supoort that interface.
  93. *****************************************************************************/
  94. HRESULT _stdcall RegistrationCookie::QueryInterface(
  95. const IID &iid,
  96. void **ppv)
  97. {
  98. HRESULT hr = S_OK;
  99. *ppv = NULL;
  100. if (iid == IID_IUnknown)
  101. {
  102. *ppv = (IUnknown*) this;
  103. hr = S_OK;
  104. }
  105. else
  106. {
  107. hr = E_NOINTERFACE;
  108. }
  109. if (SUCCEEDED(hr))
  110. {
  111. AddRef();
  112. }
  113. return hr;
  114. }
  115. /*****************************************************************************
  116. * @doc INTERNAL
  117. *
  118. * @mfunc ULONG | RegistrationCookie | AddRef |
  119. *
  120. * Increments this object's ref count. We should always AddRef when handing
  121. * out a pointer to this object.
  122. *
  123. * @rvalue Count |
  124. * The reference count after the count has been incremented.
  125. *****************************************************************************/
  126. ULONG __stdcall RegistrationCookie::AddRef()
  127. {
  128. InterlockedIncrement((long*) &m_cRef);
  129. return m_cRef;
  130. }
  131. /*****************************************************************************
  132. * @doc INTERNAL
  133. *
  134. * @mfunc ULONG | RegistrationCookie | Release |
  135. *
  136. * Decrement this object's ref count. We should always Release when finished
  137. * with a pointer to this object.
  138. *
  139. * @rvalue Count |
  140. * The reference count after the count has been decremented.
  141. *****************************************************************************/
  142. ULONG __stdcall RegistrationCookie::Release()
  143. {
  144. ULONG ulRefCount = m_cRef - 1;
  145. if (InterlockedDecrement((long*) &m_cRef) == 0)
  146. {
  147. delete this;
  148. return 0;
  149. }
  150. return ulRefCount;
  151. }