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.

325 lines
8.8 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. CopyInstances( 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. CopyClasses(IWbemServices* pServicesSrc,
  135. IWbemServices* pServicesDest,
  136. BSTR bstrParent,
  137. BOOL bCopyInstances,
  138. BOOL* pbAbort )
  139. {
  140. HRESULT hr = S_OK;
  141. //
  142. // create an enumeration of classes
  143. //
  144. XInterface<IEnumWbemClassObject> xEnum;
  145. hr = pServicesSrc->CreateClassEnum( bstrParent,
  146. WBEM_FLAG_SHALLOW | WBEM_FLAG_FORWARD_ONLY,
  147. 0,
  148. &xEnum );
  149. ULONG ulReturned = 1;
  150. XBStr xbstrClass( L"__CLASS" );
  151. hr = *pbAbort ? E_ABORT : hr ;
  152. while ( SUCCEEDED( hr ) )
  153. {
  154. XInterface<IWbemClassObject> xClass;
  155. //
  156. // for every class
  157. //
  158. hr = xEnum->Next( -1,
  159. 1,
  160. &xClass,
  161. &ulReturned );
  162. hr = *pbAbort ? E_ABORT : hr ;
  163. if ( SUCCEEDED( hr ) && ulReturned == 1 )
  164. {
  165. VARIANT var;
  166. VariantInit( &var );
  167. //
  168. // get __CLASS system property
  169. //
  170. hr = xClass->Get( xbstrClass,
  171. 0,
  172. &var,
  173. 0,
  174. 0 );
  175. if ( SUCCEEDED( hr ) )
  176. {
  177. //
  178. // system classes begin with "_", don't copy them
  179. //
  180. if ( wcsncmp( var.bstrVal, L"_", 1 ) )
  181. {
  182. //
  183. // copy class
  184. //
  185. hr = pServicesDest->PutClass( xClass,
  186. WBEM_FLAG_CREATE_OR_UPDATE,
  187. 0,
  188. 0 );
  189. if ( SUCCEEDED( hr ) )
  190. {
  191. hr = CopyClasses( pServicesSrc,
  192. pServicesDest,
  193. var.bstrVal,
  194. bCopyInstances,
  195. pbAbort );
  196. if ( FAILED( hr ) )
  197. {
  198. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyClasses: CopyClassesSorted failed with 0x%x"), hr );
  199. }
  200. else if ( bCopyInstances )
  201. {
  202. //
  203. // copy instance
  204. //
  205. hr = CopyInstances( pServicesSrc, pServicesDest, var.bstrVal, pbAbort );
  206. if ( FAILED(hr) )
  207. {
  208. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyClasses: CopyInstances failed with 0x%x"), hr );
  209. }
  210. }
  211. }
  212. }
  213. VariantClear( &var );
  214. }
  215. }
  216. else
  217. {
  218. break;
  219. }
  220. }
  221. return hr;
  222. }
  223. HRESULT
  224. CopyNameSpace( LPCWSTR wszSrc,
  225. LPCWSTR wszDest,
  226. BOOL bCopyInstances,
  227. BOOL* pbAbort,
  228. IWbemLocator* pWbemLocator )
  229. {
  230. //
  231. // parameter validation
  232. //
  233. if ( !wszSrc || !wszDest || !pbAbort )
  234. {
  235. return E_POINTER;
  236. }
  237. BOOL bLocatorObtained = ( pWbemLocator == 0 );
  238. IWbemServices* pServicesSrc;
  239. //
  240. // get a pointer to the source namespace
  241. //
  242. HRESULT hr = GetWbemServicesPtr( wszSrc, &pWbemLocator, &pServicesSrc );
  243. hr = *pbAbort ? E_ABORT : hr ;
  244. if ( SUCCEEDED( hr ) )
  245. {
  246. XInterface<IWbemServices> xServicesSrc( pServicesSrc );
  247. IWbemServices* pServicesDest;
  248. //
  249. // get a pointer to the destination namespace
  250. //
  251. hr = GetWbemServicesPtr( wszDest, &pWbemLocator, &pServicesDest );
  252. hr = *pbAbort ? E_ABORT : hr ;
  253. if ( SUCCEEDED( hr ) )
  254. {
  255. XInterface<IWbemServices> xServicesDest( pServicesDest );
  256. //
  257. // copy classes and instances
  258. //
  259. hr = CopyClasses( pServicesSrc,
  260. pServicesDest,
  261. 0,
  262. bCopyInstances,
  263. pbAbort );
  264. if ( FAILED(hr) )
  265. {
  266. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNamespace: CopyClasses failed with 0x%x"), hr );
  267. }
  268. }
  269. }
  270. //
  271. // if we created IWbemLocator, release it
  272. //
  273. if ( bLocatorObtained && pWbemLocator )
  274. {
  275. pWbemLocator->Release();
  276. }
  277. return hr;
  278. }