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.

1198 lines
41 KiB

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