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.

447 lines
10 KiB

  1. #include <wbemcli.h>
  2. #include <wmimsg.h>
  3. #include <comutl.h>
  4. #include <stdio.h>
  5. #include <arrtempl.h>
  6. #include <flexarry.h>
  7. #include <wstring.h>
  8. IWbemServices* g_pSvc;
  9. IWmiObjectAccessFactory* g_pAccessFactory;
  10. HRESULT RecurseProps( IWbemClassObject* pInst,
  11. LPCWSTR wszPropPrefix,
  12. CFlexArray& aPropHandles,
  13. CFlexArray& aEmbeddedPropHandles )
  14. {
  15. HRESULT hr;
  16. hr = pInst->BeginEnumeration( WBEM_FLAG_NONSYSTEM_ONLY );
  17. if ( FAILED(hr) )
  18. {
  19. return hr;
  20. }
  21. BSTR bstrName;
  22. VARIANT v;
  23. CIMTYPE ct;
  24. while( (hr = pInst->Next( 0, &bstrName, &v, &ct, NULL )) == S_OK )
  25. {
  26. CSysFreeMe sfm( bstrName );
  27. CClearMe cmv( &v );
  28. WString wsPropName = wszPropPrefix;
  29. wsPropName += bstrName;
  30. LPVOID pvHdl;
  31. hr = g_pAccessFactory->GetPropHandle( wsPropName, 0, &pvHdl );
  32. if ( SUCCEEDED(hr) )
  33. {
  34. if ( ct == CIM_OBJECT && V_VT(&v) == VT_UNKNOWN )
  35. {
  36. wsPropName += L".";
  37. CWbemPtr<IWbemClassObject> pEmbedded;
  38. V_UNKNOWN(&v)->QueryInterface( IID_IWbemClassObject,
  39. (void**)&pEmbedded );
  40. aEmbeddedPropHandles.Add( pvHdl );
  41. hr = RecurseProps( pEmbedded,
  42. wsPropName,
  43. aPropHandles,
  44. aEmbeddedPropHandles );
  45. }
  46. else
  47. {
  48. //
  49. // don't need to add embedded objects to the list since
  50. // we have them covered by recursing their properties.
  51. //
  52. aPropHandles.Add( pvHdl );
  53. }
  54. }
  55. if ( FAILED(hr) )
  56. {
  57. break;
  58. }
  59. }
  60. pInst->EndEnumeration();
  61. return hr;
  62. }
  63. HRESULT DeepCopyTest( IWbemClassObject* pClass,
  64. IWbemClassObject* pInstance,
  65. IWbemClassObject* pTemplate )
  66. {
  67. HRESULT hr;
  68. //
  69. // if a template, then set it on the access factory.
  70. //
  71. if ( pTemplate != NULL )
  72. {
  73. hr = g_pAccessFactory->SetObjectTemplate( pTemplate );
  74. if ( FAILED(hr) )
  75. {
  76. return hr;
  77. }
  78. }
  79. //
  80. // enumerate all the properties of this object and those of all
  81. // nested objects. As we enumerate, get the access handles.
  82. //
  83. CFlexArray aPropHandles;
  84. CFlexArray aEmbeddedPropHandles;
  85. hr = RecurseProps( pInstance, NULL, aPropHandles, aEmbeddedPropHandles );
  86. if ( FAILED(hr) )
  87. {
  88. return hr;
  89. }
  90. //
  91. // spawn a new instance to copy into
  92. //
  93. CWbemPtr<IWbemClassObject> pCopy;
  94. hr = pClass->SpawnInstance( 0, &pCopy );
  95. //
  96. // grab accessors for the original and target objects.
  97. //
  98. CWbemPtr<IWmiObjectAccess> pOrigAccess, pCopyAccess;
  99. hr = g_pAccessFactory->GetObjectAccess( &pOrigAccess );
  100. if ( FAILED(hr) )
  101. {
  102. return hr;
  103. }
  104. hr = g_pAccessFactory->GetObjectAccess( &pCopyAccess );
  105. if ( FAILED(hr) )
  106. {
  107. return hr;
  108. }
  109. hr = pOrigAccess->SetObject( pInstance );
  110. if ( FAILED(hr) )
  111. {
  112. return hr;
  113. }
  114. hr = pCopyAccess->SetObject( pCopy );
  115. if ( FAILED(hr) )
  116. {
  117. return hr;
  118. }
  119. //
  120. // first need to spawn any contained instances and set them on the new
  121. // object before we can copy the props.
  122. //
  123. for( int i=0; i < aEmbeddedPropHandles.Size(); i++ )
  124. {
  125. CPropVar v;
  126. CIMTYPE ct;
  127. hr = pOrigAccess->GetProp( aEmbeddedPropHandles[i], 0, &v, &ct );
  128. if ( FAILED(hr) )
  129. {
  130. return hr;
  131. }
  132. if ( V_VT(&v) != VT_UNKNOWN )
  133. {
  134. return WBEM_E_CRITICAL_ERROR;
  135. }
  136. //
  137. // spawn a new instance from the class of the object.
  138. //
  139. CWbemPtr<IWbemClassObject> pEmbedded;
  140. hr = V_UNKNOWN(&v)->QueryInterface( IID_IWbemClassObject,
  141. (void**)&pEmbedded );
  142. if ( FAILED(hr) )
  143. {
  144. return hr;
  145. }
  146. CPropVar vClass;
  147. hr = pEmbedded->Get( L"__CLASS", 0, &vClass, NULL, NULL );
  148. if ( FAILED(hr) )
  149. {
  150. return hr;
  151. }
  152. CWbemPtr<IWbemClassObject> pEmbeddedClass;
  153. hr = g_pSvc->GetObject( V_BSTR(&vClass),
  154. 0,
  155. NULL,
  156. &pEmbeddedClass,
  157. NULL );
  158. if ( FAILED(hr) )
  159. {
  160. return hr;
  161. }
  162. CWbemPtr<IWbemClassObject> pNewEmbedded;
  163. hr = pEmbeddedClass->SpawnInstance( 0, &pNewEmbedded );
  164. if ( FAILED(hr) )
  165. {
  166. return hr;
  167. }
  168. VARIANT vEmbedded;
  169. V_VT(&vEmbedded) = VT_UNKNOWN;
  170. V_UNKNOWN(&vEmbedded) = pNewEmbedded;
  171. hr = pCopyAccess->PutProp( aEmbeddedPropHandles[i], 0, &vEmbedded, ct);
  172. if ( FAILED(hr) )
  173. {
  174. return hr;
  175. }
  176. }
  177. hr = pCopyAccess->CommitChanges();
  178. if ( FAILED(hr) )
  179. {
  180. return hr;
  181. }
  182. //
  183. // copy all the properties to the new object.
  184. //
  185. for( int i=0; i < aPropHandles.Size(); i++ )
  186. {
  187. CPropVar v;
  188. CIMTYPE ct;
  189. hr = pOrigAccess->GetProp( aPropHandles[i], 0, &v, &ct );
  190. if ( FAILED(hr) )
  191. {
  192. return hr;
  193. }
  194. hr = pCopyAccess->PutProp( aPropHandles[i], 0, &v, ct );
  195. if ( FAILED(hr) )
  196. {
  197. return hr;
  198. }
  199. }
  200. hr = pCopyAccess->CommitChanges();
  201. if ( FAILED(hr) )
  202. {
  203. return hr;
  204. }
  205. //
  206. // compare objects. should be same
  207. //
  208. CWbemBSTR bsOrigText, bsCopyText;
  209. pInstance->GetObjectText( 0, &bsOrigText );
  210. pCopy->GetObjectText( 0, &bsCopyText );
  211. printf("Original instance looks like ... %S\n", bsOrigText );
  212. printf("Copied instance looks like ... %S\n", bsCopyText );
  213. hr = pCopy->CompareTo( 0, pInstance );
  214. if ( FAILED(hr) )
  215. {
  216. return hr;
  217. }
  218. else if ( hr == WBEM_S_DIFFERENT )
  219. {
  220. return WBEM_E_FAILED;
  221. }
  222. return WBEM_S_NO_ERROR;
  223. }
  224. int TestMain( LPCWSTR wszInstancePath, LPCWSTR wszTemplatePath )
  225. {
  226. HRESULT hr;
  227. CWbemPtr<IWbemLocator> pLocator;
  228. hr = CoCreateInstance( CLSID_WbemLocator,
  229. NULL,
  230. CLSCTX_SERVER,
  231. IID_IWbemLocator,
  232. (void**)&pLocator );
  233. if ( FAILED(hr) )
  234. {
  235. printf("ERROR CoCIing WbemLocator : hr = 0x%x\n",hr);
  236. return 1;
  237. }
  238. CWbemPtr<IWbemServices> pSvc;
  239. hr = pLocator->ConnectServer( L"root\\default",
  240. NULL,
  241. NULL,
  242. NULL,
  243. 0,
  244. NULL,
  245. NULL,
  246. &pSvc );
  247. if ( FAILED(hr) )
  248. {
  249. wprintf( L"ERROR Connecting to root\\default namespace : hr=0x%x\n",hr);
  250. return 1;
  251. }
  252. g_pSvc = pSvc;
  253. CWbemPtr<IWbemClassObject> pInst;
  254. hr = pSvc->GetObject( CWbemBSTR(wszInstancePath),
  255. 0,
  256. NULL,
  257. &pInst,
  258. NULL );
  259. if ( FAILED(hr) )
  260. {
  261. printf( "Failed getting test accessor instance : hr=0x%x\n", hr );
  262. return 1;
  263. }
  264. CWbemPtr<IWbemClassObject> pTemplate;
  265. if ( wszTemplatePath != NULL )
  266. {
  267. //
  268. // test object to use for template.
  269. //
  270. hr = pSvc->GetObject( CWbemBSTR(wszTemplatePath),
  271. 0,
  272. NULL,
  273. &pTemplate,
  274. NULL );
  275. if ( FAILED(hr) )
  276. {
  277. printf( "Failed getting test accessor template : hr=0x%x\n", hr );
  278. return 1;
  279. }
  280. }
  281. //
  282. // get the class object for the instance to use to spawn instances.
  283. //
  284. CPropVar vClass;
  285. CWbemPtr<IWbemClassObject> pClass;
  286. hr = pInst->Get( L"__CLASS", 0, &vClass, NULL, NULL );
  287. if ( SUCCEEDED(hr) )
  288. {
  289. hr = pSvc->GetObject( V_BSTR(&vClass), 0, NULL, &pClass, NULL );
  290. }
  291. if ( FAILED(hr) )
  292. {
  293. printf("Couldn't get class object for test accessor instance "
  294. ": hr=0x%x\n", hr );
  295. return hr;
  296. }
  297. //
  298. // create the accessor factory
  299. //
  300. CWbemPtr<IWmiObjectAccessFactory> pAccessFactory;
  301. hr = CoCreateInstance( CLSID_WmiSmartObjectAccessFactory,
  302. NULL,
  303. CLSCTX_INPROC,
  304. IID_IWmiObjectAccessFactory,
  305. (void**)&pAccessFactory );
  306. if ( FAILED(hr) )
  307. {
  308. printf("Failed CoCIing WmiSmartObjectAccessFactory. HR = 0x%x\n", hr);
  309. return 1;
  310. }
  311. g_pAccessFactory = pAccessFactory;
  312. hr = DeepCopyTest( pClass, pInst, pTemplate );
  313. if ( SUCCEEDED(hr) )
  314. {
  315. printf( "Successful Deep Copy Test for instance.\n" );
  316. }
  317. else
  318. {
  319. printf( "Failed Deep Copy Test for instance. HR=0x%x\n", hr );
  320. }
  321. return 0;
  322. }
  323. extern "C" int __cdecl wmain( int argc, WCHAR** argv )
  324. {
  325. if ( argc < 2 )
  326. {
  327. printf( "Usage: acctest <instancepath> [<templatepath>]\n" );
  328. return 1;
  329. }
  330. CoInitialize( NULL );
  331. TestMain( argv[1], argc < 3 ? NULL : argv[2] );
  332. CoUninitialize();
  333. }