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.

272 lines
5.3 KiB

  1. /*++
  2. Copyright (c) 2000, Microsoft Corporation
  3. Module Name:
  4. WmiGateway.h
  5. Abstract:
  6. The file contains the the class definition for CWmiGateway. CWmiGateway is a class
  7. that extracts information from WMI,
  8. --*/
  9. #pragma once
  10. #include "stdafx.h"
  11. #ifndef WMIGATEWAY_H
  12. #define WMIGATEWAY_H
  13. // Defines which interface the client is using to communicate with us
  14. //
  15. typedef enum
  16. {
  17. NO_INTERFACE = 0,
  18. COM_INTERFACE = 1,
  19. NETSH_INTERFACE = 2,
  20. }INTERFACE_TYPE;
  21. // A cache for all the repositories opened. We cache this information since it can take a long time
  22. // to connect to a repository through WMI.
  23. //
  24. typedef map<wstring, CComPtr<IWbemServices> > WbemServiceCache;
  25. struct Property
  26. {
  27. public:
  28. void SetProperty(LPCTSTR pszwPropertyName, BOOLEAN bPropertyFlags)
  29. {
  30. if( pszwPropertyName )
  31. {
  32. pszwName = new WCHAR[lstrlen(pszwPropertyName) + 1];
  33. if( pszwName )
  34. {
  35. lstrcpy(pszwName,pszwPropertyName);
  36. }
  37. }
  38. else
  39. {
  40. pszwName = NULL;
  41. }
  42. bFlags = bPropertyFlags;
  43. }
  44. public:
  45. Property()
  46. {
  47. pszwName = NULL;
  48. }
  49. Property(const Property *ref):Value(ref->Value)
  50. {
  51. if( ref != this )
  52. {
  53. SetProperty(ref->pszwName,ref->bFlags);
  54. }
  55. }
  56. Property(const Property &ref):Value(ref.Value)
  57. {
  58. if( &ref != this )
  59. {
  60. SetProperty(ref.pszwName,ref.bFlags);
  61. }
  62. }
  63. Property(LPCTSTR pszwPropertyName, BOOLEAN bPropertyFlags = 0)
  64. {
  65. SetProperty(pszwPropertyName,bPropertyFlags);
  66. }
  67. void Clear()
  68. {
  69. if ( pszwName )
  70. {
  71. delete pszwName;
  72. pszwName = NULL;
  73. }
  74. Value.clear();
  75. }
  76. ~Property()
  77. {
  78. Clear();
  79. }
  80. public:
  81. LPTSTR pszwName;
  82. BOOLEAN bFlags;
  83. typedef vector< _variant_t > Values;
  84. Values Value;
  85. };
  86. typedef list< Property > EnumProperty;
  87. struct WbemProperty: public Property
  88. {
  89. public:
  90. void SetWbemProperty(LPCTSTR pszwWbemRepository = NULL, LPCTSTR pszwWbemNamespace = NULL)
  91. {
  92. if( pszwWbemRepository != NULL )
  93. {
  94. pszwRepository = new WCHAR[lstrlen(pszwWbemRepository) + 1];
  95. if( pszwRepository )
  96. {
  97. lstrcpy(pszwRepository,pszwWbemRepository);
  98. }
  99. }
  100. else
  101. {
  102. pszwRepository = NULL;
  103. }
  104. if( pszwWbemNamespace != NULL )
  105. {
  106. pszwNamespace = new WCHAR[lstrlen(pszwWbemNamespace) + 1];
  107. if( pszwNamespace )
  108. {
  109. lstrcpy(pszwNamespace,pszwWbemNamespace);
  110. }
  111. }
  112. else
  113. {
  114. pszwNamespace = NULL;
  115. }
  116. }
  117. public:
  118. WbemProperty()
  119. {
  120. pszwRepository = NULL;
  121. pszwNamespace = NULL;
  122. }
  123. WbemProperty(const WbemProperty * ref):Property(ref->pszwName,ref->bFlags)
  124. {
  125. if( ref != this )
  126. {
  127. SetWbemProperty(ref->pszwRepository,ref->pszwNamespace);
  128. }
  129. }
  130. WbemProperty(const WbemProperty & ref): Property(ref.pszwName,ref.bFlags)
  131. {
  132. if( &ref != this )
  133. {
  134. SetWbemProperty(ref.pszwRepository,ref.pszwNamespace);
  135. }
  136. }
  137. WbemProperty(LPCTSTR pszwPropertyName, BOOLEAN bPropertyFlags = 0, LPCTSTR pszwWbemRepository = NULL, LPCTSTR pszwWbemNamespace = NULL):
  138. Property(pszwPropertyName,bPropertyFlags)
  139. {
  140. SetWbemProperty(pszwWbemRepository,pszwWbemNamespace);
  141. }
  142. ~WbemProperty()
  143. {
  144. if ( pszwRepository )
  145. {
  146. delete pszwRepository;
  147. }
  148. if ( pszwNamespace )
  149. {
  150. delete pszwNamespace;
  151. }
  152. pszwRepository = NULL;
  153. pszwNamespace = NULL;
  154. }
  155. LPTSTR pszwRepository;
  156. LPTSTR pszwNamespace;
  157. };
  158. typedef list< WbemProperty > EnumWbemProperty;
  159. class CWmiGateway
  160. /*++
  161. Class Description
  162. TBD
  163. --*/
  164. {
  165. public:
  166. CWmiGateway();
  167. void SetCancelOption(HANDLE hTerminateThread)
  168. {
  169. m_hTerminateThread = hTerminateThread;
  170. m_bTerminate = FALSE;
  171. }
  172. inline BOOL CWmiGateway::ShouldTerminate();
  173. private:
  174. HANDLE m_hTerminateThread;
  175. BOOL m_bTerminate;
  176. wstring m_wstrMachine;
  177. public:
  178. BOOL WbemInitialize(INTERFACE_TYPE bInterface);
  179. VOID SetMachine(WCHAR *pszwMachine);
  180. void EmptyCache();
  181. IWbemServices *
  182. GetWbemService(
  183. IN LPCTSTR pszService
  184. );
  185. IEnumWbemClassObject *
  186. GetEnumWbemClassObject(
  187. IN LPCTSTR pszwService,
  188. IN LPCTSTR pszwNameSpace
  189. );
  190. IWbemClassObject *
  191. GetWbemClassObject(
  192. IN LPCTSTR pszwService,
  193. IN LPCTSTR pszwNameSpace,
  194. IN const int nInstance = 0
  195. );
  196. HRESULT
  197. CWmiGateway::GetWbemProperties(
  198. IN OUT EnumWbemProperty &EnumProp
  199. );
  200. void
  201. ReleaseAll(
  202. IN IEnumWbemClassObject *pEnumWbemClassObject,
  203. IN IWbemClassObject *pWbemClassObject[],
  204. IN int nInstance
  205. );
  206. HRESULT
  207. GetWbemProperty(
  208. IN LPCTSTR pszwService,
  209. IN LPCTSTR pszwNameSpace,
  210. IN LPCTSTR pszwField,
  211. OUT _variant_t &vValue
  212. );
  213. ~CWmiGateway();
  214. wstring m_wstrWbemError;
  215. private:
  216. IWbemLocator *m_pWbemLocater;
  217. WbemServiceCache m_WbemServiceCache;
  218. };
  219. #endif