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.

192 lines
4.4 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright: Microsoft Corp. 1997-1999. All rights reserved
  4. //
  5. /////////////////////////////////////////////////////////////////////////////
  6. // Logs.cpp : Implementation of CLogs
  7. #include "stdafx.h"
  8. #include "Evntutl.h"
  9. #include "Logs.h"
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CLogs
  12. STDMETHODIMP CLogs::InterfaceSupportsErrorInfo(REFIID riid)
  13. {
  14. static const IID* arr[] =
  15. {
  16. &IID_ILogs
  17. };
  18. for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
  19. {
  20. if (InlineIsEqualGUID(*arr[i],riid))
  21. return S_OK;
  22. }
  23. return S_FALSE;
  24. }
  25. /*
  26. Function: get_Count
  27. Inputs: empty long
  28. Outputs: number event logs available
  29. Purpose: Allows user to determine the number of DEFAULT EventLogs
  30. Custom EventLogs are not visible in the count
  31. */
  32. STDMETHODIMP CLogs::get_Count(long *pVal)
  33. {
  34. *pVal = m_Count;
  35. return S_OK;
  36. }
  37. /*
  38. Function: get_NewEnum
  39. Inputs: empty IUnknown pointer
  40. Outputs: IEnumVariant object filled with names of default EventLogs
  41. Purpose: Allows user to use For Each syntax to do operations on all
  42. DEFAULT EventLogs
  43. */
  44. STDMETHODIMP CLogs::get__NewEnum(LPUNKNOWN *pVal)
  45. {
  46. HRESULT hr = S_OK;
  47. if (NULL == pVal) return E_POINTER;
  48. *pVal = NULL;
  49. EnumVar* pEVar = new EnumVar;
  50. hr = pEVar->Init(&m_pVector[0], &m_pVector[m_Count], NULL, AtlFlagCopy);
  51. if (SUCCEEDED(hr))
  52. hr = pEVar->QueryInterface(IID_IEnumVARIANT, (void**) pVal);
  53. if FAILED(hr)
  54. if (pEVar) delete pEVar;
  55. return hr;
  56. }
  57. /*
  58. Function: get_Item
  59. Inputs: Valid Index (containing integer for default or BSTR for Default and Custom), empty Variant
  60. Outputs: variant dispatch pointer to a Log object
  61. Purpose: Allows user to access individual EventLogs by name or default EventLogs by number
  62. */
  63. STDMETHODIMP CLogs::get_Item(VARIANT Index, VARIANT *pVal)
  64. {
  65. HRESULT hr = S_OK;
  66. CComObject<CLog>* pLog;
  67. ILog* ptLog;
  68. LPDISPATCH pDisp = NULL;
  69. _bstr_t bstrCurrentName;
  70. _bstr_t bstrCheck;
  71. CComBSTR bstrTemp;
  72. bool bfound = false;
  73. unsigned int i = 0;
  74. if (NULL == pVal) return E_POINTER;
  75. VariantInit(pVal);
  76. pVal->vt = VT_UNKNOWN;
  77. pVal->punkVal = NULL;
  78. switch(Index.vt)
  79. {
  80. case VT_I4 :
  81. case VT_UI2:
  82. case VT_UINT:
  83. case VT_INT:
  84. {
  85. if ((Index.iVal > 0) && (UINT(Index.iVal) < m_Count + 1))
  86. VariantCopy(pVal, &m_pVector[Index.iVal - 1]);
  87. else hr = E_INVALIDARG;
  88. }
  89. break;
  90. case VT_BSTR :
  91. {
  92. if (!Index.bstrVal)
  93. hr = E_INVALIDARG;
  94. else
  95. {
  96. m_btCurrentLogName = Index.bstrVal;
  97. // This loop should check the existing VariantArray for a log with name = Index
  98. // it completes when the item is found or all default logs have been checked
  99. while ((i<m_Count) && (false == bfound))
  100. {
  101. hr = m_pVector[i].pdispVal->QueryInterface(IID_ILog, (void**) &ptLog);
  102. hr = ptLog->get_Name(&bstrTemp);
  103. bstrCurrentName = bstrTemp;
  104. if (bstrCurrentName == m_btCurrentLogName)
  105. {
  106. VariantCopy(pVal, &m_pVector[i]);
  107. bfound = true;
  108. }
  109. ptLog->Release();
  110. i++;
  111. }
  112. if (false == bfound)
  113. {
  114. hr = CComObject<CLog>::CreateInstance(&pLog);
  115. bstrCurrentName = Index.bstrVal;
  116. pLog->m_Name = bstrCurrentName;
  117. pLog->m_ServerName = m_ServerName;
  118. hr = pLog->QueryInterface(IID_IDispatch, (void**)&pDisp);
  119. pLog->AddRef();
  120. pVal->vt = VT_DISPATCH;
  121. pVal->pdispVal = pDisp;
  122. }
  123. }
  124. }
  125. break;
  126. default:
  127. hr = E_INVALIDARG;
  128. break;
  129. }
  130. return hr;
  131. }
  132. /*
  133. Function: Init
  134. Inputs: none
  135. Outputs: HRESULT indicating what error if any occured
  136. Purpose: Prepares a variant array filled with Log objects for 3 default logs.
  137. */
  138. HRESULT CLogs::Init()
  139. {
  140. HRESULT hr = S_OK;
  141. UINT i;
  142. // Default Logs: "Application" "Security" "System"
  143. static wchar_t* LogNames[] = {L"Application", L"Security", L"System"};
  144. m_Count = 3;
  145. if (m_pVector !=NULL) delete [] m_pVector;
  146. m_pVector = new CComVariant[m_Count];
  147. CComObject<CLog>* pLog;
  148. LPDISPATCH pDisp = NULL;
  149. for (i = 0; i < m_Count; i++)
  150. {
  151. // create a CLog object
  152. hr = CComObject<CLog>::CreateInstance(&pLog);
  153. if (SUCCEEDED(hr))
  154. {
  155. pLog->m_Name = LogNames[i];
  156. pLog->m_ServerName = m_ServerName.copy();
  157. // get IDispatch pointer
  158. hr = pLog->QueryInterface(IID_IDispatch, (void**)&pDisp);
  159. if (SUCCEEDED(hr))
  160. {
  161. // create a variant reference and set the type of object
  162. CComVariant& var = m_pVector[i];
  163. var.vt = VT_DISPATCH;
  164. var.pdispVal = pDisp;
  165. }
  166. }
  167. }
  168. return hr;
  169. }