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.

559 lines
17 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. PathParse.H
  5. Abstract:
  6. Implements the default object path parser.
  7. History:
  8. a-davj 5-feb-00 Created.
  9. --*/
  10. #ifndef _PATHPARSE_H_
  11. #define _PATHPARSE_H_
  12. #include "genlex.h"
  13. //#include "opathlex.h"
  14. #include <wmiutils.h>
  15. #include <umi.h>
  16. #include "wbemcli.h"
  17. #include "flexarry.h"
  18. typedef LPVOID * PPVOID;
  19. class CSafeInCritSec
  20. {
  21. protected:
  22. CRITICAL_SECTION* m_pcs;
  23. BOOL m_bOK;
  24. public:
  25. CSafeInCritSec(CRITICAL_SECTION* pcs) : m_pcs(pcs)
  26. {
  27. m_bOK = TRUE;
  28. __try
  29. {
  30. EnterCriticalSection(m_pcs);
  31. }
  32. __except(1)
  33. {
  34. m_bOK = FALSE;
  35. };
  36. }
  37. inline ~CSafeInCritSec()
  38. {
  39. if(m_bOK)
  40. LeaveCriticalSection(m_pcs);
  41. }
  42. BOOL IsOK(){return m_bOK;};
  43. };
  44. class CRefCntCS
  45. {
  46. private:
  47. long m_lRef;
  48. DWORD m_guard1; // test
  49. CRITICAL_SECTION m_cs;
  50. DWORD m_guard2; // test
  51. HRESULT m_Status;
  52. public:
  53. CRefCntCS();
  54. ~CRefCntCS()
  55. {
  56. if(m_Status == S_OK)
  57. DeleteCriticalSection(&m_cs);
  58. m_guard1 = 0;
  59. }
  60. HRESULT GetStatus(){return m_Status;};
  61. long AddRef(void){long lRef = InterlockedIncrement(&m_lRef);return lRef;};
  62. long Release(void);
  63. CRITICAL_SECTION * GetCS(){return &m_cs;};
  64. };
  65. //***************************************************************************
  66. //
  67. // STRUCT NAME:
  68. //
  69. // CKeyRef
  70. //
  71. // DESCRIPTION:
  72. //
  73. // Holds information for a single key. Includes name, data, and data type.
  74. //
  75. //***************************************************************************
  76. struct CKeyRef
  77. {
  78. LPWSTR m_pName;
  79. DWORD m_dwType;
  80. DWORD m_dwSize;
  81. void * m_pData;
  82. CKeyRef();
  83. CKeyRef(LPCWSTR wszKeyName, DWORD dwType, DWORD dwSize, void * pData);
  84. HRESULT SetData(DWORD dwType, DWORD dwSize, void * pData);
  85. ~CKeyRef();
  86. // note that the caller is to free the returned string.
  87. LPWSTR GetValue(BOOL bQuotes=TRUE);
  88. DWORD GetValueSize();
  89. DWORD GetTotalSize();
  90. };
  91. class CUmiParsedComponent : public IUmiURLKeyList
  92. {
  93. public:
  94. CUmiParsedComponent(IWbemPathKeyList * pParent) : m_pParent(pParent){};
  95. //IUnknown members
  96. STDMETHODIMP QueryInterface(REFIID riid, PPVOID ppv)
  97. {
  98. return m_pParent->QueryInterface(riid, ppv);
  99. };
  100. STDMETHODIMP_(ULONG) AddRef(void){return m_pParent->AddRef();};
  101. STDMETHODIMP_(ULONG) Release(void){return m_pParent->Release();};
  102. HRESULT STDMETHODCALLTYPE GetCount(
  103. /* [out] */ ULONG __RPC_FAR *puKeyCount){return m_pParent->GetCount(puKeyCount);};
  104. HRESULT STDMETHODCALLTYPE SetKey(
  105. /* [string][in] */ LPCWSTR pszName,
  106. /* [string][in] */ LPCWSTR pszValue){return m_pParent->SetKey(pszName, 0, CIM_STRING,
  107. (void *)pszValue);};
  108. HRESULT STDMETHODCALLTYPE GetKey(
  109. /* [in] */ ULONG uKeyIx,
  110. /* [in] */ ULONG uFlags,
  111. /* [out][in] */ ULONG __RPC_FAR *puKeyNameBufSize,
  112. /* [in] */ LPWSTR pszKeyName,
  113. /* [out][in] */ ULONG __RPC_FAR *puValueBufSize,
  114. /* [in] */ LPWSTR pszValue);
  115. HRESULT STDMETHODCALLTYPE RemoveKey(
  116. /* [string][in] */ LPCWSTR pszName,
  117. /* [in] */ ULONG uFlags){return m_pParent->RemoveKey(pszName,0);};
  118. HRESULT STDMETHODCALLTYPE RemoveAllKeys(
  119. /* [in] */ ULONG uFlags){return m_pParent->RemoveAllKeys(0);};
  120. HRESULT STDMETHODCALLTYPE GetKeysInfo(
  121. /* [in] */ ULONG uRequestedInfo,
  122. /* [out] */ ULONGLONG __RPC_FAR *puResponse){return WBEM_E_NOT_AVAILABLE;};
  123. protected:
  124. IWbemPathKeyList * m_pParent;
  125. };
  126. //***************************************************************************
  127. //
  128. // CLASS NAME:
  129. //
  130. // CParsedComponent
  131. //
  132. // DESCRIPTION:
  133. //
  134. // Each namespace, scope and the class is represented by an instance of this. It holds
  135. // an array of CKeyRef objects and supports the IWbemPathKeyList interface.
  136. //
  137. //***************************************************************************
  138. class CParsedComponent : public IWbemPathKeyList
  139. {
  140. public:
  141. CParsedComponent(CRefCntCS *);
  142. ~CParsedComponent();
  143. friend class CDefPathParser;
  144. friend class CUmiPathParser;
  145. void ClearKeys ();
  146. HRESULT GetName(BSTR *pName);
  147. HRESULT Unparse(BSTR *pKey, bool bGetQuotes, bool bUseClassName);
  148. HRESULT GetComponentType(DWORD &dwType);
  149. BOOL AddKeyRef(CKeyRef* pAcquireRef);
  150. bool IsPossibleNamespace();
  151. bool IsInstance();
  152. HRESULT SetNS(LPCWSTR pName);
  153. //IUnknown members
  154. STDMETHODIMP QueryInterface(REFIID riid, PPVOID ppv)
  155. {
  156. *ppv=NULL;
  157. if (IID_IUnknown==riid || IID_IWbemPathKeyList==riid)
  158. *ppv=this;
  159. else if (riid == IID_IMarshal && m_pFTM)
  160. return m_pFTM->QueryInterface(riid, ppv);
  161. //postponed till Blackcomb if (IID_IUmiURLKeyList==riid)
  162. //postponed till Blackcomb *ppv = &m_UmiWrapper;
  163. if (NULL!=*ppv)
  164. {
  165. AddRef();
  166. return NOERROR;
  167. }
  168. return E_NOINTERFACE;
  169. };
  170. STDMETHODIMP_(ULONG) AddRef(void)
  171. {
  172. return InterlockedIncrement(&m_cRef);
  173. };
  174. STDMETHODIMP_(ULONG) Release(void)
  175. {
  176. long lRef = InterlockedDecrement(&m_cRef);
  177. if (0L == lRef)
  178. delete this;
  179. return lRef;
  180. };
  181. HRESULT STDMETHODCALLTYPE GetCount(
  182. /* [out] */ ULONG __RPC_FAR *puKeyCount);
  183. HRESULT STDMETHODCALLTYPE SetKey(
  184. /* [string][in] */ LPCWSTR wszName,
  185. /* [in] */ ULONG uFlags,
  186. /* [in] */ ULONG uCimType,
  187. /* [in] */ LPVOID pKeyVal);
  188. HRESULT STDMETHODCALLTYPE SetKey2(
  189. /* [string][in] */ LPCWSTR wszName,
  190. /* [in] */ ULONG uFlags,
  191. /* [in] */ ULONG uCimType,
  192. /* [in] */ VARIANT __RPC_FAR *pKeyVal);
  193. HRESULT STDMETHODCALLTYPE GetKey(
  194. /* [in] */ ULONG uKeyIx,
  195. /* [in] */ ULONG uFlags,
  196. /* [out][in] */ ULONG __RPC_FAR *puNameBufSize,
  197. /* [out][in] */ LPWSTR pszKeyName,
  198. /* [out][in] */ ULONG __RPC_FAR *puKeyValBufSize,
  199. /* [out][in] */ LPVOID pKeyVal,
  200. /* [out] */ ULONG __RPC_FAR *puApparentCimType);
  201. HRESULT STDMETHODCALLTYPE GetKey2(
  202. /* [in] */ ULONG uKeyIx,
  203. /* [in] */ ULONG uFlags,
  204. /* [out][in] */ ULONG __RPC_FAR *puNameBufSize,
  205. /* [out][in] */ LPWSTR pszKeyName,
  206. /* [out][in] */ VARIANT __RPC_FAR *pKeyValue,
  207. /* [out] */ ULONG __RPC_FAR *puApparentCimType);
  208. HRESULT STDMETHODCALLTYPE RemoveKey(
  209. /* [string][in] */ LPCWSTR wszName,
  210. /* [in] */ ULONG uFlags);
  211. HRESULT STDMETHODCALLTYPE RemoveAllKeys(
  212. /* [in] */ ULONG uFlags);
  213. HRESULT STDMETHODCALLTYPE MakeSingleton( boolean bSet);
  214. HRESULT STDMETHODCALLTYPE GetInfo(
  215. /* [in] */ ULONG uRequestedInfo,
  216. /* [out] */ ULONGLONG __RPC_FAR *puResponse);
  217. HRESULT STDMETHODCALLTYPE GetText(
  218. /* [in] */ long lFlags,
  219. /* [out][in] */ ULONG __RPC_FAR *puBuffLength,
  220. /* [string][out] */ LPWSTR pszText);
  221. private:
  222. BSTR m_sClassName;
  223. CFlexArray m_Keys;
  224. bool m_bSingleton;
  225. long m_cRef;
  226. // postponed till blackcomb CUmiParsedComponent m_UmiWrapper;
  227. CRefCntCS * m_pCS;
  228. IUnknown * m_pFTM;
  229. };
  230. //***************************************************************************
  231. //
  232. // CLASS NAME:
  233. //
  234. // CDefPathParser
  235. //
  236. // DESCRIPTION:
  237. //
  238. // Provides the default wmi path parser.
  239. //
  240. //***************************************************************************
  241. class CDefPathParser : public IWbemPath, public IUmiURL
  242. {
  243. public:
  244. CDefPathParser(void);
  245. ~CDefPathParser(void);
  246. DWORD GetNumComponents();
  247. bool IsEmpty(void);
  248. long GetNumNamespaces();
  249. void Empty(void);
  250. enum Status {UNINITIALIZED, BAD_STRING, EXECEPTION_THROWN, OK, FAILED_TO_INIT};
  251. BOOL ActualRelativeTest(LPWSTR wszMachine,
  252. LPWSTR wszNamespace,
  253. BOOL bChildrenOK);
  254. void InitEmpty(){};
  255. //IUnknown members
  256. STDMETHODIMP QueryInterface(REFIID riid, PPVOID ppv)
  257. {
  258. *ppv=NULL;
  259. if(m_dwStatus == FAILED_TO_INIT)
  260. return WBEM_E_OUT_OF_MEMORY;
  261. if(m_pCS == NULL)
  262. return E_NOINTERFACE;
  263. if (IID_IUnknown==riid || IID_IWbemPath==riid)
  264. *ppv=(IWbemPath *)this;
  265. else if (riid == IID_IMarshal && m_pFTM)
  266. return m_pFTM->QueryInterface(riid, ppv);
  267. // postponed till Blackcomb else if (IID_IUmiURL==riid)
  268. // postponed till Blackcomb *ppv=(IUmiURL *)this;
  269. if (NULL!=*ppv)
  270. {
  271. AddRef();
  272. return NOERROR;
  273. }
  274. return E_NOINTERFACE;
  275. };
  276. STDMETHODIMP_(ULONG) AddRef(void)
  277. {
  278. return InterlockedIncrement(&m_cRef);
  279. };
  280. STDMETHODIMP_(ULONG) Release(void)
  281. {
  282. long lRef = InterlockedDecrement(&m_cRef);
  283. if (0L == lRef)
  284. delete this;
  285. return lRef;
  286. };
  287. virtual HRESULT STDMETHODCALLTYPE SetText(
  288. /* [in] */ ULONG uMode,
  289. /* [in] */ LPCWSTR pszPath);
  290. virtual HRESULT STDMETHODCALLTYPE GetText(
  291. /* [in] */ long lFlags,
  292. /* [out][in] */ ULONG __RPC_FAR *puBuffLength,
  293. /* [string][out] */ LPWSTR pszText);
  294. virtual HRESULT STDMETHODCALLTYPE GetInfo(
  295. /* [in] */ ULONG uRequestedInfo,
  296. /* [out] */ ULONGLONG __RPC_FAR *puResponse);
  297. virtual HRESULT STDMETHODCALLTYPE SetServer(
  298. /* [string][in] */ LPCWSTR Name);
  299. virtual HRESULT STDMETHODCALLTYPE GetServer(
  300. /* [out][in] */ ULONG __RPC_FAR *puNameBufLength,
  301. /* [string][out] */ LPWSTR pName);
  302. virtual HRESULT STDMETHODCALLTYPE GetNamespaceCount(
  303. /* [out] */ ULONG __RPC_FAR *puCount);
  304. virtual HRESULT STDMETHODCALLTYPE SetNamespaceAt(
  305. /* [in] */ ULONG uIndex,
  306. /* [string][in] */ LPCWSTR pszName);
  307. virtual HRESULT STDMETHODCALLTYPE GetNamespaceAt(
  308. /* [in] */ ULONG uIndex,
  309. /* [out][in] */ ULONG __RPC_FAR *puNameBufLength,
  310. /* [string][out] */ LPWSTR pName);
  311. virtual HRESULT STDMETHODCALLTYPE RemoveNamespaceAt(
  312. /* [in] */ ULONG uIndex);
  313. virtual HRESULT STDMETHODCALLTYPE RemoveAllNamespaces( void);
  314. virtual HRESULT STDMETHODCALLTYPE GetScopeCount(
  315. /* [out] */ ULONG __RPC_FAR *puCount);
  316. virtual HRESULT STDMETHODCALLTYPE SetScope(
  317. unsigned long,unsigned short *);
  318. virtual HRESULT STDMETHODCALLTYPE SetScopeFromText(
  319. /* [in] */ ULONG uIndex,
  320. /* [in] */ LPWSTR pszText);
  321. virtual HRESULT STDMETHODCALLTYPE GetScope(
  322. /* [in] */ ULONG uIndex,
  323. /* [out][in] */ ULONG __RPC_FAR *puClassNameBufSize,
  324. /* [in] */ LPWSTR pszClass,
  325. /* [out] */ IWbemPathKeyList __RPC_FAR *__RPC_FAR *pKeyList);
  326. virtual HRESULT STDMETHODCALLTYPE GetScopeAsText(
  327. /* [in] */ ULONG uIndex,
  328. /* [out][in] */ ULONG __RPC_FAR *puTextBufSize,
  329. /* [out][in] */ LPWSTR pszText);
  330. virtual HRESULT STDMETHODCALLTYPE RemoveScope(
  331. /* [in] */ ULONG uIndex);
  332. virtual HRESULT STDMETHODCALLTYPE RemoveAllScopes( void);
  333. virtual HRESULT STDMETHODCALLTYPE SetClassName(
  334. /* [string][in] */ LPCWSTR Name);
  335. virtual HRESULT STDMETHODCALLTYPE GetClassName(
  336. /* [out][in] */ ULONG __RPC_FAR *puBuffLength,
  337. /* [string][out] */ LPWSTR pszName);
  338. virtual HRESULT STDMETHODCALLTYPE GetKeyList(
  339. /* [out] */ IWbemPathKeyList __RPC_FAR *__RPC_FAR *pOut);
  340. virtual HRESULT STDMETHODCALLTYPE CreateClassPart(
  341. /* [in] */ long lFlags,
  342. /* [string][in] */ LPCWSTR Name);
  343. virtual HRESULT STDMETHODCALLTYPE DeleteClassPart(
  344. /* [in] */ long lFlags);
  345. virtual BOOL STDMETHODCALLTYPE IsRelative(
  346. /* [string][in] */ LPWSTR wszMachine,
  347. /* [string][in] */ LPWSTR wszNamespace);
  348. virtual BOOL STDMETHODCALLTYPE IsRelativeOrChild(
  349. /* [string][in] */ LPWSTR wszMachine,
  350. /* [string][in] */ LPWSTR wszNamespace,
  351. /* [in] */ long lFlags);
  352. virtual BOOL STDMETHODCALLTYPE IsLocal(
  353. /* [string][in] */ LPCWSTR wszMachine);
  354. virtual BOOL STDMETHODCALLTYPE IsSameClassName(
  355. /* [string][in] */ LPCWSTR wszClass);
  356. // IUmiURL interface
  357. virtual HRESULT STDMETHODCALLTYPE Set(
  358. /* [in] */ long lFlags,
  359. /* [in] */ LPCWSTR pszText);
  360. virtual HRESULT STDMETHODCALLTYPE Get(
  361. /* [in] */ long lFlags,
  362. /* [out][in] */ ULONG __RPC_FAR *puBufSize,
  363. /* [string][in] */ LPWSTR pszDest);
  364. virtual HRESULT STDMETHODCALLTYPE GetPathInfo(
  365. /* [in] */ ULONG uRequestedInfo,
  366. /* [out] */ ULONGLONG __RPC_FAR *puResponse);
  367. virtual HRESULT STDMETHODCALLTYPE SetLocator(
  368. /* [string][in] */ LPCWSTR Name);
  369. virtual HRESULT STDMETHODCALLTYPE GetLocator(
  370. /* [out][in] */ ULONG __RPC_FAR *puNameBufLength,
  371. /* [string][in] */ LPWSTR pName);
  372. virtual HRESULT STDMETHODCALLTYPE SetRootNamespace(
  373. /* [string][in] */ LPCWSTR Name);
  374. virtual HRESULT STDMETHODCALLTYPE GetRootNamespace(
  375. /* [out][in] */ ULONG __RPC_FAR *puNameBufLength,
  376. /* [string][out][in] */ LPWSTR pName);
  377. virtual HRESULT STDMETHODCALLTYPE GetComponentCount(
  378. /* [out] */ ULONG __RPC_FAR *puCount);
  379. virtual HRESULT STDMETHODCALLTYPE SetComponent(
  380. /* [in] */ ULONG uIndex,
  381. /* [in] */ LPWSTR pszClass);
  382. virtual HRESULT STDMETHODCALLTYPE SetComponentFromText(
  383. /* [in] */ ULONG uIndex,
  384. /* [in] */ LPWSTR pszText);
  385. virtual HRESULT STDMETHODCALLTYPE GetComponent(
  386. /* [in] */ ULONG uIndex,
  387. /* [out][in] */ ULONG __RPC_FAR *puClassNameBufSize,
  388. /* [out][in] */ LPWSTR pszClass,
  389. /* [out] */ IUmiURLKeyList __RPC_FAR *__RPC_FAR *pKeyList);
  390. virtual HRESULT STDMETHODCALLTYPE GetComponentAsText(
  391. /* [in] */ ULONG uIndex,
  392. /* [out][in] */ ULONG __RPC_FAR *puTextBufSize,
  393. /* [out][in] */ LPWSTR pszText);
  394. virtual HRESULT STDMETHODCALLTYPE RemoveComponent(
  395. /* [in] */ ULONG uIndex);
  396. virtual HRESULT STDMETHODCALLTYPE RemoveAllComponents( void);
  397. virtual HRESULT STDMETHODCALLTYPE SetLeafName(
  398. /* [string][in] */ LPCWSTR Name);
  399. virtual HRESULT STDMETHODCALLTYPE GetLeafName(
  400. /* [out][in] */ ULONG __RPC_FAR *puBuffLength,
  401. /* [string][out][in] */ LPWSTR pszName);
  402. virtual HRESULT STDMETHODCALLTYPE GetKeyList(
  403. /* [out] */ IUmiURLKeyList __RPC_FAR *__RPC_FAR *pOut);
  404. virtual HRESULT STDMETHODCALLTYPE CreateLeafPart(
  405. /* [in] */ long lFlags,
  406. /* [string][in] */ LPCWSTR Name);
  407. virtual HRESULT STDMETHODCALLTYPE DeleteLeafPart(
  408. /* [in] */ long lFlags);
  409. HRESULT SetServer(LPCWSTR Name, bool m_bServerNameSetByDefault, bool bAcquire);
  410. BOOL HasServer(){return m_pServer != NULL;};
  411. LPWSTR GetPath(DWORD nStartAt, DWORD nStopAt,bool bGetServer = false);
  412. BOOL AddNamespace(LPCWSTR wszNamespace);
  413. BOOL AddClass(LPCWSTR lpClassName);
  414. BOOL AddKeyRef(CKeyRef* pAcquireRef);
  415. BOOL SetSingletonObj();
  416. LPWSTR GetNamespacePart();
  417. LPWSTR GetParentNamespacePart();
  418. BOOL SortKeys();
  419. CParsedComponent * GetLastComponent();
  420. HRESULT GetComponentString(ULONG Index, BSTR * pUnparsed, WCHAR & wDelim);
  421. HRESULT AddComponent(CParsedComponent * pComp);
  422. CParsedComponent * GetClass();
  423. CRefCntCS * GetRefCntCS(){return m_pCS;};
  424. void * m_pGenLex; // for test purposes only
  425. protected:
  426. bool m_bSetViaUMIPath;
  427. long m_cRef;
  428. LPWSTR m_pServer; // NULL if no server
  429. CFlexArray m_Components; // list of namespaces and scopes
  430. // CParsedComponent * m_pClass; // the class
  431. DWORD m_dwStatus;
  432. bool m_bParent; // true if text is ".."
  433. LPWSTR m_pRawPath; // temporary fix for Raja
  434. CRefCntCS * m_pCS;
  435. LPWSTR m_wszOriginalPath;
  436. bool m_bServerNameSetByDefault;
  437. IUnknown * m_pFTM;
  438. DWORD m_dwException;
  439. };
  440. #endif