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.

415 lines
9.9 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. // OpWrap.h
  8. #ifndef _OPWRAP_H
  9. #define _OPWRAP_H
  10. #include "MainFrm.h"
  11. _COM_SMARTPTR_TYPEDEF(IWbemClassObject, __uuidof(IWbemClassObject));
  12. _COM_SMARTPTR_TYPEDEF(IWbemObjectSink, __uuidof(IWbemObjectSink));
  13. _COM_SMARTPTR_TYPEDEF(IWbemCallResult, __uuidof(IWbemCallResult));
  14. _COM_SMARTPTR_TYPEDEF(IWbemObjectAccess, __uuidof(IWbemObjectAccess));
  15. _COM_SMARTPTR_TYPEDEF(IWbemQualifierSet, __uuidof(IWbemQualifierSet));
  16. CString GetEmbeddedObjectText(IUnknown *pUnk);
  17. class CObjPtr : public IWbemClassObjectPtr
  18. {
  19. public:
  20. CObjPtr() {}
  21. CObjPtr(IWbemClassObject *pObj) : IWbemClassObjectPtr(pObj) {}
  22. CObjPtr *operator&() { return this; }
  23. };
  24. #include "list"
  25. typedef CArray<int, int> CIntArray;
  26. typedef CMap<int, int, int, int> CIntMap;
  27. typedef CMap<CString, LPCTSTR, CString, LPCTSTR> CStringMap;
  28. class CBitmaskInfo
  29. {
  30. public:
  31. CBitmaskInfo() :
  32. m_dwBitmaskValue(0)
  33. {
  34. }
  35. DWORD m_dwBitmaskValue;
  36. CString m_strName;
  37. };
  38. typedef CArray<CBitmaskInfo, CBitmaskInfo&> CBitmaskInfoArray;
  39. // Use this to get __ExtendedStatus.
  40. IWbemClassObject *GetWMIErrorObject();
  41. class CMethodInfo
  42. {
  43. public:
  44. CString m_strName,
  45. m_strDescription;
  46. BOOL m_bStatic;
  47. CMethodInfo() :
  48. m_bStatic(FALSE)
  49. {
  50. }
  51. };
  52. typedef CList<CMethodInfo, CMethodInfo&> CMethodInfoList;
  53. class CPropInfo
  54. {
  55. public:
  56. CString m_strName;
  57. long m_iHandle;
  58. CIMTYPE m_type;
  59. long m_iFlavor;
  60. BOOL m_bKey;
  61. DWORD m_dwExpectedSize;
  62. VARENUM m_vt;
  63. BOOL m_bSigned,
  64. m_bValueMap,
  65. m_bBitmask;
  66. CStringMap m_mapValues;
  67. CBitmaskInfoArray m_bitmaskValues;
  68. CPropInfo() :
  69. m_iHandle(0),
  70. m_iFlavor(WBEM_FLAVOR_ORIGIN_LOCAL),
  71. m_bKey(FALSE),
  72. m_bValueMap(FALSE),
  73. m_bBitmask(FALSE)
  74. {
  75. SetType(CIM_UINT32);
  76. }
  77. CPropInfo(const CPropInfo& other);
  78. const CPropInfo& operator=(const CPropInfo& other);
  79. void GetPropType(CString &strType, int *piImage, BOOL bIgnoreArrayFlag = FALSE);
  80. // Funcs use to turn variants into strings. Works on arrays too.
  81. BOOL VariantToString(VARIANT *pVar, CString &strValue, BOOL bTranslate,
  82. BOOL bQuoteStrings = FALSE);
  83. // Use this to get turn an array item into a string.
  84. BOOL ArrayItemToString(VARIANT *pVar, DWORD dwIndex, CString &strValue, BOOL bTranslate);
  85. DWORD GetArrayItem(VARIANT *pVar, DWORD dwIndex);
  86. // Helper called by COpWrap::LoadPropInfoList.
  87. void SetType(CIMTYPE type);
  88. // Call this to allocate the safe array before calling StringToArrayItem.
  89. // If dwSize is 0, pVar is set to VT_NULL.
  90. BOOL PrepArrayVar(VARIANT *pVar, DWORD dwSize);
  91. BOOL StringToArrayItem(LPCTSTR szItem, VARIANT *pVar, DWORD dwIndex,
  92. BOOL bTranslate);
  93. BOOL StringToVariant(LPCSTR szValue, VARIANT *pVar, BOOL bTranslate);
  94. BOOL SetArrayItem(VARIANT *pVar, DWORD dwIndex, DWORD dwValue);
  95. BOOL IsBitmask() { return m_bBitmask; }
  96. BOOL IsArray() { return (m_vt & VT_ARRAY) != 0; }
  97. BOOL HasValuemap() { return m_bValueMap; }
  98. DWORD GetArrayItemCount(VARIANT *pVar);
  99. // Masks off the array flag.
  100. VARENUM GetRawType() { return (VARENUM) (m_vt & ~VT_ARRAY); }
  101. CIMTYPE GetRawCIMType() { return (CIMTYPE) (m_type & ~CIM_FLAG_ARRAY); }
  102. protected:
  103. BOOL VariantBlobToString(VARENUM vt, LPVOID pData, CString &strValue,
  104. BOOL bTranslate, BOOL bQuoteStrings);
  105. BOOL StringToVariantBlob(LPCTSTR szValue, LPVOID pData, BOOL bTranslate);
  106. static int GetArrayItemSize(VARENUM vt);
  107. };
  108. class CPropInfoArray : public CArray<CPropInfo, CPropInfo&>
  109. {
  110. public:
  111. CPropInfoArray() :
  112. m_nStaticMethods(0)
  113. {
  114. }
  115. CIntMap m_mapGlobalToLocalIndex;
  116. CMethodInfoList m_listMethods;
  117. // Method stuff.
  118. void LoadMethods(IWbemClassObject *pClass);
  119. int GetMethodCount() { return m_listMethods.GetCount(); }
  120. int GetStaticMethodCount() { return m_nStaticMethods; }
  121. protected:
  122. int m_nStaticMethods;
  123. };
  124. enum WMI_OP_TYPE
  125. {
  126. WMI_QUERY,
  127. WMI_EVENT_QUERY,
  128. WMI_ENUM_OBJ,
  129. WMI_GET_OBJ,
  130. WMI_ENUM_CLASS,
  131. WMI_GET_CLASS,
  132. WMI_CREATE_CLASS,
  133. WMI_CREATE_OBJ,
  134. };
  135. class CObjInfo
  136. {
  137. public:
  138. CObjInfo() :
  139. m_ppProps(NULL),
  140. m_bModified(FALSE)
  141. {
  142. }
  143. ~CObjInfo();
  144. IWbemClassObjectPtr m_pObj;
  145. void SetObj(IWbemClassObject *pObj);
  146. HRESULT LoadProps(IWbemServices *pNamespace);
  147. BOOL GetPropValue(int iIndex, CString &strValue, BOOL bTranslate);
  148. CString GetStringPropValue(LPCWSTR szName);
  149. CString GetStringPropValue(int iIndex);
  150. void GetPropInfo(
  151. int iIndex,
  152. CString &strValue,
  153. CString &strType,
  154. int *piImage,
  155. int *piFlavor,
  156. BOOL bTranslate);
  157. CString GetObjText();
  158. BOOL ValueToVariant(int iIndex, VARIANT *pVar);
  159. void SetModified(BOOL bModified) { m_bModified = bModified; }
  160. BOOL IsModified() { return m_bModified; }
  161. int GetImage() { return m_bModified ? m_iBaseImage + 3 : m_iBaseImage; }
  162. void SetBaseImage(int iImage) { m_iBaseImage = iImage; }
  163. BOOL IsInstance() { return m_iBaseImage == IMAGE_OBJECT; }
  164. CPropInfoArray *GetProps() { return m_ppProps; }
  165. void SetProps(CPropInfoArray *pProps) { m_ppProps = pProps; }
  166. void Export(CStdioFile *pFile, BOOL bShowSystem, BOOL bTranslate,
  167. int iLevel);
  168. void DoDebugStuff();
  169. protected:
  170. BOOL m_bModified;
  171. int m_iBaseImage;
  172. IWbemObjectAccessPtr m_pAccess;
  173. CPropInfoArray *m_ppProps;
  174. //CPropInfoArray m_propsModified;
  175. void GetPropType(CPropInfo &info, CString &strType, int *piImage);
  176. static DWORD BitmaskStrToValue(LPCWSTR szStr);
  177. };
  178. class CClassToProps : public CMap<CString, LPCSTR, CPropInfoArray*, CPropInfoArray*&>
  179. {
  180. public:
  181. ~CClassToProps();
  182. void Flush();
  183. };
  184. class COpWrap;
  185. class CObjSink : public IWbemObjectSink
  186. {
  187. public:
  188. COpWrap *m_pWrap;
  189. LONG m_lRef;
  190. CObjSink(COpWrap *pWrap) :
  191. m_lRef(0)
  192. {
  193. m_pWrap = pWrap;
  194. }
  195. public:
  196. STDMETHODIMP QueryInterface(REFIID refid, PVOID *ppThis)
  197. {
  198. /*
  199. if (refid == IID_IUnknown || refid == IID_IWbemObjectSink)
  200. {
  201. *ppThis = this;
  202. return S_OK;
  203. }
  204. else
  205. return E_NOINTERFACE;
  206. */
  207. HRESULT hr;
  208. if (refid == IID_IUnknown)
  209. {
  210. *ppThis = (IUnknown*) this;
  211. AddRef();
  212. hr = S_OK;
  213. }
  214. else if (refid == IID_IWbemObjectSink)
  215. {
  216. *ppThis = (IWbemObjectSink*) this;
  217. AddRef();
  218. hr = S_OK;
  219. }
  220. else
  221. hr = E_NOINTERFACE;
  222. return hr;
  223. }
  224. STDMETHODIMP_(ULONG) AddRef(void); //{ return 1; }
  225. STDMETHODIMP_(ULONG) Release(void); //{ return 1; }
  226. // IWbemObjectSink
  227. HRESULT STDMETHODCALLTYPE Indicate(
  228. LONG lObjectCount,
  229. IWbemClassObject **ppObjArray);
  230. HRESULT STDMETHODCALLTYPE SetStatus(
  231. LONG lFlags,
  232. HRESULT hResult,
  233. BSTR strParam,
  234. IWbemClassObject *pObjParam);
  235. };
  236. //class COpWrap : public IWbemObjectSink, public CObject
  237. class COpWrap : public CObject
  238. {
  239. public:
  240. CCriticalSection m_cs;
  241. CString m_strOpText;
  242. WMI_OP_TYPE m_type;
  243. HTREEITEM m_item;
  244. int m_iImageBase;
  245. CClassToProps
  246. m_mapClassToProps;
  247. int m_nCount;
  248. int m_status;
  249. BOOL m_bOption;
  250. CObjInfo m_objInfo;
  251. CList<CObjPtr, CObjPtr&> m_listObj;
  252. HRESULT m_hr;
  253. CString m_strErrorText;
  254. IWbemClassObjectPtr m_pErrorObj;
  255. IWbemObjectSinkPtr m_pObjSink;
  256. CStringArray m_strProps;
  257. CIntArray m_piDisplayCols;
  258. CIntArray m_piDisplayColsWidth;
  259. COpWrap(); // For Serialize
  260. COpWrap(WMI_OP_TYPE type, LPCTSTR szText, BOOL bOption = FALSE);
  261. ~COpWrap();
  262. // This only copies over type, text and option.
  263. const COpWrap& operator=(const COpWrap &other);
  264. HRESULT Execute(IWbemServices *pNamespace);
  265. void CancelOp(IWbemServices *pNamespace);
  266. int GetPropIndex(LPCTSTR szName, BOOL bAddToDisplay);
  267. CObjInfo *GetObjInfo() { return &m_objInfo; }
  268. int GetObjCount() { return m_nCount; }
  269. BOOL HoldsObjects() { return m_iChildImage == IMAGE_OBJECT; }
  270. BOOL ShowPathsOnly() { return m_bShowPathsOnly; }
  271. BOOL IsObject() { return m_type == WMI_GET_CLASS || m_type == WMI_GET_OBJ ||
  272. m_type == WMI_CREATE_CLASS || m_type == WMI_CREATE_OBJ; }
  273. CString GetClassName();
  274. CString GetCaption();
  275. int GetImage();
  276. //HRESULT LoadPropInfoList(CObjInfo *pInfo, IWbemClassObject *pClass,
  277. // CPropInfoArray *pProps);
  278. HRESULT RefreshPropInfo(CObjInfo *pInfo);
  279. void GetPropValue(
  280. CObjInfo *pInfo,
  281. int iIndex,
  282. CString &strValue,
  283. BOOL bTranslate);
  284. // Serialize stuff
  285. DECLARE_SERIAL(COpWrap)
  286. void Serialize(CArchive &archive);
  287. static CString GetWbemErrorText(HRESULT hres);
  288. protected:
  289. BOOL m_bShowPathsOnly;
  290. int m_iChildImage,
  291. m_nExpectedStatusCalls;
  292. LONG m_lRef;
  293. // We need this to get the amended qualifiers.
  294. IWbemServices *m_pNamespace;
  295. void SetHoldsObjects();
  296. void AddPropsToGlobalIndex(CObjInfo *pInfo);
  297. HRESULT LoadPropInfo(CObjInfo *pInfo);
  298. // Called by constructor and Serialize after member vars are set.
  299. void Init();
  300. public:
  301. // IUnknown
  302. /*
  303. STDMETHODIMP QueryInterface(REFIID refid, PVOID *ppThis)
  304. {
  305. if (refid == IID_IUnknown || refid == IID_IWbemObjectSink)
  306. {
  307. *ppThis = this;
  308. return S_OK;
  309. }
  310. else
  311. return E_NOINTERFACE;
  312. }
  313. STDMETHODIMP_(ULONG) AddRef(void); //{ return 1; }
  314. STDMETHODIMP_(ULONG) Release(void); //{ return 1; }
  315. */
  316. HRESULT STDMETHODCALLTYPE Indicate(
  317. LONG lObjectCount,
  318. IWbemClassObject **ppObjArray);
  319. HRESULT STDMETHODCALLTYPE SetStatus(
  320. LONG lFlags,
  321. HRESULT hResult,
  322. BSTR strParam,
  323. IWbemClassObject *pObjParam);
  324. };
  325. typedef CList<COpWrap, COpWrap&> COpList;
  326. #endif