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.

1065 lines
37 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. //***************************************************************************
  6. #ifndef _WMICOM_HEADER
  7. #define _WMICOM_HEADER
  8. #define THISPROVIDER LOG_WIMPROV
  9. ///////////////////////////////////////////////////////////////////////
  10. // Forward declarations
  11. ///////////////////////////////////////////////////////////////////////
  12. class CWMIManagement;
  13. class CWMIDataBlock;
  14. class CWMIProcessClass;
  15. class CNamespaceManagement;
  16. #define FROM_DATA_BLOCK 1
  17. #define FROM_INITIALIZATION 2
  18. #define RUNTIME_BINARY_MOFS_ADDED L"RuntimeBinaryMofsAdded"
  19. #define RUNTIME_BINARY_MOFS_DELETED L"RuntimeBinaryMofsDeleted"
  20. ///////////////////////////////////////////////////////////////////////
  21. //#pragma warning( disable : 4005 )
  22. #include <objbase.h>
  23. //#pragma warning( default : 4005 )
  24. // kill warning: "nonstandard extension ...
  25. //#pragma warning( disable : 4200 )
  26. #include "wmi\wmium.h"
  27. //#pragma warning( default : 4200 )
  28. #include <wbemint.h>
  29. #include <wchar.h>
  30. #include <wbemidl.h>
  31. #include <GENLEX.H>
  32. #include <OPATHLEX.H>
  33. #include <OBJPATH.H>
  34. #include <flexarry.h>
  35. #include <oahelp.inl>
  36. #include <mofcomp.h>
  37. #include <wbemutil.h>
  38. #include <cominit.h>
  39. #define SERVICES m_pWMI->Services()
  40. #define CONTEXT m_pWMI->Context()
  41. #define HANDLER m_pWMI->Handler()
  42. SAFEARRAY * OMSSafeArrayCreate( IN VARTYPE vt, IN int iNumElements);
  43. #define WMI_BINARY_MOF_GUID L"{05901221-D566-11d1-B2F0-00A0C9062910}"
  44. ///////////////////////////////////////////////////////////////////////
  45. // Defines
  46. /////////////////////////////////////////////////////////////////////
  47. #define SAFE_DELETE_PTR(pv) \
  48. { if(pv) delete pv; \
  49. pv = NULL; }
  50. #define SAFE_RELEASE_PTR(pv) \
  51. { if(pv){ pv->Release(); } \
  52. pv = NULL; }
  53. #define SAFE_DELETE_ARRAY(pv) \
  54. { if(pv) delete []pv; \
  55. pv = NULL; }
  56. #define GUID_SIZE 128
  57. #define NAME_SIZE 256*2
  58. #define ProcessOneFixedInstance 1
  59. #define ProcessUnfixedInstance 2
  60. #define NoMore 3
  61. #define MEMSIZETOALLOCATE 512
  62. #define MSG_SIZE 512
  63. #define INTERNAL_EVENT 5
  64. #define PERMANENT_EVENT 6
  65. #define SIZEOFWBEMDATETIME sizeof(WCHAR)*25
  66. #define WMI_NO_MORE 0x80044001
  67. #define PUT_WHOLE_INSTANCE 0
  68. #define PUT_PROPERTIES_ONLY 1
  69. #define PUT_PROPERTIES_IN_LIST_ONLY 2
  70. #define WMI_RESOURCE_MOF_ADDED_GUID L"{B48D49A2-E777-11D0-A50C-00A0C9062910}"
  71. #define WMI_RESOURCE_MOF_REMOVED_GUID L"{B48D49A3-E777-11d0-A50C-00A0C9062910}"
  72. #define MSG_DATA_INSTANCE_NOT_FOUND L"The instance name passed was not recognized as valid"
  73. #define MSG_DATA_NOT_AVAILABLE L"The WDM data block is no longer available."
  74. #define MSG_SUCCESS L"Operation completed successfully"
  75. #define MSG_INVALID_BLOCK_POINTER L"WDM Buffer size and actual size of data do not match"
  76. #define MSG_DRIVER_ERROR L"WDM specific error code: 4209 (Driver specific error, driver could not complete request)"
  77. #define MSG_READONLY_ERROR L"WDM specific error code: 4213 (The WDM data item or data block is read-only)"
  78. #define MSG_ARRAY_ERROR L"Array is the wrong size"
  79. #define IDS_ImpersonationFailed "Impersonation failed - Access denied"
  80. #define ANSI_MSG_DATA_INSTANCE_NOT_FOUND "The instance name passed was not recognized as valid"
  81. #define ANSI_MSG_DRIVER_ERROR "WDM specific error code: 4209 (Driver specific error, driver could not complete request)"
  82. #define ANSI_MSG_INVALID_PARAMETER "Invalid Parameter"
  83. #define ANSI_MSG_INVALID_DATA "Invalid Data"
  84. #define ANSI_MSG_INVALID_NAME_BLOCK "Invalid Name Block"
  85. #define ANSI_MSG_INVALID_DATA_BLOCK "Invalid Data Block"
  86. #define ANSI_MSG_ACCESS_DENIED "Access Denied"
  87. #define MOF_ADDED 1
  88. #define MOF_DELETED 2
  89. #define STANDARD_EVENT 0
  90. //************************************************************************************************************
  91. //============================================================================================================
  92. //
  93. // The Utility Functions
  94. //
  95. //============================================================================================================
  96. //************************************************************************************************************
  97. BOOL IsBinaryMofResourceEvent(LPOLESTR pGuid, GUID gGuid);
  98. bool IsNT(void);
  99. BOOL GetUserThreadToken(HANDLE * phThreadTok);
  100. void TranslateAndLog( WCHAR * wcsMsg );
  101. BOOL SetGuid(WCHAR * wcsGuid, CLSID & Guid);
  102. HRESULT AllocAndCopy(WCHAR * wcsSource, WCHAR ** pwcsDest );
  103. HRESULT CheckIfThisIsAValidKeyProperty(WCHAR * wcsClass, WCHAR * wcsProperty, IWbemServices * p);
  104. HRESULT GetParsedPath( BSTR ObjectPath,WCHAR * wcsClass, WCHAR * wcsInstance,IWbemServices * p);
  105. BOOL GetParsedPropertiesAndClass( BSTR Query,WCHAR * wcsClass );
  106. //************************************************************************************************************
  107. //============================================================================================================
  108. //
  109. // The Utility Classes / struct definitions
  110. //
  111. //============================================================================================================
  112. //************************************************************************************************************
  113. class CAutoWChar
  114. {
  115. WCHAR * m_pStr;
  116. public:
  117. CAutoWChar(int nSize) { m_pStr = new WCHAR[nSize+1]; if( m_pStr ) memset( m_pStr,NULL,nSize+1); }
  118. ~CAutoWChar() { SAFE_DELETE_ARRAY(m_pStr);}
  119. BOOL Valid() { if( !m_pStr ) return FALSE; return TRUE; }
  120. operator PWCHAR() { return m_pStr; }
  121. };
  122. ///////////////////////////////////////////////////////////////////////
  123. class CCriticalSection
  124. {
  125. public:
  126. CCriticalSection() { }
  127. ~CCriticalSection() { }
  128. inline void Init() { InitializeCriticalSection(&m_criticalsection); }
  129. inline void Delete() { DeleteCriticalSection(&m_criticalsection); }
  130. inline void Enter() { EnterCriticalSection(&m_criticalsection); }
  131. inline void Leave() { LeaveCriticalSection(&m_criticalsection); }
  132. private:
  133. CRITICAL_SECTION m_criticalsection; // standby critical section
  134. };
  135. ///////////////////////////////////////////////////////////////////////
  136. class CAutoBlock
  137. {
  138. private:
  139. CCriticalSection *m_pCriticalSection;
  140. public:
  141. CAutoBlock(CCriticalSection *pCriticalSection)
  142. {
  143. m_pCriticalSection = NULL;
  144. if(pCriticalSection)
  145. {
  146. pCriticalSection->Enter();
  147. }
  148. m_pCriticalSection = pCriticalSection;
  149. }
  150. ~CAutoBlock()
  151. {
  152. if(m_pCriticalSection)
  153. m_pCriticalSection->Leave();
  154. }
  155. };
  156. typedef struct _AccessList
  157. {
  158. CFlexArray m_List;
  159. HRESULT Add(IWbemObjectAccess * pPtr)
  160. {
  161. HRESULT hr = S_OK;
  162. pPtr->AddRef();
  163. if(CFlexArray::out_of_memory == m_List.Add(pPtr))
  164. {
  165. pPtr->Release();
  166. hr = E_OUTOFMEMORY;
  167. }
  168. return hr;
  169. }
  170. inline long Size() { return m_List.Size(); }
  171. inline void ** List() { return m_List.GetArrayPtr(); }
  172. _AccessList() {}
  173. ~_AccessList(); // code elsewhere
  174. }AccessList;
  175. typedef struct _IdList
  176. {
  177. CFlexArray m_List;
  178. // 170635
  179. HRESULT Add( ULONG_PTR l)
  180. {
  181. HRESULT hr = S_OK;
  182. ULONG_PTR * lp = new ULONG_PTR;
  183. if(lp)
  184. {
  185. *lp = l;
  186. if(CFlexArray::out_of_memory == m_List.Add(lp))
  187. {
  188. SAFE_DELETE_PTR(lp);
  189. hr = E_OUTOFMEMORY;
  190. }
  191. }
  192. else
  193. {
  194. hr = E_OUTOFMEMORY;
  195. }
  196. return hr;
  197. }
  198. inline long Size() { return m_List.Size(); }
  199. inline void ** List() { return m_List.GetArrayPtr(); }
  200. _IdList() {}
  201. ~_IdList(); // code elsewhere
  202. } IdList;
  203. typedef struct _HandleList
  204. {
  205. CFlexArray m_List;
  206. // 170635
  207. HRESULT Add( HANDLE l )
  208. {
  209. HRESULT hr = S_OK;
  210. HANDLE * lp = new HANDLE;
  211. if(lp)
  212. {
  213. *lp = l;
  214. if(CFlexArray::out_of_memory == m_List.Add(lp))
  215. {
  216. SAFE_DELETE_PTR(lp);
  217. hr = E_OUTOFMEMORY;
  218. }
  219. }
  220. else
  221. {
  222. hr = E_OUTOFMEMORY;
  223. }
  224. return hr;
  225. }
  226. inline long Size() { return m_List.Size(); }
  227. inline void ** List() { return m_List.GetArrayPtr(); }
  228. _HandleList() {}
  229. ~_HandleList(); // code elsewhere
  230. } HandleList;
  231. typedef struct _KeyList
  232. {
  233. CWStringArray m_List;
  234. // ================================================================
  235. // Appends a new element to the end of the array. Copies the param.
  236. // ================================================================
  237. int Add(WCHAR * pStr)
  238. {
  239. return m_List.Add( pStr );
  240. }
  241. // ================================================================
  242. // Locates a string or returns -1 if not found.
  243. // ================================================================
  244. int Find(WCHAR * pStr)
  245. {
  246. int nFlags = 0;
  247. return m_List.FindStr( pStr, nFlags );
  248. }
  249. // ================================================================
  250. // Removes a string
  251. // ================================================================
  252. HRESULT Remove(WCHAR * pStr)
  253. {
  254. int nIndex = Find(pStr);
  255. if( nIndex > -1 )
  256. {
  257. m_List.RemoveAt( nIndex );
  258. }
  259. return S_OK;
  260. }
  261. // ================================================================
  262. // Removes a string
  263. // ================================================================
  264. BOOL OldDriversLeftOver()
  265. {
  266. if( m_List.Size() > 0 )
  267. {
  268. return TRUE;
  269. }
  270. return FALSE;
  271. }
  272. // ================================================================
  273. // Get how many are in there
  274. // ================================================================
  275. int GetSize()
  276. {
  277. return m_List.Size();
  278. }
  279. // ================================================================
  280. // Get at a specific position
  281. // ================================================================
  282. WCHAR * GetAt(int n)
  283. {
  284. return m_List.GetAt(n);
  285. }
  286. _KeyList() {}
  287. ~_KeyList(){}
  288. } KeyList;
  289. typedef struct _InstanceList
  290. {
  291. CFlexArray m_List;
  292. // 170635
  293. HRESULT Add( WCHAR * p )
  294. {
  295. HRESULT hr = S_OK;
  296. WCHAR * pNew = NULL;
  297. if(SUCCEEDED(hr = AllocAndCopy(p, &pNew)))
  298. {
  299. if(CFlexArray::out_of_memory == m_List.Add(pNew))
  300. {
  301. hr = E_OUTOFMEMORY;
  302. SAFE_DELETE_ARRAY(pNew);
  303. }
  304. }
  305. return hr;
  306. }
  307. inline long Size() { return m_List.Size(); }
  308. inline void ** List() { return m_List.GetArrayPtr(); }
  309. _InstanceList() {}
  310. ~_InstanceList(); // code elsewhere
  311. } InstanceList;
  312. typedef struct _OldClassInfo
  313. {
  314. WCHAR * m_pClass;
  315. WCHAR * m_pPath;
  316. _OldClassInfo() { m_pClass = m_pPath = NULL; }
  317. ~_OldClassInfo(); // code elsewhere
  318. } OldClassInfo;
  319. typedef struct _OldClassList
  320. {
  321. CFlexArray m_List;
  322. HRESULT Add( WCHAR * pClass, WCHAR * pPath )
  323. {
  324. HRESULT hr = S_OK;
  325. if ( !pClass )
  326. {
  327. return WBEM_E_INVALID_PARAMETER;
  328. }
  329. if ( !pPath)
  330. {
  331. return WBEM_E_INVALID_PARAMETER;
  332. }
  333. OldClassInfo * pInfo = new OldClassInfo;
  334. if( pInfo )
  335. {
  336. if(SUCCEEDED(hr = AllocAndCopy(pClass, &(pInfo)->m_pClass)))
  337. {
  338. if(SUCCEEDED(hr = AllocAndCopy(pPath, &(pInfo)->m_pPath)))
  339. {
  340. if(CFlexArray::out_of_memory == m_List.Add(pInfo))
  341. {
  342. hr = E_OUTOFMEMORY;
  343. SAFE_DELETE_PTR(pInfo);
  344. }
  345. }
  346. }
  347. }
  348. else
  349. {
  350. hr = E_OUTOFMEMORY;
  351. }
  352. return hr;
  353. }
  354. WCHAR * GetClass( int nIndex )
  355. {
  356. HRESULT hr = S_OK;
  357. OldClassInfo * pInfo = NULL;
  358. pInfo = (OldClassInfo *) m_List[nIndex];
  359. return pInfo->m_pClass;
  360. }
  361. WCHAR * GetPath( int nIndex )
  362. {
  363. HRESULT hr = S_OK;
  364. OldClassInfo * pInfo = NULL;
  365. pInfo = (OldClassInfo *)m_List[nIndex];
  366. return pInfo->m_pPath;
  367. }
  368. inline long Size() { return m_List.Size(); }
  369. inline void ** List() { return m_List.GetArrayPtr(); }
  370. _OldClassList() {}
  371. ~_OldClassList(); // code elsewhere
  372. } OldClassList;
  373. typedef struct _WMIEventRequest
  374. {
  375. DWORD dwId;
  376. WCHAR wcsGuid[GUID_SIZE];
  377. WCHAR * pwcsClass;
  378. BOOL fHardCoded;
  379. CLSID gGuid;
  380. IWbemObjectSink __RPC_FAR * pHandler;
  381. IWbemServices __RPC_FAR * pServices;
  382. IWbemContext __RPC_FAR * pCtx;
  383. _WMIEventRequest();
  384. ~_WMIEventRequest();
  385. void AddPtrs( IWbemObjectSink __RPC_FAR * pHandler,IWbemServices __RPC_FAR * pServices,IWbemContext __RPC_FAR * pCtx);
  386. BOOL SetClassName( WCHAR * p ) { SAFE_DELETE_ARRAY(pwcsClass); return SUCCEEDED(AllocAndCopy( p, &pwcsClass)) ? TRUE : FALSE; }
  387. } WMIEventRequest;
  388. typedef struct _WMIHandleMap
  389. {
  390. HANDLE WMIHandle;
  391. GUID Guid;
  392. ULONG uDesiredAccess;
  393. LONG RefCount;
  394. void AddRef();
  395. LONG Release();
  396. _WMIHandleMap() { WMIHandle = 0; uDesiredAccess = 0; RefCount = 0; }
  397. ~_WMIHandleMap() { }
  398. }WMIHandleMap;
  399. typedef struct _WMIHiPerfHandleMap
  400. {
  401. WMIHANDLE WMIHandle;
  402. ULONG_PTR lHiPerfId;
  403. BOOL m_fEnumerator;
  404. CWMIProcessClass * m_pClass;
  405. IWbemHiPerfEnum * m_pEnum;
  406. _WMIHiPerfHandleMap(CWMIProcessClass * p, IWbemHiPerfEnum * pEnum);
  407. ~_WMIHiPerfHandleMap();
  408. }WMIHiPerfHandleMap;
  409. typedef struct _IDOrder
  410. {
  411. WCHAR * pwcsPropertyName;
  412. WCHAR * pwcsEmbeddedObject;
  413. long lType;
  414. int nWMISize;
  415. long lHandle;
  416. DWORD dwArraySize;
  417. BOOL fPutProperty;
  418. _IDOrder() { pwcsPropertyName = NULL; pwcsEmbeddedObject = NULL; lHandle = 0; }
  419. ~_IDOrder() { SAFE_DELETE_PTR(pwcsPropertyName); SAFE_DELETE_PTR(pwcsEmbeddedObject); }
  420. BOOL SetPropertyName( WCHAR * p ) { SAFE_DELETE_PTR(pwcsPropertyName); return SUCCEEDED(AllocAndCopy( p, &pwcsPropertyName)) ? TRUE : FALSE; }
  421. BOOL SetEmbeddedName( WCHAR * p ) { SAFE_DELETE_PTR(pwcsEmbeddedObject);return SUCCEEDED(AllocAndCopy( p, &pwcsEmbeddedObject)) ? TRUE : FALSE; }
  422. } IDOrder;
  423. ///////////////////////////////////////////////////////////////////////
  424. class CAnsiUnicode
  425. {
  426. public:
  427. CAnsiUnicode() {}
  428. ~CAnsiUnicode() {}
  429. HRESULT UnicodeToAnsi(WCHAR * pszW, char *& pAnsi);
  430. HRESULT AllocateAndConvertAnsiToUnicode(char * pstr, WCHAR *& pszW);
  431. };
  432. ///////////////////////////////////////////////////////////////////////
  433. class CAutoChangePointer
  434. {
  435. private:
  436. CWMIProcessClass * m_pTmp;
  437. CWMIProcessClass ** m_pOriginal;
  438. public:
  439. CAutoChangePointer(CWMIProcessClass ** ppOriginal, CWMIProcessClass * pNew)
  440. { m_pTmp = *ppOriginal; m_pOriginal = ppOriginal; *ppOriginal = pNew; }
  441. ~CAutoChangePointer()
  442. { *m_pOriginal = m_pTmp; }
  443. };
  444. //************************************************************************************************************
  445. //============================================================================================================
  446. //
  447. // The Common Base Classes
  448. //
  449. //============================================================================================================
  450. //************************************************************************************************************
  451. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  452. //
  453. // Common functions regarding binary mof processing & security
  454. //
  455. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  456. ///////////////////////////////////////////////////////////////////////
  457. class CHandleMap
  458. {
  459. protected:
  460. CFlexArray m_List;
  461. CCriticalSection m_HandleCs;
  462. public:
  463. CHandleMap() { m_HandleCs.Init(); }
  464. ~CHandleMap() { CloseAllOutstandingWMIHandles(); m_HandleCs.Delete();}
  465. inline CCriticalSection * GetCriticalSection() { return (CCriticalSection*)&m_HandleCs;}
  466. HRESULT Add(CLSID Guid, HANDLE hCurrent, ULONG uDesiredAccess);
  467. int ExistingHandleAlreadyExistsForThisGuidUseIt(CLSID Guid, HANDLE & hCurrentWMIHandle, BOOL & fCloseHandle, ULONG uDesiredAccess);
  468. void CloseAllOutstandingWMIHandles();
  469. int ReleaseHandle( HANDLE hCurrentWMIHandle );
  470. int GetHandle(CLSID Guid, HANDLE & hCurrentWMIHandle );
  471. };
  472. ///////////////////////////////////////////////////////////////////////
  473. class CHiPerfHandleMap : public CHandleMap
  474. {
  475. private:
  476. int m_nIndex;
  477. public:
  478. CHiPerfHandleMap() { m_nIndex = 0; }
  479. ~CHiPerfHandleMap(){CloseAndReleaseHandles();}
  480. HRESULT Delete( HANDLE & hCurrent, ULONG_PTR lHiPerfId );
  481. HRESULT Add( HANDLE hCurrent, ULONG_PTR lHiPerfId, CWMIProcessClass * p, IWbemHiPerfEnum * pEnum );
  482. HRESULT FindHandleAndGetClassPtr( HANDLE & lWMIHandle, ULONG_PTR lHiPerfId,CWMIProcessClass *& p);
  483. HRESULT GetFirstHandle(HANDLE & hCurrent,CWMIProcessClass *& p, IWbemHiPerfEnum *& pEnum);
  484. HRESULT GetNextHandle(HANDLE & hCurrent,CWMIProcessClass *& p, IWbemHiPerfEnum *& pEnum);
  485. void CloseAndReleaseHandles();
  486. };
  487. ///////////////////////////////////////////////////////////////////////
  488. class CWMI_IDOrder
  489. {
  490. private:
  491. int m_nTotal;
  492. int m_nCurrent;
  493. int m_nStartingPosition;
  494. // CWMIProcessClass * m_pObj;
  495. IWbemObjectAccess * m_pAccess;
  496. IWbemClassObject * m_pClass;
  497. IDOrder * m_pWMIDataIdList;
  498. DWORD GetSizeOfArray(LPCWSTR bProp, LONG lType);
  499. HRESULT ProcessPropertyQualifiers(LPCWSTR bstrPropName, int nMax,BOOL fHiPerf);
  500. public:
  501. // CWMI_IDOrder(CWMIProcessClass * p);
  502. CWMI_IDOrder(IWbemClassObject * p, IWbemObjectAccess * p2);
  503. ~CWMI_IDOrder();
  504. WCHAR * GetFirstID();
  505. WCHAR * GetNextID();
  506. void InitMemberVars();
  507. HRESULT ProcessMethodProperties();
  508. HRESULT GetPropertiesInIDOrder(BOOL HiPerf);
  509. inline long PropertyType() { return m_pWMIDataIdList[m_nCurrent].lType;}
  510. inline int PropertySize() { return m_pWMIDataIdList[m_nCurrent].nWMISize;}
  511. inline int ArraySize() { return m_pWMIDataIdList[m_nCurrent].dwArraySize; }
  512. inline WCHAR * PropertyName() { return m_pWMIDataIdList[m_nCurrent].pwcsPropertyName;}
  513. inline WCHAR * EmbeddedClassName() { return m_pWMIDataIdList[m_nCurrent].pwcsEmbeddedObject;}
  514. inline ULONG WMIDataId() { return m_nCurrent; }
  515. inline void SetPutProperty(BOOL fV) { m_pWMIDataIdList[m_nCurrent].fPutProperty = fV;}
  516. inline BOOL GetPutProperty(void) { return m_pWMIDataIdList[m_nCurrent].fPutProperty;}
  517. inline LONG GetPropertyHandle() { return m_pWMIDataIdList[m_nCurrent].lHandle;}
  518. };
  519. ////////////////////////////////////////////////////////////////////
  520. class CWMIManagement
  521. {
  522. protected:
  523. IWbemObjectSink __RPC_FAR * m_pHandler;
  524. IWbemServices __RPC_FAR * m_pServices;
  525. IWbemContext __RPC_FAR * m_pCtx;
  526. CHandleMap * m_pHandleMap;
  527. //======================================================
  528. // ************** PUBLIC ********************
  529. //======================================================
  530. public:
  531. CWMIManagement();
  532. ~CWMIManagement();
  533. inline IWbemObjectSink __RPC_FAR * Handler() { return m_pHandler;}
  534. inline IWbemServices __RPC_FAR * Services() { return m_pServices;}
  535. inline IWbemContext __RPC_FAR * Context() { return m_pCtx;}
  536. inline CHandleMap * HandleMap() { return m_pHandleMap;}
  537. inline void SetWMIPointers(CHandleMap * pList, IWbemServices __RPC_FAR * pServices, IWbemObjectSink __RPC_FAR * pHandler, IWbemContext __RPC_FAR *pCtx)
  538. { m_pHandleMap = pList; m_pServices = pServices; m_pHandler = pHandler; m_pCtx = pCtx; }
  539. //==========================================================
  540. // THE Event Group
  541. //==========================================================
  542. BOOL CancelWMIEventRegistration( GUID gGuid , ULONG_PTR uContext );
  543. //======================================================
  544. // Cleanup Group
  545. //======================================================
  546. void CloseAllOutstandingWMIHandles();
  547. //======================================================
  548. // Error message processing/checking access
  549. //======================================================
  550. void SendPrivilegeExtendedErrorObject(HRESULT hrToReturn,WCHAR * wcsClass);
  551. HRESULT SetErrorMessage(HRESULT hrToReturn, WCHAR * wcsClass, WCHAR * wcsMsg);
  552. HRESULT GetListOfUserPrivileges(TOKEN_PRIVILEGES *& ptPriv);
  553. void ProcessPrivileges(TOKEN_PRIVILEGES *ptPriv, SAFEARRAY *& psaPrivNotHeld, SAFEARRAY * psaPrivReq );
  554. };
  555. ////////////////////////////////////////////////////////////////////
  556. class CWMIProcessClass
  557. {
  558. protected:
  559. CWMIManagement * m_pWMI;
  560. CWMI_IDOrder * m_pCurrentProperty;
  561. WCHAR * m_pwcsClassName;
  562. IWbemClassObject * m_pClass;
  563. IWbemClassObject * m_pClassInstance;
  564. IWbemObjectAccess * m_pAccess;
  565. IWbemObjectAccess * m_pAccessInstance;
  566. CLSID m_Guid;
  567. WORD m_wHardCodedGuid;
  568. BOOL m_fHiPerf;
  569. BOOL m_fGetNewInstance;
  570. BOOL m_fInit;
  571. //=============================================
  572. // Private functions
  573. //=============================================
  574. void InitMemberVars();
  575. void ReleaseInstancePointers();
  576. HRESULT GetPropertiesInIDOrder(BOOL fHiPerf);
  577. public:
  578. CWMIProcessClass(BOOL b);
  579. ~CWMIProcessClass();
  580. enum __PropertyCategory{ EmbeddedClass = 0, // For the property categories
  581. Array = 1,
  582. Data = 2
  583. } _PropertyCategory;
  584. HRESULT Initialize();
  585. //=============================================
  586. // inline functions
  587. //=============================================
  588. inline BOOL SetHiPerf(BOOL f) { return m_fHiPerf = f;}
  589. inline BOOL GetNewInstance(BOOL f) { return m_fGetNewInstance = f;}
  590. inline CWMIManagement * WMI() { return m_pWMI;}
  591. inline void SetWMIPointers(CWMIProcessClass * p)
  592. { if( m_pWMI ){m_pWMI->SetWMIPointers(p->WMI()->HandleMap(),p->WMI()->Services(),p->WMI()->Handler(),p->WMI()->Context()); } }
  593. inline CWMIManagement * GetWMIManagementPtr() { return m_pWMI; }
  594. inline void SetHardCodedGuidType( WORD wValue ) { m_wHardCodedGuid= wValue; }
  595. inline WORD GetHardCodedGuidType() { return m_wHardCodedGuid;}
  596. inline CLSID * GuidPtr() { return &m_Guid;}
  597. inline IWbemObjectAccess * GetAccessInstancePtr() { return m_pAccessInstance; }
  598. inline WCHAR * EmbeddedClassName() { return m_pCurrentProperty->EmbeddedClassName();}
  599. inline WCHAR * FirstProperty() { return m_pCurrentProperty->GetFirstID(); }
  600. inline WCHAR * NextProperty() { return m_pCurrentProperty->GetNextID(); }
  601. inline int ArraySize() { return m_pCurrentProperty->ArraySize(); }
  602. inline int PropertySize() { return m_pCurrentProperty->PropertySize(); }
  603. inline long PropertyType() { return m_pCurrentProperty->PropertyType(); }
  604. inline WCHAR * GetClassName() { return m_pwcsClassName;}
  605. inline IWbemClassObject * ClassPtr() { return m_pClass; }
  606. inline long GetPropertyHandle() { return m_pCurrentProperty->GetPropertyHandle(); }
  607. //=============================================
  608. // Basic class manipulation
  609. //=============================================
  610. BOOL GetANewAccessInstance();
  611. BOOL GetANewInstance() ;
  612. inline BOOL ValidClass() { if( m_pClass && m_pCurrentProperty){return TRUE;} return FALSE;}
  613. int PropertyCategory();
  614. HRESULT SetClassName(WCHAR * wcsName);
  615. HRESULT SetClass(WCHAR * wcsClass);
  616. HRESULT SetClass(IWbemClassObject * pPtr);
  617. HRESULT SetAccess(IWbemObjectAccess * pPtr);
  618. HRESULT SetClassPointerOnly(IWbemClassObject * pPtr);
  619. HRESULT SetClassPointerOnly(IWbemObjectAccess * pPtr);
  620. HRESULT GetGuid(void);
  621. HRESULT SetKeyFromAccessPointer();
  622. HRESULT GetKeyFromAccessPointer(CVARIANT * v);
  623. HRESULT InitializeEmbeddedClass(CWMIProcessClass * Em );
  624. //=============================================
  625. // Property manipulation
  626. //=============================================
  627. void SetActiveProperty();
  628. HRESULT SetHiPerfProperties(LARGE_INTEGER TimeStamp) ;
  629. inline HRESULT PutPropertyInInstance(VARIANT * vToken)
  630. { return ( m_pClassInstance->Put(m_pCurrentProperty->PropertyName(), 0, vToken, NULL));}
  631. inline HRESULT GetPropertyInInstance(WCHAR * pwcsProperty,CVARIANT & vValue, LONG & lType)
  632. { return m_pClass->Get(pwcsProperty, 0, &vValue, &lType, NULL);}
  633. HRESULT GetSizeOfArray(long & lType, DWORD & dwCount, BOOL & fDynamic);
  634. //=============================================
  635. // Embedded classes
  636. //=============================================
  637. HRESULT ReadEmbeddedClassInstance( IUnknown * pUnknown, CVARIANT & v );
  638. HRESULT GetLargestDataTypeInClass(int & nSize);
  639. void SaveEmbeddedClass(CVARIANT & v);
  640. HRESULT GetSizeOfClass(DWORD & dwSize);
  641. //=============================================
  642. // Send the instance back to WBEM
  643. //=============================================
  644. inline ULONG WMIDataId() { return m_pCurrentProperty->WMIDataId();}
  645. inline void SetPutProperty(BOOL fV) { m_pCurrentProperty->SetPutProperty(fV);}
  646. inline BOOL GetPutProperty() { return m_pCurrentProperty->GetPutProperty();}
  647. HRESULT SendInstanceBack();
  648. HRESULT SetInstanceName(WCHAR * wName,BOOL);
  649. HRESULT GetInstanceName(WCHAR *& p);
  650. //=============================================
  651. // Class functions, providing access to the
  652. // properties, qualifiers in a class.
  653. // NOTE: Properties are in WMI order
  654. //=============================================
  655. HRESULT GetQualifierString( WCHAR * pwcsPropertyName, WCHAR * pwcsQualifierName,
  656. WCHAR * pwcsExternalOutputBuffer,int nSize);
  657. HRESULT GetQualifierValue( WCHAR * pwcsPropertyName, WCHAR * pwcsQualifierName, CVARIANT * vQual);
  658. HRESULT GetPrivilegesQualifer(SAFEARRAY ** psaPrivReq);
  659. //=============================================
  660. // Methods
  661. //=============================================
  662. ULONG GetMethodId(LPCWSTR bProp);
  663. HRESULT GetMethodProperties();
  664. };
  665. //=============================================================
  666. class CWMIDataBlock
  667. {
  668. protected:
  669. BOOL m_fUpdateNamespace;
  670. BOOL m_fMofHasChanged;
  671. CWMIProcessClass * m_Class;
  672. HANDLE m_hCurrentWMIHandle;
  673. BOOL m_fCloseHandle;
  674. BYTE * m_pbDataBuffer,* m_pbCurrentDataPtr,* m_pbWorkingDataPtr;
  675. DWORD m_dwDataBufferSize;
  676. ULONG * m_upNameOffsets;
  677. ULONG * m_pMaxPtr;
  678. ULONG m_ulVersion,m_uInstanceSize;
  679. int m_nCurrentInstance;
  680. int m_nTotalInstances;
  681. BOOL m_fFixedInstance;
  682. PWNODE_SINGLE_INSTANCE m_pSingleWnode;
  683. PWNODE_ALL_DATA m_pAllWnode;
  684. WNODE_HEADER* m_pHeaderWnode;
  685. DWORD m_dwCurrentAllocSize;
  686. DWORD m_dwAccumulativeSizeOfBlock;
  687. BOOL m_fMore;
  688. WCHAR m_wcsMsg[MSG_SIZE];
  689. ULONG m_uDesiredAccess;
  690. //======================================================
  691. // Initializing member variables
  692. //======================================================
  693. void InitMemberVars();
  694. HRESULT SetAllInstanceInfo();
  695. HRESULT SetSingleInstanceInfo();
  696. //=============================================
  697. // Get the data from WMI
  698. //=============================================
  699. BOOL InitializeDataPtr();
  700. void GetNextNode();
  701. BOOL ParseHeader();
  702. HRESULT ProcessArrayTypes(VARIANT & vToken,WCHAR * pwcsProperty);
  703. HRESULT WriteArrayTypes(WCHAR * pwcsProperty,CVARIANT & v);
  704. HRESULT FillInProperty();
  705. BOOL GetDataBlockReady(DWORD dwSize,BOOL );
  706. HRESULT ReAllocateBuffer(DWORD wCount);
  707. HRESULT AllocateBuffer(DWORD dwSize);
  708. inline BOOL PtrOk(ULONG * pPtr,ULONG uHowMany);
  709. int AssignNewHandleAndKeepItIfWMITellsUsTo();
  710. //===============================================
  711. // Mapping return code and dumping out
  712. // WNODE info
  713. //===============================================
  714. HRESULT MapReturnCode(ULONG uRc);
  715. HRESULT DumpWnodeInfo(char * pwcsMsg);
  716. void DumpAllWnode();
  717. void DumpSingleWnode();
  718. void DumpWnodeMsg(char * wcsMsg) ;
  719. public:
  720. CWMIDataBlock();
  721. ~CWMIDataBlock();
  722. void UpdateNamespace(BOOL fUpdate) { m_fUpdateNamespace = fUpdate;}
  723. BOOL UpdateNamespace() { return m_fUpdateNamespace; }
  724. BOOL HasMofChanged() { return m_fMofHasChanged; }
  725. inline void SetClassProcessPtr(CWMIProcessClass * Class) { m_Class = Class;}
  726. //=============================================
  727. // Open and Close WMI ... :)
  728. //=============================================
  729. void CloseAllOutstandingWMIHandles(void);
  730. HRESULT OpenWMI();
  731. HRESULT OpenWMIForBinaryMofGuid();
  732. //=============================================
  733. // Setting up and cancelling Events
  734. // Setting ptrs to the data sent by the event
  735. //=============================================
  736. HRESULT RegisterWMIEvent( WCHAR * wcsGuid, ULONG_PTR uContext, CLSID & Guid, BOOL fRegistered);
  737. HRESULT SetAllInstancePtr( PWNODE_ALL_DATA pwAllNode );
  738. //=============================================
  739. // Processing the data we got back and putting
  740. // it into WBEM
  741. //=============================================
  742. virtual HRESULT FillOutProperties()=0;
  743. HRESULT ProcessBinaryMof();
  744. HRESULT ReadWMIDataBlockAndPutIntoWbemInstance();
  745. inline BOOL MoreToProcess() { return m_fMore;}
  746. //=============================================
  747. // Embedded Class
  748. //=============================================
  749. HRESULT ProcessEmbeddedClass(CVARIANT & v);
  750. //=====================================================
  751. // The Put Instance Group
  752. //=====================================================
  753. HRESULT WriteEmbeddedClass(IUnknown * pUnknown,CVARIANT & v);
  754. HRESULT ConstructDataBlock(BOOL fInit);
  755. HRESULT WriteDataToBufferAndIfSinglePropertySubmitToWMI(BOOL fInit,BOOL fPutProperty);
  756. HRESULT SetSingleInstancePtr( PWNODE_SINGLE_INSTANCE pwSingleNode);
  757. HRESULT SetSingleItem();
  758. HRESULT PutSingleProperties();
  759. BOOL GetListOfPropertiesToPut(int nWhich, CVARIANT & vList);
  760. //=====================================================
  761. // Manipulate data in the data block
  762. //=====================================================
  763. void GetWord(WORD & wWord) ;
  764. void GetDWORD(DWORD & dwWord) ;
  765. void GetFloat(float & fFloat) ;
  766. void GetDouble(DOUBLE & dDouble) ;
  767. void GetSInt64(WCHAR * pwcsBuffer) ;
  768. void GetUInt64(WCHAR * pwcsBuffer) ;
  769. void GetQWORD(unsigned __int64 & uInt64);
  770. void GetString(WCHAR * pwcsBuffer,WORD wCount, WORD wBufferSize) ;
  771. void GetByte(BYTE & bByte) ;
  772. void SetWord(WORD wWord) ;
  773. void SetDWORD(DWORD dwWord) ;
  774. void SetFloat(float fFloat) ;
  775. void SetDouble(DOUBLE dDouble) ;
  776. void SetSInt64(__int64 Int64) ;
  777. void SetUInt64(unsigned __int64 UInt64) ;
  778. void SetString(WCHAR * pwcsBuffer,WORD wCount) ;
  779. void SetByte(byte bByte) ;
  780. void AddPadding(DWORD dwBytesToPad);
  781. BOOL CurrentPtrOk(ULONG uHowMany);
  782. HRESULT GetBufferReady(DWORD wCount);
  783. //=======================================================
  784. // Utility functions
  785. //=======================================================
  786. inline void InitDataBufferToNull() { m_dwDataBufferSize = 0; m_pbDataBuffer = NULL;}
  787. BOOL ResetMissingQualifierValue(WCHAR * pwcsProperty, SAFEARRAY *& pSafe);
  788. BOOL ResetMissingQualifierValue(WCHAR * pwcsProperty, CVARIANT & vToken);
  789. HRESULT ProcessDataBlock();
  790. int AdjustDataBlockPtr(HRESULT & hr);
  791. HRESULT ProcessNameBlock(BOOL f);
  792. //=========================================================
  793. // Binary mof processing
  794. //=========================================================
  795. HRESULT ExtractImageAndResourceName(CVARIANT & vImagePath,CVARIANT & vResourceName);
  796. HRESULT AddBinaryMof(CVARIANT & vImagePath,CVARIANT & vResourceName);
  797. HRESULT DeleteBinaryMof(CVARIANT & vImagePath,CVARIANT & vResourceName);
  798. HRESULT ProcessBinaryMofDataBlock(CVARIANT & vResourceName, WCHAR * w);
  799. //=========================================================
  800. // Cleanup
  801. //=========================================================
  802. void ResetDataBuffer();
  803. //======================================================
  804. // Error Message
  805. //======================================================
  806. inline WCHAR * GetMessage() { return m_wcsMsg; }
  807. inline void SetDesiredAccess(ULONG u) { m_uDesiredAccess = u; }
  808. };
  809. //************************************************************************************************************
  810. //============================================================================================================
  811. //
  812. // The Standard Provider Classes
  813. //
  814. //============================================================================================================
  815. //************************************************************************************************************
  816. class CProcessStandardDataBlock : public CWMIDataBlock
  817. {
  818. private:
  819. HRESULT FillOutProperties();
  820. CWMIProcessClass * m_pMethodInput;
  821. CWMIProcessClass * m_pMethodOutput;
  822. public:
  823. CProcessStandardDataBlock();
  824. ~CProcessStandardDataBlock() ;
  825. inline void SetMethodInput( CWMIProcessClass * p ) { m_pMethodInput = p;}
  826. inline void SetMethodOutput( CWMIProcessClass * p ) { m_pMethodOutput = p;}
  827. //=============================================
  828. // Getting the data to process in response to
  829. // a request for enumeration. We either get
  830. // a single guy, or a bunch of guys
  831. //=============================================
  832. ULONG GetDataBufferAndQueryAllData(DWORD dwSize);
  833. ULONG GetDataBufferAndQuerySingleInstance(DWORD dwSize,WCHAR * wcsInstanceName);
  834. HRESULT QueryAllData();
  835. HRESULT QuerySingleInstance(WCHAR * wcsInstanceName);
  836. HRESULT SetSingleInstance();
  837. //=============================================
  838. // Methods
  839. //=============================================
  840. HRESULT ProcessMethodInstanceParameters();
  841. HRESULT ExecuteMethod(ULONG MethodId, WCHAR * MethodInstanceName, ULONG InputValueBufferSize,BYTE * InputValueBuffer);
  842. //=============================================
  843. // Methods
  844. //=============================================
  845. ULONG GetMethodId(LPCWSTR bProp);
  846. HRESULT CreateOutParameterBlockForMethods();
  847. HRESULT CreateInParameterBlockForMethods( BYTE *& Buffer, ULONG & uBufferSize);
  848. HRESULT AllocateBlockForMethodOutput(DWORD & dwSize,CWMIProcessClass & Class);
  849. HRESULT GetEmbeddedClassSize(WCHAR * wcsEmbedded, DWORD & dwSize);
  850. };
  851. //************************************************************************************************************
  852. //============================================================================================================
  853. //
  854. // The Hi Performance Classes
  855. //
  856. //============================================================================================================
  857. //************************************************************************************************************
  858. class CProcessHiPerfDataBlock : public CWMIDataBlock
  859. {
  860. private:
  861. ULONG GetDataBufferAndHiPerfQueryAllData(DWORD dwSize, WMIHANDLE * List, long lHandleCount);
  862. ULONG GetDataBufferAndHiPerfQuerySingleInstance( DWORD dwSize,WMIHANDLE *List, PWCHAR * pInstances, long lHandleCount);
  863. public:
  864. CProcessHiPerfDataBlock() { m_fCloseHandle = FALSE;}
  865. ~CProcessHiPerfDataBlock() {}
  866. HRESULT HiPerfQuerySingleInstance(WMIHANDLE *List, PWCHAR * pInstances, DWORD dwInstanceNameSize, long lHandleCount);
  867. HRESULT HiPerfQueryAllData(WMIHANDLE * List,long lHandleCount);
  868. HRESULT OpenHiPerfHandle();
  869. HRESULT GetWMIHandle(HANDLE & lWMIHandle);
  870. HRESULT FillOutProperties();
  871. };
  872. #endif