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.

319 lines
6.2 KiB

  1. //***************************************************************************
  2. //
  3. // File:
  4. //
  5. // Module: MS SNMP Provider
  6. //
  7. // Purpose:
  8. //
  9. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include <precomp.h>
  13. #include "csmir.h"
  14. #include "handles.h"
  15. #include "classfac.h"
  16. #include <textdef.h>
  17. #include <helper.h>
  18. #include "bstring.h"
  19. #include "evtcons.h"
  20. #ifdef ICECAP_PROFILE
  21. #include <icapexp.h>
  22. #endif
  23. /*
  24. * CSmirWbemEventConsumer::QueryInterface
  25. *
  26. * Purpose:
  27. * Manages the interfaces for this object which supports the
  28. * IUnknown interface.
  29. *
  30. * Parameters:
  31. * riid REFIID of the interface to return.
  32. * ppv PPVOID in which to store the pointer.
  33. *
  34. * Return Value:
  35. * SCODE NOERROR on success, E_NOINTERFACE if the
  36. * interface is not supported.
  37. */
  38. STDMETHODIMP CSmirWbemEventConsumer::QueryInterface(REFIID riid, PPVOID ppv)
  39. {
  40. SetStructuredExceptionHandler seh;
  41. try
  42. {
  43. //Always NULL the out-parameters
  44. *ppv=NULL;
  45. //are we being asked for an interface we support?
  46. if ((IID_IUnknown == riid) ||
  47. (IID_IWbemObjectSink == riid) ||
  48. (IID_ISMIR_WbemEventConsumer == riid) )
  49. {
  50. *ppv=this;
  51. ((LPUNKNOWN)*ppv)->AddRef();
  52. return NOERROR;
  53. }
  54. //we don't support the interface being asked for...
  55. return ResultFromScode(E_NOINTERFACE);
  56. }
  57. catch(Structured_Exception e_SE)
  58. {
  59. return E_UNEXPECTED;
  60. }
  61. catch(Heap_Exception e_HE)
  62. {
  63. return E_OUTOFMEMORY;
  64. }
  65. catch(...)
  66. {
  67. return E_UNEXPECTED;
  68. }
  69. }
  70. /*
  71. * CSmirWbemEventConsumer::AddRef
  72. * CSmirWbemEventConsumer::Release
  73. *
  74. * Reference counting members. When Release sees a zero count
  75. * the object destroys itself.
  76. */
  77. ULONG CSmirWbemEventConsumer::AddRef(void)
  78. {
  79. SetStructuredExceptionHandler seh;
  80. try
  81. {
  82. return InterlockedIncrement(&m_cRef);
  83. }
  84. catch(Structured_Exception e_SE)
  85. {
  86. return 0;
  87. }
  88. catch(Heap_Exception e_HE)
  89. {
  90. return 0;
  91. }
  92. catch(...)
  93. {
  94. return 0;
  95. }
  96. }
  97. ULONG CSmirWbemEventConsumer::Release(void)
  98. {
  99. SetStructuredExceptionHandler seh;
  100. try
  101. {
  102. long ret;
  103. if ( 0 != (ret = InterlockedDecrement(&m_cRef)) )
  104. {
  105. return ret;
  106. }
  107. delete this;
  108. return 0;
  109. }
  110. catch(Structured_Exception e_SE)
  111. {
  112. return 0;
  113. }
  114. catch(Heap_Exception e_HE)
  115. {
  116. return 0;
  117. }
  118. catch(...)
  119. {
  120. return 0;
  121. }
  122. }
  123. CSmirWbemEventConsumer::CSmirWbemEventConsumer(CSmir* psmir) : m_hEvents (NULL), m_Serv(NULL)
  124. {
  125. CSMIRClassFactory::objectsInProgress++;
  126. //init the reference count
  127. m_cRef=0;
  128. m_callbackThread = NULL;
  129. if (NULL == psmir)
  130. {
  131. m_hEvents = NULL;
  132. return;
  133. }
  134. //Create the event
  135. m_hEvents = new HANDLE[SMIR_EVT_COUNT];
  136. for (int x = 0; x < SMIR_EVT_COUNT; x++)
  137. {
  138. m_hEvents[x] = NULL;
  139. }
  140. m_hEvents[SMIR_CHANGE_EVT] = CreateEvent(NULL, FALSE, FALSE, NULL);
  141. m_hEvents[SMIR_THREAD_EVT] = CreateEvent(NULL, FALSE, TRUE, NULL);
  142. }
  143. CSmirWbemEventConsumer :: ~CSmirWbemEventConsumer()
  144. {
  145. //close the change event handle
  146. if(NULL != m_hEvents)
  147. {
  148. if ((NULL != m_callbackThread) && (NULL != m_hEvents[SMIR_THREAD_EVT]) &&
  149. (WAIT_OBJECT_0 != WaitForSingleObject(m_hEvents[SMIR_THREAD_EVT], 0)) )
  150. {
  151. m_callbackThread->Release();
  152. }
  153. for (ULONG i = 0; i < SMIR_EVT_COUNT; i++)
  154. {
  155. if (NULL != m_hEvents[i])
  156. {
  157. CloseHandle(m_hEvents[i]);
  158. }
  159. }
  160. delete [] m_hEvents;
  161. }
  162. if (NULL != m_Serv)
  163. {
  164. m_Serv->Release();
  165. m_Serv = NULL;
  166. }
  167. CSMIRClassFactory::objectsInProgress--;
  168. }
  169. HRESULT CSmirWbemEventConsumer::Indicate(IN long lObjectCount, IN IWbemClassObject **ppObjArray)
  170. {
  171. SetStructuredExceptionHandler seh;
  172. try
  173. {
  174. if ((NULL != m_hEvents) && (NULL != m_hEvents[SMIR_THREAD_EVT]))
  175. {
  176. //if thread is dead start a thread to watch for further change events
  177. if (WAIT_OBJECT_0 == WaitForSingleObject(m_hEvents[SMIR_THREAD_EVT], 0))
  178. {
  179. m_callbackThread = new CNotifyThread(m_hEvents, SMIR_EVT_COUNT);
  180. m_callbackThread->AddRef();
  181. DWORD dwThreadHandle = m_callbackThread->Start();
  182. if (WBEM_E_FAILED == dwThreadHandle)
  183. {
  184. m_callbackThread->Release();
  185. m_callbackThread = NULL;
  186. }
  187. }
  188. else
  189. {
  190. //set change event to restart timer
  191. SetEvent(m_hEvents[SMIR_CHANGE_EVT]);
  192. }
  193. }
  194. return NOERROR;
  195. }
  196. catch(Structured_Exception e_SE)
  197. {
  198. return WBEM_E_UNEXPECTED;
  199. }
  200. catch(Heap_Exception e_HE)
  201. {
  202. return WBEM_E_OUT_OF_MEMORY;
  203. }
  204. catch(...)
  205. {
  206. return WBEM_E_UNEXPECTED;
  207. }
  208. }
  209. HRESULT CSmirWbemEventConsumer::SetStatus(IN long lFlags, IN long lParam, IN BSTR strParam,
  210. IN IWbemClassObject *pObjParam)
  211. {
  212. return NOERROR;
  213. }
  214. HRESULT CSmirWbemEventConsumer::Register(CSmir* psmir)
  215. {
  216. if (NULL == m_hEvents)
  217. {
  218. return WBEM_E_FAILED;
  219. }
  220. IWbemServices * moServ = NULL ;
  221. IWbemContext *moContext = NULL ;
  222. HRESULT result= CSmirAccess :: GetContext (psmir , &moContext);
  223. result = CSmirAccess :: Open(psmir,&moServ);
  224. if ((S_FALSE==result)||(NULL == moServ))
  225. {
  226. if ( moContext )
  227. moContext->Release () ;
  228. //we have a problem the SMIR is not there and cannot be created
  229. return WBEM_E_FAILED;
  230. }
  231. BSTR t_bstrQueryType = SysAllocString(FILTER_QUERYTYPE_VAL);
  232. BSTR t_bstrQuery = SysAllocString(FILTER_QUERY_VAL);
  233. result = moServ->ExecNotificationQueryAsync(
  234. t_bstrQueryType, // [in] BSTR QueryLanguage,
  235. t_bstrQuery, // [in] BSTR Query,
  236. 0, // [in] long lFlags,
  237. moContext, // [in] IWbemContext *pCtx,
  238. this); // [in] IWbemObjectSink *pResponseHandler
  239. SysFreeString(t_bstrQueryType);
  240. SysFreeString(t_bstrQuery);
  241. if ( moContext )
  242. moContext->Release () ;
  243. //keep this around for unregister...
  244. m_Serv = moServ;
  245. return result;
  246. }
  247. HRESULT CSmirWbemEventConsumer::GetUnRegisterParams(IWbemServices** ppServ)
  248. {
  249. HRESULT retVal = WBEM_E_FAILED;
  250. if (m_Serv)
  251. {
  252. *ppServ = m_Serv;
  253. m_Serv = NULL;
  254. retVal = S_OK;
  255. }
  256. return retVal;
  257. }
  258. HRESULT CSmirWbemEventConsumer::UnRegister(CSmir* psmir, IWbemServices* pServ)
  259. {
  260. if (NULL == m_hEvents)
  261. {
  262. return WBEM_E_FAILED;
  263. }
  264. return pServ->CancelAsyncCall(this);
  265. }