Source code of Windows XP (NT5)
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.

280 lines
6.1 KiB

  1. /*++
  2. Copyright (C) 1998-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. //***************************************************************************
  8. //
  9. // EVCONS.CPP
  10. //
  11. // Test event consumer shell.
  12. //
  13. // raymcc 4-Jun-98
  14. //
  15. //***************************************************************************
  16. #include <windows.h>
  17. #include <stdio.h>
  18. #include <wbemidl.h>
  19. #include "oahelp.inl"
  20. #include "evcons.h"
  21. //***************************************************************************
  22. //
  23. //***************************************************************************
  24. CMyEventConsumer::CMyEventConsumer()
  25. {
  26. m_cRef = 0;
  27. m_pConsumer1 = new CMySink("Log1");
  28. m_pConsumer1->AddRef();
  29. m_pConsumer2 = new CMySink("Log2");
  30. m_pConsumer2->AddRef();
  31. }
  32. //***************************************************************************
  33. //
  34. //***************************************************************************
  35. CMyEventConsumer::~CMyEventConsumer()
  36. {
  37. m_pConsumer1->Release();
  38. m_pConsumer2->Release();
  39. }
  40. //***************************************************************************
  41. //
  42. //***************************************************************************
  43. HRESULT CMyEventConsumer::FindConsumer(
  44. /* [in] */ IWbemClassObject __RPC_FAR *pLogicalConsumer,
  45. /* [out] */ IWbemUnboundObjectSink __RPC_FAR *__RPC_FAR *ppConsumer
  46. )
  47. {
  48. // Examine the logical consumer to see which one it is.
  49. // ====================================================
  50. VARIANT v;
  51. VariantInit(&v);
  52. HRESULT hRes = WBEM_E_NOT_FOUND;
  53. *ppConsumer = 0;
  54. pLogicalConsumer->Get(CBSTR(L"Name"), 0, &v, 0, 0);
  55. // Decide which of the two logical consumers to send back.
  56. // =======================================================
  57. if (_wcsicmp(V_BSTR(&v), L"Consumer1") == 0)
  58. {
  59. m_pConsumer1->AddRef();
  60. *ppConsumer = m_pConsumer1;
  61. hRes = WBEM_S_NO_ERROR;
  62. }
  63. else if (_wcsicmp(V_BSTR(&v), L"Consumer2") == 0)
  64. {
  65. m_pConsumer1->AddRef();
  66. *ppConsumer = m_pConsumer2;
  67. hRes =WBEM_S_NO_ERROR;
  68. }
  69. VariantClear(&v);
  70. return hRes;
  71. }
  72. //***************************************************************************
  73. //
  74. //***************************************************************************
  75. // ok
  76. HRESULT CMyEventConsumer::Initialize(
  77. /* [in] */ LPWSTR pszUser,
  78. /* [in] */ LONG lFlags,
  79. /* [in] */ LPWSTR pszNamespace,
  80. /* [in] */ LPWSTR pszLocale,
  81. /* [in] */ IWbemServices __RPC_FAR *pNamespace,
  82. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  83. /* [in] */ IWbemProviderInitSink __RPC_FAR *pInitSink
  84. )
  85. {
  86. pInitSink->SetStatus(0, WBEM_S_INITIALIZED);
  87. return WBEM_S_NO_ERROR;
  88. }
  89. //***************************************************************************
  90. //
  91. //***************************************************************************
  92. // ok
  93. HRESULT CMyEventConsumer::QueryInterface(REFIID riid, LPVOID * ppv)
  94. {
  95. *ppv = 0;
  96. if (IID_IUnknown==riid || IID_IWbemEventConsumerProvider==riid)
  97. {
  98. *ppv = (IWbemEventConsumerProvider *) this;
  99. AddRef();
  100. return NOERROR;
  101. }
  102. if (IID_IWbemProviderInit==riid)
  103. {
  104. *ppv = (IWbemProviderInit *) this;
  105. AddRef();
  106. return NOERROR;
  107. }
  108. return ResultFromScode(E_NOINTERFACE);
  109. }
  110. //***************************************************************************
  111. //
  112. //***************************************************************************
  113. // ok
  114. ULONG CMyEventConsumer::AddRef()
  115. {
  116. return ++m_cRef;
  117. }
  118. //***************************************************************************
  119. //
  120. //***************************************************************************
  121. // ok
  122. ULONG CMyEventConsumer::Release()
  123. {
  124. if (0 != --m_cRef)
  125. return m_cRef;
  126. delete this;
  127. return 0;
  128. }
  129. //***************************************************************************
  130. //
  131. //***************************************************************************
  132. // ok
  133. HRESULT CMySink::IndicateToConsumer(
  134. /* [in] */ IWbemClassObject __RPC_FAR *pLogicalConsumer,
  135. /* [in] */ long lNumObjects,
  136. /* [size_is][in] */ IWbemClassObject __RPC_FAR *__RPC_FAR *apObjects
  137. )
  138. {
  139. FILE *f = fopen(m_pszLogFile, "at");
  140. for (long i = 0; i < lNumObjects; i++)
  141. {
  142. IWbemClassObject *pObj = apObjects[i];
  143. VARIANT v;
  144. VariantInit(&v);
  145. // Verify the events are of the class we expect.
  146. // =============================================
  147. pObj->Get(CBSTR(L"__CLASS"), 0, &v, 0, 0);
  148. if (_wcsicmp(V_BSTR(&v), L"MyEvent") != 0)
  149. {
  150. VariantClear(&v);
  151. continue;
  152. }
  153. VariantClear(&v);
  154. // Get the properties and log them to a file.
  155. // ==========================================
  156. VARIANT v2;
  157. VariantInit(&v2);
  158. pObj->Get(CBSTR(L"Name"), 0, &v, 0, 0);
  159. pObj->Get(CBSTR(L"Value"), 0, &v2, 0, 0);
  160. fprintf(f, "Event %S value = %d\n", V_BSTR(&v), V_I4(&v2));
  161. VariantClear(&v);
  162. VariantClear(&v2);
  163. }
  164. fclose(f);
  165. return WBEM_S_NO_ERROR;
  166. }
  167. //***************************************************************************
  168. //
  169. //***************************************************************************
  170. // ok
  171. STDMETHODIMP CMySink::QueryInterface(REFIID riid, LPVOID * ppv)
  172. {
  173. *ppv = 0;
  174. if (IID_IUnknown==riid || IID_IWbemUnboundObjectSink==riid)
  175. {
  176. *ppv = (IWbemUnboundObjectSink *) this;
  177. AddRef();
  178. return NOERROR;
  179. }
  180. return ResultFromScode(E_NOINTERFACE);
  181. }
  182. //***************************************************************************
  183. //
  184. //***************************************************************************
  185. // ok
  186. ULONG CMySink::AddRef()
  187. {
  188. return ++m_cRef;
  189. }
  190. //***************************************************************************
  191. //
  192. //***************************************************************************
  193. // ok
  194. ULONG CMySink::Release()
  195. {
  196. if (0 != --m_cRef)
  197. return m_cRef;
  198. delete this;
  199. return 0;
  200. }