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.

508 lines
14 KiB

  1. /*++
  2. Module Name:
  3. wrapmb.cpp
  4. Abstract:
  5. wrapper classes for the metabase class. Yes, I am wrapping a wrapper. Why?
  6. because including mb.hxx totally screws up the headers in my stdafx based
  7. MFC files. This way they can just include wrapmb.h and not have to worry
  8. about including all the other stuff. Also, I can set INITGUID here. That
  9. way I can use precompiled headers in the main project to Greatly increase
  10. compile times. If that isn't reason enough, then I can also manage the pointer
  11. to the interface object itself here.
  12. Author:
  13. Boyd Multerer boydm
  14. --*/
  15. //C:\nt\public\sdk\lib\i386
  16. #include "stdafx.h"
  17. /*
  18. #define INITGUID
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #include <nt.h>
  23. #include <ntrtl.h>
  24. #include <nturtl.h>
  25. #include <windows.h>
  26. #include <lm.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #ifdef __cplusplus
  30. } // extern "C"
  31. #endif // __cplusplus
  32. #include <ole2.h>
  33. #include <coguid.h>
  34. */
  35. #include <iadmw.h>
  36. #include "iiscnfg.h"
  37. #include "wrapmb.h"
  38. #ifdef _NO_TRACING_
  39. DECLARE_DEBUG_PRINTS_OBJECT();
  40. #endif
  41. #define MB_TIMEOUT 5000
  42. // a macro to automatically cast the pointer to the mb object
  43. //#define _pmb ((MB*)m_pvMB)
  44. // globals
  45. //IMSAdminBase* g_pMBCom = NULL;
  46. // $(BASEDIR)\private\iis\svcs\lib\*\isdebug.lib \
  47. // $(BASEDIR)\private\iis\svcs\lib\*\tsstr.lib \
  48. // $(BASEDIR)\private\iis\svcs\lib\*\tsres.lib
  49. //TARGETLIBS=\
  50. // ..\..\..\svcs\lib\*\isdebug.lib \
  51. // ..\..\..\svcs\lib\*\tsstr.lib
  52. //----------------------------------------------------------------
  53. IMSAdminBase* FInitMetabaseWrapper( OLECHAR* pocMachineName )
  54. {
  55. IClassFactory* pcsfFactory = NULL;
  56. COSERVERINFO csiMachineName;
  57. COSERVERINFO* pcsiParam = NULL;
  58. HRESULT hresError;
  59. IMSAdminBase* pMBCom = NULL;
  60. //fill the structure for CoGetClassObject
  61. ZeroMemory( &csiMachineName, sizeof(csiMachineName) );
  62. // csiMachineName.pAuthInfo = NULL;
  63. // csiMachineName.dwFlags = 0;
  64. // csiMachineName.pServerInfoExt = NULL;
  65. csiMachineName.pwszName = pocMachineName;
  66. pcsiParam = &csiMachineName;
  67. hresError = CoGetClassObject(GETAdminBaseCLSID(TRUE), CLSCTX_SERVER, pcsiParam,
  68. IID_IClassFactory, (void**) &pcsfFactory);
  69. if (FAILED(hresError))
  70. return FALSE;
  71. // create the instance of the interface
  72. hresError = pcsfFactory->CreateInstance(NULL, IID_IMSAdminBase, (void **)&pMBCom);
  73. // release the factory
  74. pcsfFactory->Release();
  75. // success
  76. return pMBCom;
  77. }
  78. //----------------------------------------------------------------
  79. BOOL FCloseMetabaseWrapper( IMSAdminBase* pMBCom )
  80. {
  81. if ( !pMBCom )
  82. return FALSE;
  83. if ( pMBCom )
  84. {
  85. pMBCom->Release();
  86. pMBCom = NULL;
  87. }
  88. return TRUE;
  89. }
  90. //=================================================================== The wrapper class
  91. //----------------------------------------------------------------
  92. CWrapMetaBase::CWrapMetaBase():
  93. m_pMetabase( NULL ),
  94. m_hMeta( NULL ),
  95. m_count(0),
  96. m_pBuffer( NULL ),
  97. m_cbBuffer(0),
  98. m_pPathBuffer( NULL ),
  99. m_cchPathBuffer( 0 )
  100. {
  101. // attempt to allocate the general buffer
  102. m_pBuffer = GlobalAlloc( GPTR, BUFFER_SIZE );
  103. if ( m_pBuffer )
  104. m_cbBuffer = BUFFER_SIZE;
  105. }
  106. //----------------------------------------------------------------
  107. CWrapMetaBase::~CWrapMetaBase()
  108. {
  109. // make sure the metabase handle is closed
  110. Close();
  111. // free the buffer
  112. if ( m_pBuffer )
  113. GlobalFree( m_pBuffer );
  114. m_pBuffer = NULL;
  115. }
  116. //----------------------------------------------------------------
  117. BOOL CWrapMetaBase::FInit( PVOID pMBCom )
  118. {
  119. BOOL fAnswer = FALSE;
  120. // NULL was passed in, use the global reference - most cases will do this
  121. if ( pMBCom )
  122. m_pMetabase = (IMSAdminBase*)pMBCom;
  123. // if the interface is not there, fail
  124. if ( !m_pMetabase )
  125. return FALSE;
  126. // return success
  127. return TRUE;
  128. }
  129. //==========================================================================================
  130. // open, close and save the object and such
  131. //----------------------------------------------------------------
  132. BOOL CWrapMetaBase::Open( LPCTSTR pszPath, DWORD dwFlags )
  133. {
  134. return Open( METADATA_MASTER_ROOT_HANDLE, pszPath, dwFlags );
  135. }
  136. //----------------------------------------------------------------
  137. BOOL CWrapMetaBase::Open( METADATA_HANDLE hOpenRoot, LPCTSTR pszPath, DWORD dwFlags )
  138. {
  139. m_count++;
  140. HRESULT hRes;
  141. // if a metabase handle is already open, close it
  142. if ( m_hMeta )
  143. Close();
  144. hRes = m_pMetabase->OpenKey( hOpenRoot, pszPath, dwFlags, MB_TIMEOUT, &m_hMeta );
  145. if ( SUCCEEDED( hRes ))
  146. return TRUE;
  147. SetLastError( HRESULTTOWIN32( hRes ) );
  148. return FALSE;
  149. }
  150. //----------------------------------------------------------------
  151. BOOL CWrapMetaBase::Close( void )
  152. {
  153. if ( m_hMeta )
  154. {
  155. m_count--;
  156. m_pMetabase->CloseKey( m_hMeta );
  157. }
  158. m_hMeta = NULL;
  159. return TRUE;
  160. }
  161. //----------------------------------------------------------------
  162. BOOL CWrapMetaBase::Save( void )
  163. {
  164. HRESULT hRes = m_pMetabase->SaveData();
  165. if ( SUCCEEDED( hRes ))
  166. return TRUE;
  167. SetLastError( HRESULTTOWIN32( hRes ));
  168. return FALSE;
  169. }
  170. // enumerate the objects
  171. //----------------------------------------------------------------
  172. // fortunately, we know that there is a max length to the name of any individual
  173. // key in the metabase of 256 characters
  174. BOOL CWrapMetaBase::EnumObjects( LPCTSTR pszPath, LPTSTR pName, DWORD Index )
  175. {
  176. // enumerate into the wide character buffer
  177. HRESULT hRes = m_pMetabase->EnumKeys( m_hMeta, pszPath, pName, Index );
  178. // Check for success
  179. if ( SUCCEEDED( hRes ))
  180. {
  181. return TRUE;
  182. }
  183. SetLastError( HRESULTTOWIN32( hRes ));
  184. return FALSE;
  185. }
  186. //==========================================================================================
  187. // Add and delete objects
  188. //----------------------------------------------------------------
  189. BOOL CWrapMetaBase::AddObject( LPCTSTR pszPath )
  190. {
  191. HRESULT hRes = m_pMetabase->AddKey( m_hMeta, pszPath );
  192. if ( SUCCEEDED( hRes ))
  193. return TRUE;
  194. SetLastError( HRESULTTOWIN32( hRes ));
  195. return FALSE;
  196. }
  197. //----------------------------------------------------------------
  198. BOOL CWrapMetaBase::DeleteObject( LPCTSTR pszPath )
  199. {
  200. HRESULT hRes = m_pMetabase->DeleteKey( m_hMeta, pszPath );
  201. if ( SUCCEEDED( hRes ))
  202. return TRUE;
  203. SetLastError( HRESULTTOWIN32( hRes ));
  204. return FALSE;
  205. }
  206. //==========================================================================================
  207. // access the metahandle
  208. //----------------------------------------------------------------
  209. METADATA_HANDLE CWrapMetaBase::QueryHandle()
  210. {
  211. return m_hMeta;
  212. }
  213. //==========================================================================================
  214. // setting values
  215. //----------------------------------------------------------------
  216. BOOL CWrapMetaBase::SetDword( LPCTSTR pszPath, DWORD dwPropID, DWORD dwUserType,
  217. DWORD dwValue, DWORD dwFlags )
  218. {
  219. return SetData( pszPath,
  220. dwPropID,
  221. dwUserType,
  222. DWORD_METADATA,
  223. (PVOID) &dwValue,
  224. sizeof( DWORD ),
  225. dwFlags );
  226. }
  227. //----------------------------------------------------------------
  228. BOOL CWrapMetaBase::SetString( LPCTSTR pszPath, DWORD dwPropID, DWORD dwUserType,
  229. LPCTSTR pszValue, DWORD dwFlags )
  230. {
  231. int len = wcslen( pszValue )+1;
  232. DWORD cbWide = len * sizeof(WCHAR);
  233. // set the string into place
  234. BOOL fAnswer = SetData( pszPath,
  235. dwPropID,
  236. dwUserType,
  237. STRING_METADATA,
  238. (PVOID)pszValue,
  239. cbWide, // string length ignored for inprocess clients
  240. dwFlags );
  241. // return the answer
  242. return fAnswer;
  243. }
  244. //==========================================================================================
  245. // getting values
  246. //----------------------------------------------------------------
  247. BOOL CWrapMetaBase::GetDword( LPCTSTR pszPath, DWORD dwPropID, DWORD dwUserType,
  248. DWORD* pdwValue, DWORD dwFlags )
  249. {
  250. DWORD cb = sizeof(DWORD);
  251. return GetData( pszPath,
  252. dwPropID,
  253. dwUserType,
  254. DWORD_METADATA,
  255. pdwValue,
  256. &cb,
  257. dwFlags );
  258. }
  259. //----------------------------------------------------------------
  260. BOOL CWrapMetaBase::GetString( LPCTSTR pszPath, DWORD dwPropID, DWORD dwUserType,
  261. LPTSTR pszValue, DWORD* pcchValue, DWORD dwFlags )
  262. {
  263. BOOL fAnswer = FALSE;
  264. // get the data and put it right into the buffer - this is the wide version
  265. if ( GetData( pszPath,
  266. dwPropID,
  267. dwUserType,
  268. STRING_METADATA,
  269. pszValue,
  270. pcchValue,
  271. dwFlags ) )
  272. {
  273. fAnswer = TRUE;
  274. }
  275. // return the answer
  276. return fAnswer;
  277. }
  278. //==========================================================================================
  279. // deleting values
  280. //----------------------------------------------------------------
  281. BOOL CWrapMetaBase::DeleteData( LPCTSTR pszPath, DWORD dwPropID, DWORD dwDataType )
  282. {
  283. // go right ahead and delete it
  284. HRESULT hRes = m_pMetabase->DeleteData( m_hMeta, pszPath, dwPropID, dwDataType );
  285. // test for success
  286. if ( SUCCEEDED( hRes ))
  287. return TRUE;
  288. // clean up after a failure
  289. SetLastError( HRESULTTOWIN32( hRes ));
  290. return(FALSE);
  291. }
  292. //----------------------------------------------------------------
  293. BOOL CWrapMetaBase::RenameObject( LPCTSTR pszPathOld, LPCTSTR pszNewName )
  294. {
  295. // rename the key
  296. HRESULT hRes = m_pMetabase->RenameKey( m_hMeta, pszPathOld, pszNewName );
  297. // test for success
  298. if ( SUCCEEDED( hRes ))
  299. return TRUE;
  300. // clean up after a failure
  301. SetLastError( HRESULTTOWIN32( hRes ));
  302. return FALSE;
  303. }
  304. //=====================================================================================
  305. //----------------------------------------------------------------
  306. BOOL CWrapMetaBase::SetData( LPCTSTR pszPath, DWORD dwPropID, DWORD dwUserType, DWORD dwDataType,
  307. PVOID pData, DWORD cbData, DWORD dwFlags )
  308. {
  309. METADATA_RECORD mdRecord;
  310. HRESULT hRes;
  311. // prepare the set data record
  312. mdRecord.dwMDIdentifier = dwPropID;
  313. mdRecord.dwMDAttributes = dwFlags;
  314. mdRecord.dwMDUserType = dwUserType;
  315. mdRecord.dwMDDataType = dwDataType;
  316. mdRecord.dwMDDataLen = cbData;
  317. mdRecord.pbMDData = (PBYTE)pData;
  318. // set the data
  319. hRes = m_pMetabase->SetData( m_hMeta, pszPath, &mdRecord );
  320. // test for success
  321. if ( SUCCEEDED( hRes ))
  322. return TRUE;
  323. // there was an error, clean up
  324. SetLastError( HRESULTTOWIN32( hRes ) );
  325. return FALSE;
  326. }
  327. //----------------------------------------------------------------
  328. BOOL CWrapMetaBase::GetData( LPCTSTR pszPath, DWORD dwPropID, DWORD dwUserType, DWORD dwDataType,
  329. PVOID pData, DWORD* pcbData, DWORD dwFlags )
  330. {
  331. METADATA_RECORD mdRecord;
  332. HRESULT hRes;
  333. DWORD dwRequiredLen;
  334. // prepare the get data record
  335. mdRecord.dwMDIdentifier = dwPropID;
  336. mdRecord.dwMDAttributes = dwFlags;
  337. mdRecord.dwMDUserType = dwUserType;
  338. mdRecord.dwMDDataType = dwDataType;
  339. mdRecord.dwMDDataLen = *pcbData;
  340. mdRecord.pbMDData = (PBYTE)pData;
  341. // get the data
  342. hRes = m_pMetabase->GetData( m_hMeta, pszPath, &mdRecord, &dwRequiredLen );
  343. // test for success
  344. if ( SUCCEEDED( hRes ))
  345. {
  346. *pcbData = mdRecord.dwMDDataLen;
  347. return TRUE;
  348. }
  349. // there was a failure - clean up
  350. *pcbData = dwRequiredLen;
  351. SetLastError( HRESULTTOWIN32( hRes ) );
  352. return FALSE;
  353. }
  354. //----------------------------------------------------------------
  355. // another form of GetData that automatically allocates the buffer. It should then be
  356. // freed using GlobalFree(p);
  357. PVOID CWrapMetaBase::GetData( LPCTSTR pszPath, DWORD dwPropID, DWORD dwUserType, DWORD dwDataType,
  358. DWORD* pcbData, DWORD dwFlags )
  359. {
  360. PVOID pData = m_pBuffer;
  361. DWORD cbData = m_cbBuffer;
  362. DWORD err = 0;
  363. BOOL f;
  364. // first - attempt to get the data in the buffer that has already been allocated;
  365. f = GetData( pszPath, dwPropID, dwUserType, dwDataType, pData, &cbData, dwFlags );
  366. // if the get data function worked, we can pretty much leave
  367. if ( f )
  368. {
  369. // set the data size
  370. *pcbData = cbData;
  371. // return the allocated buffer
  372. return pData;
  373. }
  374. // check the error - it could be some sort of memory error
  375. err = GetLastError();
  376. // it is ok that the GetData failed, but the reason had better be ERROR_INSUFFICIENT_BUFFER
  377. // otherwise, it is something we can't handle
  378. if ( err != ERROR_INSUFFICIENT_BUFFER )
  379. return NULL;
  380. // allocate the buffer
  381. pData = GlobalAlloc( GPTR, cbData );
  382. if ( !pData )
  383. return NULL;
  384. // first, get the size of the data that we are looking for
  385. f = GetData( pszPath, dwPropID, dwUserType, dwDataType, pData, &cbData, dwFlags );
  386. // if that getting failed, we need to cleanup
  387. if ( !f )
  388. {
  389. GlobalFree( pData );
  390. pData = NULL;
  391. }
  392. // set the data size
  393. *pcbData = cbData;
  394. // return the allocated buffer
  395. return pData;
  396. }
  397. //----------------------------------------------------------------
  398. // free memory returned by GetData
  399. void CWrapMetaBase::FreeWrapData( PVOID pData )
  400. {
  401. // if it is trying to free the local buffer, do nothing
  402. if ( pData == m_pBuffer )
  403. return;
  404. // ah - but it was not the local buffer - we should dispose of it
  405. if ( pData )
  406. GlobalFree( pData );
  407. }