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.

427 lines
12 KiB

  1. //*************************************************************
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1998
  5. //
  6. // File: NameSpace.cpp
  7. //
  8. // Contents: Functions to copy classes and instances from one namespace to
  9. // another
  10. //
  11. // History: 25-Aug-99 NishadM Created
  12. //
  13. //*************************************************************
  14. #include <windows.h>
  15. #include <ole2.h>
  16. #include <initguid.h>
  17. #include <wbemcli.h>
  18. #include "smartptr.h"
  19. #include "RsopInc.h"
  20. #include "rsoputil.h"
  21. #include "rsopdbg.h"
  22. HRESULT
  23. GetWbemServicesPtr( LPCWSTR wszNameSpace,
  24. IWbemLocator** ppLocator,
  25. IWbemServices** ppServices )
  26. {
  27. HRESULT hr;
  28. IWbemLocator* pWbemLocator = 0;
  29. if ( !wszNameSpace || !ppLocator || !ppServices )
  30. {
  31. hr = E_INVALIDARG;
  32. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("GetWbemServicesPtr: Invalid argument" ));
  33. }
  34. else
  35. {
  36. if ( !*ppLocator )
  37. {
  38. //
  39. // get a handle to IWbemLocator
  40. //
  41. hr = CoCreateInstance( CLSID_WbemLocator,
  42. NULL,
  43. CLSCTX_INPROC_SERVER,
  44. IID_IWbemLocator,
  45. (void**) &pWbemLocator );
  46. if ( SUCCEEDED( hr ) )
  47. {
  48. *ppLocator = pWbemLocator;
  49. }
  50. else
  51. {
  52. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("GetWbemServicesPtr: CoCreateInstance failed with 0x%x"), hr );
  53. }
  54. }
  55. else
  56. {
  57. //
  58. // IWbemLocator was passed in. don't create it
  59. //
  60. pWbemLocator = *ppLocator;
  61. }
  62. }
  63. if ( pWbemLocator )
  64. {
  65. XBStr xNameSpace( (LPWSTR) wszNameSpace );
  66. if ( xNameSpace )
  67. {
  68. //
  69. // based on the name space, get a handle to IWbemServices
  70. //
  71. hr = pWbemLocator->ConnectServer( xNameSpace,
  72. 0,
  73. 0,
  74. 0L,
  75. 0L,
  76. 0,
  77. 0,
  78. ppServices );
  79. }
  80. }
  81. return hr;
  82. }
  83. HRESULT
  84. CopyClassInstances( IWbemServices* pServicesSrc,
  85. IWbemServices* pServicesDest,
  86. BSTR bstrClass,
  87. BOOL* pbAbort )
  88. {
  89. HRESULT hr;
  90. IEnumWbemClassObject* pEnum = 0;
  91. //
  92. // create an enumeration of instances
  93. //
  94. hr = pServicesSrc->CreateInstanceEnum( bstrClass,
  95. WBEM_FLAG_SHALLOW | WBEM_FLAG_FORWARD_ONLY,
  96. NULL,
  97. &pEnum );
  98. XInterface<IEnumWbemClassObject> xEnum( pEnum );
  99. ULONG ulReturned = 1;
  100. hr = *pbAbort ? E_ABORT : hr ;
  101. while ( SUCCEEDED( hr ) )
  102. {
  103. IWbemClassObject *pInstance;
  104. //
  105. // for every instance
  106. //
  107. hr = xEnum->Next( -1,
  108. 1,
  109. &pInstance,
  110. &ulReturned );
  111. //
  112. // perf: use batching calls
  113. //
  114. if ( SUCCEEDED( hr ) && ulReturned == 1 )
  115. {
  116. XInterface<IWbemClassObject> xInstance( pInstance );
  117. //
  118. // copy to the destination namespace
  119. //
  120. hr = pServicesDest->PutInstance( pInstance,
  121. WBEM_FLAG_CREATE_OR_UPDATE,
  122. 0,
  123. 0 );
  124. hr = *pbAbort ? E_ABORT : hr ;
  125. }
  126. else
  127. {
  128. break;
  129. }
  130. }
  131. return hr;
  132. }
  133. HRESULT
  134. CopyInstances(IWbemServices* pServicesSrc,
  135. IWbemServices* pServicesDest,
  136. BSTR bstrParent,
  137. BOOL* pbAbort )
  138. {
  139. HRESULT hr = S_OK;
  140. XBStr xbstrClass( L"__CLASS" );
  141. if ( !xbstrClass )
  142. {
  143. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyInstances::Failed to allocated memory" ));
  144. return E_OUTOFMEMORY;
  145. }
  146. //
  147. // create an enumeration of classes
  148. //
  149. XInterface<IEnumWbemClassObject> xEnum;
  150. hr = pServicesDest->CreateClassEnum( bstrParent,
  151. WBEM_FLAG_SHALLOW | WBEM_FLAG_FORWARD_ONLY,
  152. 0,
  153. &xEnum );
  154. ULONG ulReturned = 1;
  155. hr = *pbAbort ? E_ABORT : hr ;
  156. while ( SUCCEEDED( hr ) )
  157. {
  158. XInterface<IWbemClassObject> xClass;
  159. //
  160. // for every class
  161. //
  162. hr = xEnum->Next( -1,
  163. 1,
  164. &xClass,
  165. &ulReturned );
  166. hr = *pbAbort ? E_ABORT : hr ;
  167. if ( SUCCEEDED( hr ) && ulReturned == 1 )
  168. {
  169. VARIANT var;
  170. VariantInit( &var );
  171. //
  172. // get __CLASS system property
  173. //
  174. hr = xClass->Get( xbstrClass,
  175. 0,
  176. &var,
  177. 0,
  178. 0 );
  179. if ( SUCCEEDED( hr ) )
  180. {
  181. //
  182. // system classes begin with "_", don't copy them
  183. //
  184. if ( wcsncmp( var.bstrVal, L"_", 1 ) )
  185. {
  186. //
  187. // copy instances
  188. //
  189. hr = CopyClassInstances( pServicesSrc, pServicesDest, var.bstrVal, pbAbort );
  190. if ( SUCCEEDED( hr ) )
  191. {
  192. hr = CopyInstances( pServicesSrc,
  193. pServicesDest,
  194. var.bstrVal,
  195. pbAbort );
  196. if ( FAILED( hr ) )
  197. {
  198. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyInstances: CopyInstances failed with 0x%x"), hr );
  199. }
  200. }
  201. else
  202. {
  203. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyInstances: CopyClassInstances failed with 0x%x"), hr );
  204. }
  205. }
  206. VariantClear( &var );
  207. }
  208. }
  209. else
  210. {
  211. break;
  212. }
  213. }
  214. return hr;
  215. }
  216. HRESULT
  217. CopyClasses(IWbemServices* pServicesSrc,
  218. IWbemServices* pServicesDest,
  219. BSTR bstrParent,
  220. BOOL* pbAbort )
  221. {
  222. HRESULT hr = S_OK;
  223. XBStr xbstrClass( L"__CLASS" );
  224. if ( !xbstrClass )
  225. {
  226. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyClasses::Failed to allocated memory" ));
  227. return E_OUTOFMEMORY;
  228. }
  229. //
  230. // create an enumeration of classes
  231. //
  232. XInterface<IEnumWbemClassObject> xEnum;
  233. hr = pServicesSrc->CreateClassEnum( bstrParent,
  234. WBEM_FLAG_SHALLOW | WBEM_FLAG_FORWARD_ONLY,
  235. 0,
  236. &xEnum );
  237. ULONG ulReturned = 1;
  238. hr = *pbAbort ? E_ABORT : hr ;
  239. while ( SUCCEEDED( hr ) )
  240. {
  241. XInterface<IWbemClassObject> xClass;
  242. //
  243. // for every class
  244. //
  245. hr = xEnum->Next( -1,
  246. 1,
  247. &xClass,
  248. &ulReturned );
  249. hr = *pbAbort ? E_ABORT : hr ;
  250. if ( SUCCEEDED( hr ) && ulReturned == 1 )
  251. {
  252. VARIANT var;
  253. VariantInit( &var );
  254. //
  255. // get __CLASS system property
  256. //
  257. hr = xClass->Get( xbstrClass,
  258. 0,
  259. &var,
  260. 0,
  261. 0 );
  262. if ( SUCCEEDED( hr ) )
  263. {
  264. //
  265. // system classes begin with "_", don't copy them
  266. //
  267. if ( wcsncmp( var.bstrVal, L"_", 1 ) )
  268. {
  269. //
  270. // copy class
  271. //
  272. hr = pServicesDest->PutClass( xClass,
  273. WBEM_FLAG_CREATE_OR_UPDATE,
  274. 0,
  275. 0 );
  276. if ( SUCCEEDED( hr ) )
  277. {
  278. hr = CopyClasses( pServicesSrc,
  279. pServicesDest,
  280. var.bstrVal,
  281. pbAbort );
  282. if ( FAILED( hr ) )
  283. {
  284. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyClasses: CopyClassesSorted failed with 0x%x"), hr );
  285. }
  286. }
  287. }
  288. VariantClear( &var );
  289. }
  290. }
  291. else
  292. {
  293. break;
  294. }
  295. }
  296. return hr;
  297. }
  298. HRESULT
  299. CopyNameSpace( LPCWSTR wszSrc,
  300. LPCWSTR wszDest,
  301. BOOL bCopyInstances,
  302. BOOL* pbAbort,
  303. IWbemLocator* pWbemLocator )
  304. {
  305. //
  306. // parameter validation
  307. //
  308. if ( !wszSrc || !wszDest || !pbAbort )
  309. {
  310. return E_POINTER;
  311. }
  312. BOOL bLocatorObtained = ( pWbemLocator == 0 );
  313. IWbemServices* pServicesSrc;
  314. //
  315. // get a pointer to the source namespace
  316. //
  317. HRESULT hr = GetWbemServicesPtr( wszSrc, &pWbemLocator, &pServicesSrc );
  318. hr = *pbAbort ? E_ABORT : hr ;
  319. if ( SUCCEEDED( hr ) )
  320. {
  321. XInterface<IWbemServices> xServicesSrc( pServicesSrc );
  322. IWbemServices* pServicesDest;
  323. //
  324. // get a pointer to the destination namespace
  325. //
  326. hr = GetWbemServicesPtr( wszDest, &pWbemLocator, &pServicesDest );
  327. hr = *pbAbort ? E_ABORT : hr ;
  328. if ( SUCCEEDED( hr ) )
  329. {
  330. XInterface<IWbemServices> xServicesDest( pServicesDest );
  331. //
  332. // copy classes
  333. //
  334. hr = CopyClasses( pServicesSrc,
  335. pServicesDest,
  336. 0,
  337. pbAbort );
  338. if ( FAILED(hr) )
  339. {
  340. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNamespace: CopyClasses failed with 0x%x"), hr );
  341. }
  342. else if ( bCopyInstances )
  343. {
  344. //
  345. // now copy instances
  346. //
  347. hr = CopyInstances( pServicesSrc,
  348. pServicesDest,
  349. 0,
  350. pbAbort );
  351. if ( FAILED(hr) )
  352. {
  353. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNamespace: CopyInstances failed with 0x%x"), hr );
  354. }
  355. }
  356. }
  357. }
  358. //
  359. // if we created IWbemLocator, release it
  360. //
  361. if ( bLocatorObtained && pWbemLocator )
  362. {
  363. pWbemLocator->Release();
  364. }
  365. return hr;
  366. }