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.

438 lines
12 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1998-1999 Microsoft Corporation
  4. //
  5. // File: cwbemdsp.h
  6. //
  7. // Description :
  8. // Defines the CWbemDispatchMgr class, which implements the IDispatch
  9. // interfaces for Wbem Objects. The implementation is similar to a
  10. // standard IDispatch, but there is an additional functionality ("dot notation") that allows
  11. // users to call into GetIDsOfNames() & Invoke() using an Wbem property name
  12. // or method name directly (although this is not a property or method of the CWbemObject class).
  13. //
  14. // Part of : WBEM automation interface layer
  15. //
  16. // History:
  17. // corinaf 4/3/98 Created
  18. // alanbos 03/21/00 Revised for Whistler
  19. //
  20. //***************************************************************************
  21. #ifndef _CWBEMDISPMGR_H_
  22. #define _CWBEMDISPMGR_H_
  23. class CSWbemServices;
  24. class CSWbemSecurity;
  25. class CSWbemObject;
  26. class CWbemSchemaIDCache;
  27. //***************************************************************************
  28. //
  29. // Class : CWbemDispID
  30. //
  31. // Description :
  32. // An encoded Dispatch ID for handling typelib, WMI schema
  33. // and custom interface DispId's.
  34. //
  35. //***************************************************************************
  36. typedef unsigned long classCookie;
  37. class CWbemDispID
  38. {
  39. private:
  40. DISPID m_dispId;
  41. static unsigned long s_dispIdCounter;
  42. // Static constants
  43. static const unsigned long s_wmiDispIdTypeMask;
  44. static const unsigned long s_wmiDispIdTypeStatic;
  45. static const unsigned long s_wmiDispIdTypeSchema;
  46. static const unsigned long s_wmiDispIdSchemaTypeMask;
  47. static const unsigned long s_wmiDispIdSchemaTypeProperty;
  48. static const unsigned long s_wmiDispIdSchemaTypeMethod;
  49. static const unsigned long s_wmiDispIdSchemaElementIDMask;
  50. public:
  51. CWbemDispID (void) : m_dispId (0) {}
  52. CWbemDispID (DISPID dispId) : m_dispId (dispId) {}
  53. CWbemDispID (const CWbemDispID & obj) : m_dispId (obj.m_dispId) {}
  54. virtual ~CWbemDispID (void) {}
  55. bool SetAsSchemaID (DISPID dispId, bool bIsProperty = true)
  56. {
  57. bool result = false;
  58. if (dispId <= s_wmiDispIdSchemaElementIDMask)
  59. {
  60. result = true;
  61. m_dispId = dispId;
  62. // Add the bits to identify as static
  63. m_dispId |= s_wmiDispIdTypeSchema;
  64. // Add a bit for the property
  65. if (bIsProperty)
  66. m_dispId |= s_wmiDispIdSchemaTypeMask;
  67. }
  68. return result;
  69. }
  70. bool IsStatic () const
  71. {
  72. return ((DISPID_NEWENUM == m_dispId) ||
  73. (DISPID_VALUE == m_dispId) ||
  74. s_wmiDispIdTypeStatic == (s_wmiDispIdTypeMask & m_dispId));
  75. }
  76. bool IsSchema () const
  77. {
  78. return (s_wmiDispIdTypeSchema == (s_wmiDispIdTypeMask & m_dispId));
  79. }
  80. bool IsSchemaProperty () const
  81. {
  82. return (s_wmiDispIdSchemaTypeProperty == (s_wmiDispIdSchemaTypeMask & m_dispId));
  83. }
  84. bool IsSchemaMethod () const
  85. {
  86. return (s_wmiDispIdSchemaTypeMethod == (s_wmiDispIdSchemaTypeMask & m_dispId));
  87. }
  88. DISPID GetStaticElementID () const
  89. {
  90. return m_dispId;
  91. }
  92. DISPID GetSchemaElementID () const
  93. {
  94. return m_dispId & s_wmiDispIdSchemaElementIDMask;
  95. }
  96. operator DISPID () const
  97. {
  98. return m_dispId;
  99. }
  100. bool operator < (const CWbemDispID & dispId) const
  101. {
  102. return (m_dispId < dispId.m_dispId);
  103. }
  104. };
  105. //***************************************************************************
  106. //
  107. // CLASS NAME:
  108. //
  109. // BSTRless
  110. //
  111. // DESCRIPTION:
  112. //
  113. // Simple utility struct that provides an operator for use in a map based
  114. // on CComBSTR.
  115. //
  116. //***************************************************************************
  117. struct BSTRless : std::binary_function<CComBSTR, CComBSTR, bool>
  118. {
  119. bool operator () (const CComBSTR& _X, const CComBSTR& _Y) const
  120. {
  121. bool result = false;
  122. if (_X.m_str && _Y.m_str)
  123. result = (_wcsicmp (_X.m_str, _Y.m_str) > 0);
  124. else
  125. {
  126. // Treat any string as greater than NULL
  127. if (_X.m_str && !_Y.m_str)
  128. result = true;
  129. }
  130. return result;
  131. }
  132. };
  133. //***************************************************************************
  134. //
  135. // CLASS NAME:
  136. //
  137. // IIDless
  138. //
  139. // DESCRIPTION:
  140. //
  141. // Simple utility struct that provides an operator for use in a map based
  142. // on IID.
  143. //
  144. //***************************************************************************
  145. struct GUIDless : std::binary_function<GUID, GUID, bool>
  146. {
  147. bool operator () (const GUID& _X, const GUID& _Y) const
  148. {
  149. RPC_STATUS rpcStatus;
  150. return (UuidCompare ((GUID*)&_X, (GUID*)&_Y, &rpcStatus) > 0);
  151. }
  152. };
  153. //***************************************************************************
  154. //
  155. // Class : CWbemAllocException
  156. //
  157. // Description :
  158. // Represents an exception thrown when an allocation fails.
  159. //
  160. //***************************************************************************
  161. class CWbemAllocException
  162. {
  163. };
  164. //***************************************************************************
  165. //
  166. // FUNCTION NAME:
  167. //
  168. // _Allocate
  169. //
  170. // DESCRIPTION:
  171. //
  172. // Custom allocator used in the STL map classes to throw exception in cases when
  173. // the allocation fails.
  174. //
  175. //***************************************************************************
  176. template<class _Ty>
  177. class CWbemAllocator {
  178. public:
  179. typedef _SIZT size_type;
  180. typedef _PDFT difference_type;
  181. typedef _Ty _FARQ *pointer;
  182. typedef const _Ty _FARQ *const_pointer;
  183. typedef _Ty _FARQ& reference;
  184. typedef const _Ty _FARQ& const_reference;
  185. typedef _Ty value_type;
  186. pointer address(reference _X) const
  187. {return (&_X); }
  188. const_pointer address(const_reference _X) const
  189. {return (&_X); }
  190. pointer allocate(size_type _N, const void *)
  191. {
  192. // //BUGBUG : This is only to test that we hit all the cases... - should remove eventually !!
  193. // throw CWbemAllocException();
  194. _Ty _FARQ *result = 0;
  195. if (0 == (result = _Allocate((difference_type)_N, (pointer)0)))
  196. throw CWbemAllocException();
  197. return result;
  198. }
  199. char _FARQ *_Charalloc(size_type _N)
  200. {
  201. // //BUGBUG : This is only to test that we hit all the cases... - should remove eventually !!
  202. // throw CWbemAllocException();
  203. char _FARQ *result = 0;
  204. if (0 == (result = _Allocate((difference_type)_N, (char _FARQ *)0)))
  205. throw CWbemAllocException();
  206. return result;
  207. }
  208. void deallocate(void _FARQ *_P, size_type)
  209. {operator delete(_P); }
  210. void construct(pointer _P, const _Ty& _V)
  211. {_Construct(_P, _V); }
  212. void destroy(pointer _P)
  213. {_Destroy(_P); }
  214. _SIZT max_size() const
  215. {_SIZT _N = (_SIZT)(-1) / sizeof (_Ty);
  216. return (0 < _N ? _N : 1); }
  217. };
  218. // return that all specializations of this allocator are interchangeable
  219. //
  220. // Note: we need these operators bacause they are called by swap friend function
  221. //
  222. template <class T1, class T2>
  223. bool operator== (const CWbemAllocator<T1>&,
  224. const CWbemAllocator<T2>&){
  225. return true;
  226. }
  227. template <class T1, class T2>
  228. bool operator!= (const CWbemAllocator<T1>&,
  229. const CWbemAllocator<T2>&){
  230. return false;
  231. }
  232. //***************************************************************************
  233. //
  234. // Class : CWbemDispatchMgr
  235. //
  236. // Description :
  237. // Implements IDispatch for Wbem objects
  238. //
  239. // Public Methods :
  240. // Constructor, Destructor
  241. // IDispatch Methods
  242. //
  243. // Public Data Members :
  244. //
  245. //***************************************************************************
  246. class CWbemDispatchMgr
  247. {
  248. public:
  249. CWbemDispatchMgr(CSWbemServices *pWbemServices,
  250. CSWbemObject *pSWbemObject);
  251. ~CWbemDispatchMgr();
  252. //Dispatch methods
  253. STDMETHOD(GetTypeInfoCount)(THIS_ UINT FAR* pctinfo);
  254. STDMETHOD(GetTypeInfo)(THIS_ UINT itinfo,
  255. LCID lcid,
  256. ITypeInfo FAR* FAR* pptinfo);
  257. STDMETHOD(GetIDsOfNames)(THIS_ REFIID riid,
  258. OLECHAR FAR* FAR* rgszNames,
  259. UINT cNames,
  260. LCID lcid,
  261. DISPID FAR* rgdispid);
  262. STDMETHOD(Invoke)(THIS_ DISPID dispidMember,
  263. REFIID riid,
  264. LCID lcid,
  265. WORD wFlags,
  266. DISPPARAMS FAR* pdispparams,
  267. VARIANT FAR* pvarResult,
  268. EXCEPINFO FAR* pexcepinfo,
  269. UINT FAR* puArgErr);
  270. // IDispatchEx methods
  271. HRESULT STDMETHODCALLTYPE GetDispID(
  272. /* [in] */ BSTR bstrName,
  273. /* [in] */ DWORD grfdex,
  274. /* [out] */ DISPID __RPC_FAR *pid);
  275. /* [local] */ HRESULT STDMETHODCALLTYPE InvokeEx(
  276. /* [in] */ DISPID id,
  277. /* [in] */ LCID lcid,
  278. /* [in] */ WORD wFlags,
  279. /* [in] */ DISPPARAMS __RPC_FAR *pdp,
  280. /* [out] */ VARIANT __RPC_FAR *pvarRes,
  281. /* [out] */ EXCEPINFO __RPC_FAR *pei,
  282. /* [unique][in] */ IServiceProvider __RPC_FAR *pspCaller)
  283. {
  284. UINT uArgErr;
  285. return Invoke(id, IID_NULL, lcid, wFlags, pdp, pvarRes, pei, &uArgErr);
  286. }
  287. HRESULT STDMETHODCALLTYPE DeleteMemberByName(
  288. /* [in] */ BSTR bstr,
  289. /* [in] */ DWORD grfdex)
  290. { return S_FALSE; }
  291. HRESULT STDMETHODCALLTYPE DeleteMemberByDispID(
  292. /* [in] */ DISPID id)
  293. { return S_FALSE; }
  294. HRESULT STDMETHODCALLTYPE GetMemberProperties(
  295. /* [in] */ DISPID id,
  296. /* [in] */ DWORD grfdexFetch,
  297. /* [out] */ DWORD __RPC_FAR *pgrfdex)
  298. { return S_FALSE; }
  299. HRESULT STDMETHODCALLTYPE GetMemberName(
  300. /* [in] */ DISPID id,
  301. /* [out] */ BSTR __RPC_FAR *pbstrName)
  302. { return S_FALSE; }
  303. HRESULT STDMETHODCALLTYPE GetNextDispID(
  304. /* [in] */ DWORD grfdex,
  305. /* [in] */ DISPID id,
  306. /* [out] */ DISPID __RPC_FAR *pid)
  307. { return S_FALSE; }
  308. HRESULT STDMETHODCALLTYPE GetNameSpaceParent(
  309. /* [out] */ IUnknown __RPC_FAR *__RPC_FAR *ppunk)
  310. { return S_FALSE; }
  311. // IProvideClassInfo methods
  312. HRESULT STDMETHODCALLTYPE GetClassInfo (
  313. /* [out] */ ITypeInfo **ppTI
  314. );
  315. // Other Methods
  316. void RaiseException (HRESULT hr);
  317. void SetNewObject (IWbemClassObject *pNewObject);
  318. IWbemClassObject *GetObject ()
  319. {
  320. return m_pWbemObject;
  321. }
  322. IWbemClassObject *GetClassObject ()
  323. {
  324. EnsureClassRetrieved ();
  325. return m_pWbemClass;
  326. }
  327. ISWbemObject *GetSWbemObject ()
  328. {
  329. return (ISWbemObject *)m_pSWbemObject;
  330. }
  331. private:
  332. HRESULT m_hResult;
  333. IWbemClassObject *m_pWbemObject; //pointer to represented WBEM object
  334. CSWbemObject *m_pSWbemObject; //pointer to represented Scripting WBEM object
  335. CSWbemServices *m_pWbemServices; //pointer to WBEM services
  336. IWbemClassObject *m_pWbemClass; //used when m_pWbemObject is an instance, to hold the
  337. //class definition for browsing method signatures
  338. ITypeInfo *m_pTypeInfo; //caches the type info pointer for the interface
  339. ITypeInfo *m_pCTypeInfo; //caches the type info pointer for the coclass
  340. CWbemSchemaIDCache *m_pSchemaCache; // cache of DISPID-Name bindings for WMI schema
  341. //Invokes a WBEM property get or put
  342. HRESULT InvokeWbemProperty(DISPID dispid, unsigned short wFlags,
  343. DISPPARAMS FAR* pdispparams, VARIANT FAR* pvarResult,
  344. EXCEPINFO FAR* pexcepinfo, unsigned int FAR* puArgErr);
  345. //Invokes a WBEM method
  346. HRESULT InvokeWbemMethod(DISPID dispid, DISPPARAMS FAR* pdispparams, VARIANT FAR* pvarResult);
  347. //Helpers for WBEM method out parameter mapping
  348. HRESULT MapReturnValue (VARIANT *pDest, VARIANT *pSrc);
  349. HRESULT MapOutParameter (VARIANT *pDest, VARIANT *pSrc);
  350. HRESULT MapOutParameters (DISPPARAMS FAR* pdispparams, IWbemClassObject *pOutParameters,
  351. IWbemClassObject *pOutParamsInstance, VARIANT FAR* pvarResult);
  352. // Helpers for WBEM method in parameter mapping
  353. HRESULT MapInParameters (DISPPARAMS FAR* pdispparams, IWbemClassObject *pInParameters,
  354. IWbemClassObject **ppInParamsInstance);
  355. HRESULT MapInParameter (VARIANT FAR* pDest, VARIANT FAR* pSrc, CIMTYPE lType);
  356. //Error handling
  357. HRESULT HandleError (DISPID dispidMember, unsigned short wFlags, DISPPARAMS FAR* pdispparams,
  358. VARIANT FAR* pvarResult,UINT FAR* puArgErr,HRESULT hr);
  359. // Class retrieval
  360. void EnsureClassRetrieved ();
  361. };
  362. #endif //_CWBEMDISPMGR_H_