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.

343 lines
7.9 KiB

  1. /*++
  2. Copyright (c) 1998-2000 Microsoft Corporation
  3. Module Name:
  4. utils.h
  5. Abstract:
  6. General purpose utilities
  7. Author:
  8. ???
  9. Revision History:
  10. Mohit Srivastava 18-Dec-00
  11. --*/
  12. #ifndef _utils_h_
  13. #define _utils_h_
  14. #define MAX_BUF_SIZE 1024
  15. #define MAX_KEY_NAME_SIZE 32
  16. #define MAX_KEY_TYPE_SIZE 32
  17. #include "WbemServices.h"
  18. #include "schema.h"
  19. #include <wbemprov.h>
  20. #include <windows.h>
  21. #include <comutil.h>
  22. #include <genlex.h>
  23. #include <opathlex.h>
  24. #include <objpath.h>
  25. #include <dbgutil.h>
  26. #include <atlbase.h>
  27. class CUtils
  28. {
  29. public:
  30. //
  31. // Wrappers for commonly used WMI operations
  32. //
  33. static HRESULT CreateEmptyMethodInstance(
  34. CWbemServices* i_pNamespace,
  35. IWbemContext* i_pCtx,
  36. LPCWSTR i_wszClassName,
  37. LPCWSTR i_wszMethodName,
  38. IWbemClassObject** o_ppMethodInstance);
  39. static HRESULT GetQualifiers(
  40. IWbemClassObject* i_pClass,
  41. LPCWSTR* i_awszQualNames,
  42. VARIANT* io_aQualValues,
  43. ULONG i_NrQuals);
  44. static HRESULT GetPropertyQualifiers(
  45. IWbemClassObject* i_pClass,
  46. LPCWSTR i_wszPropName,
  47. DWORD* io_pdwQuals);
  48. static HRESULT SetQualifiers(
  49. IWbemClassObject* i_pClass,
  50. LPCWSTR* i_awszQualNames,
  51. VARIANT* i_avtQualValues,
  52. ULONG i_iNrQuals,
  53. ULONG i_iFlags);
  54. static HRESULT SetMethodQualifiers(
  55. IWbemClassObject* i_pClass,
  56. LPCWSTR i_wszMethName,
  57. LPCWSTR* i_awszQualNames,
  58. VARIANT* i_avtQualValues,
  59. ULONG i_iNrQuals);
  60. static HRESULT SetPropertyQualifiers(
  61. IWbemClassObject* i_pClass,
  62. LPCWSTR i_wszPropName,
  63. LPCWSTR* i_awszQualNames,
  64. VARIANT* i_avtQualValues,
  65. ULONG i_iNrQuals);
  66. static HRESULT CreateEmptyInstance(
  67. LPWSTR i_wszClass,
  68. CWbemServices* i_pNamespace,
  69. IWbemClassObject** o_ppInstance);
  70. //
  71. // Retrieval from schema
  72. //
  73. static bool GetClass(
  74. LPCWSTR i_wszClassName,
  75. WMI_CLASS** o_ppClass);
  76. static bool GetAssociation(
  77. LPCWSTR i_wszAssocName,
  78. WMI_ASSOCIATION** o_ppAssoc);
  79. static bool GetMethod(
  80. LPCWSTR i_wszMethod,
  81. WMI_METHOD** i_apMethodList,
  82. WMI_METHOD** o_ppMethod);
  83. //
  84. // Data conversion/comparison
  85. //
  86. static bool CompareKeyType(
  87. LPCWSTR i_wszKeyFromMb,
  88. METABASE_KEYTYPE* i_pktKeyCompare);
  89. static bool CompareMultiSz(
  90. WCHAR* i_msz1,
  91. WCHAR* i_msz2);
  92. static HRESULT CreateByteArrayFromSafeArray(
  93. _variant_t& i_vt,
  94. LPBYTE* o_paBytes,
  95. DWORD* io_pdw);
  96. static HRESULT LoadSafeArrayFromByteArray(
  97. LPBYTE i_aBytes,
  98. DWORD i_iBytes,
  99. _variant_t& io_vt);
  100. static bool CompareByteArray(
  101. LPBYTE i_aBytes1,
  102. ULONG i_iBytes1,
  103. LPBYTE i_aBytes2,
  104. ULONG i_iBytes2
  105. );
  106. static BSTR ExtractBstrFromVt(
  107. const VARIANT* i_pvt,
  108. LPCWSTR i_wszVtName=NULL);
  109. static LONG ExtractLongFromVt(
  110. const VARIANT* i_pvt,
  111. LPCWSTR i_wszVtName=NULL);
  112. //
  113. // Other
  114. //
  115. static KeyRef* GetKey(
  116. ParsedObjectPath* i_pParsedObjectPath,
  117. WCHAR* i_wsz);
  118. static void GetMetabasePath(
  119. IWbemClassObject* i_pObj,
  120. ParsedObjectPath* i_pParsedObjectPath,
  121. WMI_CLASS* i_pClass,
  122. _bstr_t& io_bstrPath);
  123. static HRESULT GetParentMetabasePath(
  124. LPCWSTR i_wszChildPath,
  125. LPWSTR io_wszParentPath);
  126. static HRESULT ConstructObjectPath(
  127. LPCWSTR i_wszMbPath,
  128. const WMI_CLASS* i_pClass,
  129. BSTR* o_pbstrPath);
  130. static void FileTimeToWchar(
  131. FILETIME* i_pFileTime,
  132. LPWSTR io_wszDateTime);
  133. //
  134. // For exception handling and/or errors
  135. //
  136. static HRESULT ParserErrToHR(DWORD i_dwErr)
  137. {
  138. switch(i_dwErr)
  139. {
  140. case CObjectPathParser::NoError:
  141. break;
  142. case CObjectPathParser::OutOfMemory:
  143. return WBEM_E_OUT_OF_MEMORY;
  144. default:
  145. return WBEM_E_INVALID_OBJECT;
  146. }
  147. return WBEM_S_NO_ERROR;
  148. }
  149. static void HRToText(
  150. HRESULT i_hr,
  151. BSTR* o_pbstrText);
  152. static void MessageCodeToText(
  153. DWORD i_dwMC,
  154. va_list* i_pArgs,
  155. BSTR* o_pbstrText);
  156. static void Throw_Exception(HRESULT, METABASE_PROPERTY*);
  157. };
  158. class CIIsProvException
  159. {
  160. public:
  161. CIIsProvException() :
  162. m_hr(0),
  163. m_bErrorSet(false)
  164. {
  165. }
  166. ~CIIsProvException()
  167. {
  168. }
  169. void SetHR(HRESULT i_hr, LPCWSTR i_wszParams=NULL)
  170. /*++
  171. Synopsis:
  172. Arguments: [i_hr] - The HR
  173. [i_wszParams=NULL] - The param field of __ExtendedStatus
  174. --*/
  175. {
  176. DBG_ASSERT(m_bErrorSet == false);
  177. m_hr = i_hr;
  178. m_sbstrParams = i_wszParams;
  179. m_bErrorSet = true;
  180. ConstructStringFromHR(i_hr);
  181. }
  182. void SetMC(HRESULT i_hr, DWORD i_dwMC, LPCWSTR i_wszParams, ...)
  183. /*++
  184. Synopsis:
  185. Arguments: [i_hr] - The HR
  186. [i_dwMC] - The MC code
  187. [i_wszParams] - The param field of __ExtendedStatus
  188. [...] - The args for the MC error string
  189. --*/
  190. {
  191. DBG_ASSERT(m_bErrorSet == false);
  192. m_hr = i_hr;
  193. m_sbstrParams = i_wszParams;
  194. va_list (marker);
  195. va_start(marker, i_wszParams);
  196. m_bErrorSet = true;
  197. ConstructStringFromMC(i_dwMC, &marker);
  198. }
  199. //
  200. // For getting errors (Get text representation, hr, and culprit param)
  201. // These are fields of __Extended Status
  202. //
  203. HRESULT GetHR() const
  204. {
  205. DBG_ASSERT(m_bErrorSet == true);
  206. return m_hr;
  207. }
  208. BSTR GetParams() const
  209. {
  210. DBG_ASSERT(m_bErrorSet == true);
  211. return m_sbstrParams;
  212. }
  213. BSTR GetErrorText() const
  214. {
  215. DBG_ASSERT(m_bErrorSet == true);
  216. return m_sbstrError;
  217. }
  218. private:
  219. void ConstructStringFromHR(
  220. HRESULT i_hr)
  221. {
  222. DBG_ASSERT(m_bErrorSet == true);
  223. //
  224. // If this fails, m_sbstrError will be NULL. This is okay.
  225. //
  226. CUtils::HRToText(i_hr, &m_sbstrError);
  227. }
  228. void ConstructStringFromMC(
  229. DWORD i_dwMC,
  230. va_list* i_pArgs)
  231. {
  232. DBG_ASSERT(m_bErrorSet == true);
  233. //
  234. // If this fails, m_sbstrError will be NULL. This is okay.
  235. //
  236. CUtils::MessageCodeToText(i_dwMC, i_pArgs, &m_sbstrError);
  237. }
  238. //
  239. // These are fields of __ExtendedStatus
  240. //
  241. HRESULT m_hr;
  242. CComBSTR m_sbstrParams;
  243. CComBSTR m_sbstrError;
  244. //
  245. // Used just for assert
  246. //
  247. bool m_bErrorSet;
  248. };
  249. #define THROW_ON_FALSE(b) \
  250. if (!b) \
  251. throw((HRESULT)WBEM_E_FAILED);
  252. // if client cancelled, stop and return successfully
  253. #define THROW_ON_ERROR(hr) \
  254. if (FAILED(hr)) \
  255. { \
  256. DBGPRINTF((DBG_CONTEXT, "FAILED: hr = %x\n", hr)); \
  257. throw(hr == WBEM_E_CALL_CANCELLED ? WBEM_NO_ERROR : (HRESULT)hr); \
  258. } \
  259. #define THROW_E_ON_ERROR(hr, pmbp) \
  260. if (FAILED(hr)) \
  261. { \
  262. CUtils::Throw_Exception(hr, pmbp); \
  263. }
  264. #define EXIT_ON_ERROR(hr) \
  265. if(FAILED(hr)) \
  266. { \
  267. goto exit; \
  268. }
  269. #endif