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.

206 lines
9.5 KiB

  1. //=============================================================================
  2. // This file describes classes used by MSInfo to abstract its access to WMI
  3. // classes and enumerations. This abstraction allows for displaying data
  4. // from live WMI or from a saved XML file.
  5. //=============================================================================
  6. #pragma once
  7. #include "resource.h"
  8. //-----------------------------------------------------------------------------
  9. // MSInfo has a couple of specific errors - one where there is no property
  10. // by the specified name, and when where the value returned for the requested
  11. // property is NULL.
  12. //-----------------------------------------------------------------------------
  13. typedef enum tag_MSINFOSTATUS
  14. {
  15. MSINFO_NO_ERROR = 0,
  16. E_MSINFO_NOVALUE = 0x80043001,
  17. E_MSINFO_NOPROPERTY = 0x80043002
  18. } MSINFOSTATUS;
  19. //-----------------------------------------------------------------------------
  20. // The CWMIObject abstract base class encapsulates a WMI object, which may
  21. // in reality be a live WMI object, or an object recreated from the XML
  22. // storage of an object.
  23. //-----------------------------------------------------------------------------
  24. class CWMIObject
  25. {
  26. public:
  27. CWMIObject() {};
  28. virtual ~CWMIObject() {};
  29. // The following methods return information about a property of this object.
  30. //
  31. // S_OK implies success
  32. // E_MSINFO_NOPROPERTY means the named property doesn't exist
  33. // E_MSINFO_NOVALUE means the property exists, but is empty
  34. virtual HRESULT GetValue(LPCTSTR szProperty, VARIANT * pvarValue) = 0;
  35. virtual HRESULT GetValueString(LPCTSTR szProperty, CString * pstrValue) = 0;
  36. virtual HRESULT GetValueDWORD(LPCTSTR szProperty, DWORD * pdwValue) = 0;
  37. virtual HRESULT GetValueTime(LPCTSTR szProperty, SYSTEMTIME * psystimeValue) = 0;
  38. virtual HRESULT GetValueDoubleFloat(LPCTSTR szProperty, double * pdblValue) = 0;
  39. virtual HRESULT GetValueValueMap(LPCTSTR szProperty, CString * pstrValue) = 0;
  40. // Some shortcuts and helper functions.
  41. virtual CString GetString(LPCTSTR szProperty)
  42. {
  43. CString strReturn;
  44. if (SUCCEEDED(GetValueString(szProperty, &strReturn)))
  45. return strReturn;
  46. else
  47. return CString(_T(""));
  48. }
  49. virtual HRESULT GetInterpretedValue(LPCTSTR szProperty, LPCTSTR szFormat, TCHAR chFormat, CString * pstrValue, DWORD * pdwValue);
  50. };
  51. //-----------------------------------------------------------------------------
  52. // The CWMIObjectCollection abstract base class encapsulates a collection
  53. // of CWMIObject's. This collection may be treated like an enumeration.
  54. // Subclases of this class may implement the collection as a WMI enumerator,
  55. // or an existing blob of XML data.
  56. //-----------------------------------------------------------------------------
  57. class CWMIObjectCollection
  58. {
  59. public:
  60. CWMIObjectCollection() {};
  61. virtual ~CWMIObjectCollection() {};
  62. // The Create function creates the collection of objects (note - Create
  63. // may be called multiple times on the same object). If the szProperties
  64. // parameter is non-NULL, then it contains a comma delimited list of the
  65. // minimum set of properties which should be included in the collection
  66. // of objects. If it's NULL, then all available properties should be
  67. // included.
  68. virtual HRESULT Create(LPCTSTR szClass, LPCTSTR szProperties = NULL) = 0;
  69. // The following two functions are used to manage the enumeration. GetNext
  70. // returns the next enumerated CWMIObject. When there are no more objects,
  71. // GetNext returns S_FALSE. Obviously, the caller is responsible for
  72. // deleting the object returned.
  73. //
  74. // Note - if the ppObject points to a non-NULL pointer, it's assumed that
  75. // the object has already been created, and can be reused.
  76. virtual HRESULT GetNext(CWMIObject ** ppObject) = 0;
  77. };
  78. //-----------------------------------------------------------------------------
  79. // The CEnumMap is a utility class to cache IEnumWbemClassObject pointers.
  80. // There will be one instance of this class used to improve performance
  81. // by avoiding the high overhead associated with creating enumerators for
  82. // certain classes.
  83. //-----------------------------------------------------------------------------
  84. struct IEnumWbemClassObject;
  85. class CEnumMap
  86. {
  87. public:
  88. CEnumMap() { };
  89. ~CEnumMap() { Reset(); };
  90. IEnumWbemClassObject * GetEnumerator(const CString & strClass);
  91. void SetEnumerator(const CString & strClass, IEnumWbemClassObject * pEnum);
  92. void Reset();
  93. private:
  94. CMapStringToPtr m_mapEnum;
  95. };
  96. //-----------------------------------------------------------------------------
  97. // The CWMIHelper function encapsulates a WMI connection (which might be to
  98. // XML).
  99. //-----------------------------------------------------------------------------
  100. struct GATH_FIELD;
  101. struct IWbemServices;
  102. struct IWbemClassObject;
  103. class CMSIEnumerator;
  104. class CMSIObject;
  105. class CWMIHelper
  106. {
  107. public:
  108. CWMIHelper() : m_pIWbemServices(NULL)
  109. {
  110. ::AfxSetResourceHandle(_Module.GetResourceInstance());
  111. m_strTrue.LoadString(IDS_VERSION5YES);
  112. m_strFalse.LoadString(IDS_VERSION5NO);
  113. m_strPropertyUnavail.LoadString(IDS_ERROR_NOVALUE);
  114. m_strBadProperty.LoadString(IDS_ERROR_NOPROPERTY);
  115. };
  116. virtual ~CWMIHelper() {};
  117. // Enumerate creates a CWMIObjectCollection derived object which enumerates the specified class.
  118. // If szProperties is not null, then it points to a string containing a list of properties to be
  119. // gathered; otherwise all the properties are included.
  120. //
  121. // Note - if ppCollection points to a non-NULL pointer, it's assumed that this object
  122. // can be reused, and no new collection is created.
  123. virtual HRESULT Enumerate(LPCTSTR szClass, CWMIObjectCollection ** ppCollection, LPCTSTR szProperties = NULL) = 0;
  124. // Performs a WQL query (if the subclass supports it).
  125. virtual HRESULT WQLQuery(LPCTSTR szQuery, CWMIObjectCollection ** ppCollection)
  126. {
  127. return E_FAIL;
  128. }
  129. virtual void LoadColumnsFromResource(UINT uiResourceID, CPtrList * aColValues, int iColCount);
  130. virtual void LoadColumnsFromString(LPCTSTR szColumns, CPtrList * aColValues, int iColCount);
  131. virtual CWMIObject * GetSingleObject(LPCTSTR szClass, LPCTSTR szProperties = NULL);
  132. virtual HRESULT NewNamespace(LPCTSTR szNamespace, CWMIHelper **ppNewHelper) { return E_FAIL; };
  133. virtual HRESULT GetNamespace(CString * pstrNamespace) { return E_FAIL; };
  134. virtual HRESULT GetObject(LPCTSTR szObjectPath, CWMIObject ** ppObject) { return E_FAIL; };
  135. virtual void AddObjectToOutput(CPtrList * aColValues, int iColCount, CWMIObject * pObject, LPCTSTR szProperties, UINT uiColumns);
  136. virtual void AddObjectToOutput(CPtrList * aColValues, int iColCount, CWMIObject * pObject, LPCTSTR szProperties, LPCTSTR szColumns);
  137. virtual void AppendBlankLine(CPtrList * aColValues, int iColCount, BOOL fOnlyIfNotEmpty = TRUE);
  138. virtual void AppendCell(CPtrList & listColumns, const CString & strValue, DWORD dwValue, BOOL fAdvanced = FALSE);
  139. // These functions are specific to version 5.0 refreshes, and will be overloaded by the
  140. // live WMI helper.
  141. virtual BOOL Version5ResetClass(const CString & strClass, GATH_FIELD * pConstraintFields) { return FALSE; };
  142. virtual BOOL Version5EnumClass(const CString & strClass, GATH_FIELD * pConstraintFields) { return FALSE; };
  143. virtual BOOL Version5QueryValueDWORD(const CString & strClass, const CString & strProperty, DWORD & dwResult, CString & strMessage) { return FALSE; };
  144. virtual BOOL Version5QueryValueDateTime(const CString & strClass, const CString & strProperty, COleDateTime & datetime, CString & strMessage) { return FALSE; };
  145. virtual BOOL Version5QueryValueDoubleFloat(const CString & strClass, const CString & strProperty, double & dblResult, CString & strMessage) { return FALSE; };
  146. virtual BOOL Version5QueryValue(const CString & strClass, const CString & strProperty, CString & strResult) { return FALSE; };
  147. virtual CMSIEnumerator * Version5GetEnumObject(const CString & strClass, const GATH_FIELD * pConstraints = NULL) { return NULL; };
  148. virtual void Version5RemoveObject(const CString & strClass) {};
  149. virtual CMSIObject * Version5GetObject(const CString & strClass, const GATH_FIELD * pConstraints, CString * pstrLabel = NULL) { return NULL; };
  150. virtual IWbemServices * Version5GetWBEMService(CString * pstrNamespace = NULL) { return NULL; };
  151. virtual BOOL Version5EvaluateFilter(IWbemClassObject * pObject, const GATH_FIELD * pConstraints) { return FALSE; };
  152. virtual void Version5EvaluateJoin(const CString & strClass, IWbemClassObject * pObject, const GATH_FIELD * pConstraints) {};
  153. virtual BOOL Version5IsDependencyJoin(const GATH_FIELD * pConstraints) { return FALSE; };
  154. virtual void Version5EvaluateDependencyJoin(IWbemClassObject * pObject) {};
  155. virtual void Version5RemoveEnumObject(const CString & strClass) {};
  156. virtual void Version5ClearCache() {};
  157. virtual HRESULT Version5CheckValueMap(const CString& strClass, const CString& strProperty, const CString& strVal, CString &strResult) { return E_FAIL; };
  158. public:
  159. CString m_strTrue, m_strFalse, m_strPropertyUnavail, m_strBadProperty;
  160. CMapStringToPtr m_mapClassToInterface;
  161. CMapStringToPtr m_mapClassToEnumInterface;
  162. CEnumMap m_enumMap;
  163. IWbemServices * m_pIWbemServices;
  164. HRESULT m_hrLastVersion5Error;
  165. };
  166. //-----------------------------------------------------------------------------
  167. // Useful utility functions.
  168. //-----------------------------------------------------------------------------
  169. extern void StringReplace(CString & str, LPCTSTR szLookFor, LPCTSTR szReplaceWith);
  170. extern CString GetMSInfoHRESULTString(HRESULT hr);