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.

472 lines
11 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name :
  4. mbs.hxx
  5. Abstract:
  6. This module defines the USER-level wrapper class for access to the
  7. metabase
  8. Environment:
  9. Win32 - User Mode
  10. Project:
  11. Internet Server DLL
  12. Revision History:
  13. --*/
  14. #ifndef _MBS_HXX_
  15. #define _MBS_HXX_
  16. #if !defined( dllexp)
  17. #define dllexp __declspec( dllexport)
  18. #endif // !defined( dllexp)
  19. //
  20. // Default timeout
  21. //
  22. #define MB_TIMEOUT 5000
  23. //
  24. // Default timeout for SaveData
  25. //
  26. #define MB_SAVE_TIMEOUT 100
  27. /************************************************************
  28. * Type Definitions
  29. ************************************************************/
  30. inline
  31. DWORD
  32. HresToWin32(
  33. HRESULT hRes
  34. )
  35. /*++
  36. Routine Description:
  37. Converts an HRESULT to a Win32 error code
  38. Arguments:
  39. hRes - HRESULT to convert
  40. Return:
  41. Win32 error code
  42. --*/
  43. {
  44. return HRESULTTOWIN32(hRes);
  45. }
  46. //
  47. // Simple wrapper class around the metabase APIs
  48. //
  49. // The Metabase Interface pointer is assumed to remain valid for the lifetime
  50. // of this object.
  51. //
  52. // The character counts for paths should include the terminating '\0'.
  53. //
  54. //
  55. class MB
  56. {
  57. public:
  58. MB( IMDCOM * pMBCom )
  59. : _pMBCom( pMBCom ),
  60. _hMB ( NULL )
  61. {
  62. }
  63. ~MB( VOID )
  64. {
  65. Close();
  66. _pMBCom = NULL;
  67. }
  68. inline
  69. BOOL Open( const CHAR * pszPath,
  70. DWORD dwFlags = METADATA_PERMISSION_READ )
  71. {
  72. return Open( METADATA_MASTER_ROOT_HANDLE,
  73. pszPath,
  74. dwFlags );
  75. }
  76. inline
  77. BOOL Open( METADATA_HANDLE hOpenRoot,
  78. const CHAR * pszPath,
  79. DWORD dwFlags = METADATA_PERMISSION_READ );
  80. inline
  81. BOOL Save( VOID )
  82. {
  83. HRESULT hRes;
  84. METADATA_HANDLE mdhRoot;
  85. //
  86. // First try to lock the tree
  87. //
  88. hRes = _pMBCom->ComMDOpenMetaObjectW(METADATA_MASTER_ROOT_HANDLE,
  89. NULL,
  90. METADATA_PERMISSION_READ,
  91. MB_SAVE_TIMEOUT,
  92. &mdhRoot);
  93. //
  94. // If failed, then someone has a write handle open,
  95. // and there might be an inconsistent data state, so don't save.
  96. //
  97. if (SUCCEEDED(hRes)) {
  98. //
  99. // call metadata com api
  100. //
  101. hRes = _pMBCom->ComMDSaveData(mdhRoot);
  102. _pMBCom->ComMDCloseMetaObject(mdhRoot);
  103. }
  104. if ( SUCCEEDED( hRes ))
  105. {
  106. return TRUE;
  107. }
  108. SetLastError( HresToWin32( hRes ));
  109. return FALSE;
  110. }
  111. BOOL SetDword( const CHAR * pszPath,
  112. DWORD dwPropID,
  113. DWORD dwUserType,
  114. DWORD dwValue,
  115. DWORD dwFlags = METADATA_INHERIT )
  116. {
  117. return SetData( pszPath,
  118. dwPropID,
  119. dwUserType,
  120. DWORD_METADATA,
  121. (PVOID) &dwValue,
  122. sizeof( DWORD ),
  123. dwFlags );
  124. }
  125. BOOL SetString( const CHAR * pszPath,
  126. DWORD dwPropID,
  127. DWORD dwUserType,
  128. CHAR * pszValue,
  129. DWORD dwFlags = METADATA_INHERIT )
  130. {
  131. return SetData( pszPath,
  132. dwPropID,
  133. dwUserType,
  134. STRING_METADATA,
  135. pszValue,
  136. 0, // string length ignored for inprocess clients
  137. dwFlags );
  138. }
  139. BOOL GetDword( const CHAR * pszPath,
  140. DWORD dwPropID,
  141. DWORD dwUserType,
  142. DWORD * pdwValue,
  143. DWORD dwFlags = METADATA_INHERIT )
  144. {
  145. DWORD cb = sizeof(DWORD);
  146. return GetData( pszPath,
  147. dwPropID,
  148. dwUserType,
  149. DWORD_METADATA,
  150. pdwValue,
  151. &cb,
  152. dwFlags );
  153. }
  154. BOOL GetString( const CHAR * pszPath,
  155. DWORD dwPropID,
  156. DWORD dwUserType,
  157. CHAR * pszValue,
  158. DWORD * pcbValue,
  159. DWORD dwFlags = METADATA_INHERIT )
  160. {
  161. return GetData( pszPath,
  162. dwPropID,
  163. dwUserType,
  164. STRING_METADATA,
  165. pszValue,
  166. pcbValue,
  167. dwFlags );
  168. }
  169. BOOL GetExpandString( const CHAR * pszPath,
  170. DWORD dwPropID,
  171. DWORD dwUserType,
  172. CHAR * pszValue,
  173. DWORD * pcbValue,
  174. DWORD dwFlags = METADATA_INHERIT )
  175. {
  176. return GetData( pszPath,
  177. dwPropID,
  178. dwUserType,
  179. EXPANDSZ_METADATA,
  180. pszValue,
  181. pcbValue,
  182. dwFlags );
  183. }
  184. inline
  185. BOOL SetData( const CHAR * pszPath,
  186. DWORD dwPropID,
  187. DWORD dwUserType,
  188. DWORD dwDataType,
  189. VOID * pvData,
  190. DWORD cbData,
  191. DWORD dwFlags = METADATA_INHERIT );
  192. inline
  193. BOOL GetData( const CHAR * pszPath,
  194. DWORD dwPropID,
  195. DWORD dwUserType,
  196. DWORD dwDataType,
  197. VOID * pvData,
  198. DWORD * cbData,
  199. DWORD dwFlags = METADATA_INHERIT );
  200. BOOL EnumObjects( const CHAR * pszPath,
  201. CHAR * pszChildName,
  202. DWORD dwIndex)
  203. {
  204. HRESULT hRes;
  205. hRes = _pMBCom->ComMDEnumMetaObjects( _hMB,
  206. (LPBYTE) pszPath,
  207. (LPBYTE) pszChildName,
  208. dwIndex
  209. );
  210. if ( SUCCEEDED( hRes ))
  211. {
  212. return TRUE;
  213. }
  214. SetLastError( HresToWin32( hRes ) );
  215. return FALSE;
  216. }
  217. BOOL Close( VOID )
  218. {
  219. BOOL fRet = TRUE;
  220. if ( _hMB )
  221. {
  222. fRet = SUCCEEDED(_pMBCom->ComMDCloseMetaObject( _hMB ));
  223. _hMB = NULL;
  224. }
  225. return fRet;
  226. }
  227. METADATA_HANDLE QueryHandle( VOID ) const
  228. { return _hMB; }
  229. private:
  230. IMDCOM * _pMBCom;
  231. METADATA_HANDLE _hMB;
  232. };
  233. inline
  234. BOOL
  235. MB::Open(
  236. METADATA_HANDLE hOpenRoot,
  237. const CHAR * pszPath,
  238. DWORD dwFlags
  239. )
  240. /*++
  241. Routine Description:
  242. Opens the metabase
  243. Arguments:
  244. hOpenRoot - Relative root or METADATA_MASTER_ROOT_HANDLE
  245. pszPath - Path to open
  246. dwFlags - Open flags
  247. Return:
  248. TRUE if success, FALSE on error, (call GetLastError())
  249. --*/
  250. {
  251. HRESULT hRes;
  252. hRes = _pMBCom->ComMDOpenMetaObject( hOpenRoot,
  253. (BYTE *) pszPath,
  254. dwFlags,
  255. MB_TIMEOUT,
  256. &_hMB );
  257. if ( SUCCEEDED( hRes ))
  258. {
  259. return TRUE;
  260. }
  261. SetLastError( HresToWin32( hRes ) );
  262. return FALSE;
  263. }
  264. inline
  265. BOOL
  266. MB::SetData(
  267. const CHAR * pszPath,
  268. DWORD dwPropID,
  269. DWORD dwUserType,
  270. DWORD dwDataType,
  271. VOID * pvData,
  272. DWORD cbData,
  273. DWORD dwFlags
  274. )
  275. /*++
  276. Routine Description:
  277. Sets a metadata property on an openned metabase
  278. Arguments:
  279. pszPath - Path to set data on
  280. dwPropID - Metabase property ID
  281. dwUserType - User type for this property
  282. dwDataType - Type of data being set (dword, string etc)
  283. pvData - Pointer to data
  284. cbData - Size of data
  285. dwFlags - Inheritance flags
  286. Return:
  287. TRUE if success, FALSE on error, (call GetLastError())
  288. --*/
  289. {
  290. METADATA_RECORD mdRecord;
  291. HRESULT hRes;
  292. mdRecord.dwMDIdentifier = dwPropID;
  293. mdRecord.dwMDAttributes = dwFlags;
  294. mdRecord.dwMDUserType = dwUserType;
  295. mdRecord.dwMDDataType = dwDataType;
  296. mdRecord.dwMDDataLen = cbData;
  297. mdRecord.pbMDData = (PBYTE) pvData;
  298. hRes = _pMBCom->ComMDSetMetaData( _hMB,
  299. (LPBYTE) pszPath,
  300. &mdRecord );
  301. if ( SUCCEEDED( hRes ))
  302. {
  303. return TRUE;
  304. }
  305. SetLastError( HresToWin32( hRes ) );
  306. return FALSE;
  307. }
  308. inline
  309. BOOL
  310. MB::GetData(
  311. const CHAR * pszPath,
  312. DWORD dwPropID,
  313. DWORD dwUserType,
  314. DWORD dwDataType,
  315. VOID * pvData,
  316. DWORD * pcbData,
  317. DWORD dwFlags
  318. )
  319. /*++
  320. Routine Description:
  321. Retrieves a metadata property on an openned metabase
  322. Arguments:
  323. pszPath - Path to set data on
  324. dwPropID - Metabase property ID
  325. dwUserType - User type for this property
  326. dwDataType - Type of data being set (dword, string etc)
  327. pvData - Pointer to data
  328. pcbData - Size of pvData, receives size of object
  329. dwFlags - Inheritance flags
  330. Return:
  331. TRUE if success, FALSE on error, (call GetLastError())
  332. --*/
  333. {
  334. METADATA_RECORD mdRecord;
  335. HRESULT hRes;
  336. DWORD dwRequiredLen;
  337. mdRecord.dwMDIdentifier = dwPropID;
  338. mdRecord.dwMDAttributes = dwFlags;
  339. mdRecord.dwMDUserType = dwUserType;
  340. mdRecord.dwMDDataType = dwDataType;
  341. mdRecord.dwMDDataLen = *pcbData;
  342. mdRecord.pbMDData = (PBYTE) pvData;
  343. hRes = _pMBCom->ComMDGetMetaData( _hMB,
  344. (LPBYTE) pszPath,
  345. &mdRecord,
  346. &dwRequiredLen );
  347. if ( SUCCEEDED( hRes ))
  348. {
  349. *pcbData = mdRecord.dwMDDataLen;
  350. return TRUE;
  351. }
  352. *pcbData = dwRequiredLen;
  353. #if 0
  354. DBGPRINTF(( DBG_CONTEXT,
  355. "[MB::GetData] Failed, PropID(%d), UserType(%d) Flags(%d) on %s, hRes = 0x%08x (%d)\n",
  356. dwPropID,
  357. dwUserType,
  358. dwFlags,
  359. pszPath,
  360. hRes,
  361. HresToWin32( hRes ) ));
  362. #endif
  363. SetLastError( HresToWin32( hRes ) );
  364. return FALSE;
  365. }
  366. #endif // _MBS_HXX_