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.

242 lines
4.1 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1997 - 1999
  3. Module Name:
  4. cfacnet.cxx
  5. Abstract:
  6. Implements the Class Factory for the SENS ISensNetwork Subscriber.
  7. Author:
  8. Gopal Parupudi <GopalP>
  9. [Notes:]
  10. optional-notes
  11. Revision History:
  12. GopalP 11/17/1997 Start.
  13. --*/
  14. #include <common.hxx>
  15. #include <ole2.h>
  16. #include <sensevts.h>
  17. #include "sinkcomn.hxx"
  18. #include "cfacnet.hxx"
  19. #include "cimpnet.hxx"
  20. #include "cimplogn.hxx"
  21. //
  22. // Global counts for the number of objects in the server and the number of
  23. // locks.
  24. //
  25. extern DWORD gTid;
  26. extern HANDLE ghEvent;
  27. ULONG g_cObj = 0L;
  28. ULONG g_cLock = 0L;
  29. //
  30. // Constructor and Destructor
  31. //
  32. CISensNetworkCF::CISensNetworkCF(
  33. void
  34. ) : m_cRef(1L)
  35. {
  36. }
  37. CISensNetworkCF::~CISensNetworkCF(
  38. void
  39. )
  40. {
  41. // Empty Destructor.
  42. }
  43. //
  44. // QI
  45. //
  46. STDMETHODIMP
  47. CISensNetworkCF::QueryInterface(
  48. REFIID riid,
  49. LPVOID *ppv
  50. )
  51. {
  52. HRESULT hr = S_OK;
  53. *ppv = NULL; // To handle failure cases
  54. // IUnknown or IClassFactory
  55. if (IsEqualIID(riid, IID_IUnknown) ||
  56. IsEqualIID(riid, IID_IClassFactory))
  57. {
  58. *ppv = (LPUNKNOWN) this;
  59. }
  60. else
  61. {
  62. hr = E_NOINTERFACE;
  63. }
  64. if (NULL != *ppv)
  65. {
  66. ((LPUNKNOWN)*ppv)->AddRef();
  67. }
  68. return hr;
  69. }
  70. //
  71. // AddRef
  72. //
  73. STDMETHODIMP_(ULONG)
  74. CISensNetworkCF::AddRef(
  75. void
  76. )
  77. {
  78. return InterlockedIncrement((PLONG) &m_cRef);
  79. }
  80. //
  81. // Release
  82. //
  83. STDMETHODIMP_(ULONG)
  84. CISensNetworkCF::Release(
  85. void
  86. )
  87. {
  88. ULONG cRefT;
  89. cRefT = InterlockedDecrement((PLONG) &m_cRef);
  90. if (0 == m_cRef)
  91. {
  92. // Invoke the callback function.
  93. ObjectDestroyed();
  94. delete this;
  95. }
  96. return cRefT;
  97. }
  98. //
  99. // CreateInstance
  100. //
  101. STDMETHODIMP
  102. CISensNetworkCF::CreateInstance(
  103. LPUNKNOWN pUnkOuter,
  104. REFIID riid,
  105. LPVOID *ppvObj
  106. )
  107. {
  108. LPCIMPISENSNETWORK pObjNet;
  109. HRESULT hr;
  110. DebugTraceGuid("CISensNetworkCF::CreateInstance()", riid);
  111. hr = E_OUTOFMEMORY;
  112. *ppvObj = NULL;
  113. pObjNet = NULL;
  114. //
  115. // Return the appropriate interface pointer.
  116. //
  117. if (IsEqualIID(riid, IID_ISensNetwork) ||
  118. IsEqualIID(riid, IID_IUnknown))
  119. {
  120. SensPrintA(SENS_INFO, ("\t| ClassFactory::CreateInstance(ISensNetwork)\n"));
  121. pObjNet = new CImpISensNetwork(ObjectDestroyed);
  122. if (NULL != pObjNet)
  123. {
  124. hr = pObjNet->QueryInterface(riid, ppvObj);
  125. SensPrintA(SENS_INFO, ("\t| QI on CImpISensNetwork returned 0x%x\n", hr));
  126. }
  127. }
  128. else
  129. {
  130. hr = E_NOINTERFACE;
  131. }
  132. if (NULL != *ppvObj)
  133. {
  134. InterlockedIncrement((PLONG) &g_cObj);
  135. }
  136. SensPrintA(SENS_INFO, ("\t| Returning 0x%x from CF:CreateInstance\n", hr));
  137. return hr;
  138. }
  139. //
  140. // LockServer
  141. //
  142. STDMETHODIMP
  143. CISensNetworkCF::LockServer(
  144. BOOL fLock
  145. )
  146. {
  147. if (fLock)
  148. {
  149. InterlockedIncrement((PLONG) &g_cLock);
  150. }
  151. else
  152. {
  153. InterlockedDecrement((PLONG) &g_cLock);
  154. InterlockedIncrement((PLONG) &g_cObj);
  155. ObjectDestroyed(); // this does a --g_cObj
  156. }
  157. return NOERROR;
  158. }
  159. //
  160. // ObjectDestroyed
  161. //
  162. void FAR PASCAL
  163. ObjectDestroyed(
  164. void
  165. )
  166. {
  167. BOOL bSuccess = FALSE;
  168. if ((0 == InterlockedDecrement((PLONG) &g_cObj)) &&
  169. (0 == g_cLock))
  170. {
  171. SensPrintA(SENS_INFO, ("\t| ObjectDestroyed: g_cObj = %d and g_cLock = %d\n", g_cObj, g_cLock));
  172. SensPrintA(SENS_INFO, ("\t| Shutting down the app. Calling SetEvent()\n"));
  173. Sleep(2000);
  174. SetEvent(ghEvent);
  175. /*
  176. SensPrintA(SENS_INFO, ("\t| Shutting down the app. Calling PostThreadMessage()\n"));
  177. bSuccess = PostThreadMessage(gTid, WM_QUIT, 0, 0);
  178. if (bSuccess == FALSE)
  179. {
  180. SensPrintA(SENS_ERR, ("\t| PostThreadMessage(Tid = %d) failed!\n"));
  181. }
  182. */
  183. }
  184. }