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.

220 lines
5.4 KiB

  1. #include <wbemcli.h>
  2. #include <wmimsg.h>
  3. #include <comutl.h>
  4. #include <stdio.h>
  5. BOOL MarshalTest( IWmiObjectMarshal* pMrsh,
  6. IWmiObjectMarshal* pUnmrsh,
  7. IWbemClassObject* pObj,
  8. DWORD dwFlags )
  9. {
  10. HRESULT hr;
  11. //
  12. // pack original object into buffer
  13. //
  14. ULONG cLen;
  15. hr = pMrsh->Pack( pObj, NULL, dwFlags, 0, NULL, &cLen );
  16. if ( FAILED(hr) && hr != WBEM_E_BUFFER_TOO_SMALL )
  17. {
  18. return FALSE;
  19. }
  20. PBYTE pBuff = new BYTE[cLen];
  21. if ( pBuff == NULL )
  22. {
  23. return FALSE;
  24. }
  25. ULONG cUsed;
  26. hr = pMrsh->Pack( pObj, NULL, dwFlags, cLen, pBuff, &cUsed );
  27. if ( FAILED(hr) )
  28. {
  29. delete [] pBuff;
  30. return FALSE;
  31. }
  32. ULONG cPacked = cUsed;
  33. //
  34. // unpack buffer into new object
  35. //
  36. CWbemPtr<IWbemClassObject> pNewObj;
  37. hr = pUnmrsh->Unpack( cLen, pBuff, dwFlags, &pNewObj, &cUsed );
  38. // delete [] pBuff;
  39. if ( FAILED(hr) || cUsed != cPacked )
  40. {
  41. return FALSE;
  42. }
  43. //
  44. // compare original and new objects
  45. //
  46. hr = pNewObj->CompareTo( WBEM_FLAG_IGNORE_OBJECT_SOURCE, pObj );
  47. if ( hr != WBEM_S_SAME )
  48. {
  49. return FALSE;
  50. }
  51. delete [] pBuff;
  52. return TRUE;
  53. }
  54. int TestMain()
  55. {
  56. HRESULT hr;
  57. CWbemPtr<IWbemLocator> pLocator;
  58. hr = CoCreateInstance( CLSID_WbemLocator,
  59. NULL,
  60. CLSCTX_SERVER,
  61. IID_IWbemLocator,
  62. (void**)&pLocator );
  63. if ( FAILED(hr) )
  64. {
  65. printf("ERROR CoCIing WbemLocator : hr = 0x%x\n",hr);
  66. return 1;
  67. }
  68. CWbemPtr<IWbemServices> pSvc;
  69. hr = pLocator->ConnectServer( L"root\\default",
  70. NULL,
  71. NULL,
  72. NULL,
  73. 0,
  74. NULL,
  75. NULL,
  76. &pSvc );
  77. if ( FAILED(hr) )
  78. {
  79. wprintf( L"ERROR Connecting to root\\default namespace : hr=0x%x\n",hr);
  80. return 1;
  81. }
  82. CWbemPtr<IWbemClassObject> pClass;
  83. hr = pSvc->GetObject( CWbemBSTR(L"TestMarshalClass"),
  84. 0,
  85. NULL,
  86. &pClass,
  87. NULL );
  88. if ( FAILED(hr) )
  89. {
  90. printf( "Failed getting test marshal class : hr=0x%x\n", hr );
  91. return 1;
  92. }
  93. CWbemPtr<IWbemClassObject> pObj;
  94. hr = pSvc->GetObject( CWbemBSTR(L"TestMarshalClass='BasicObject'"),
  95. 0,
  96. NULL,
  97. &pObj,
  98. NULL );
  99. if ( FAILED(hr) )
  100. {
  101. printf( "Failed getting test marshal object : hr=0x%x\n", hr );
  102. return 1;
  103. }
  104. CWbemPtr<IWmiObjectMarshal> pMrsh, pUnmrsh;
  105. hr = CoCreateInstance( CLSID_WmiSmartObjectMarshal,
  106. NULL,
  107. CLSCTX_INPROC,
  108. IID_IWmiObjectMarshal,
  109. (void**)&pMrsh );
  110. hr = CoCreateInstance( CLSID_WmiSmartObjectUnmarshal,
  111. NULL,
  112. CLSCTX_INPROC,
  113. IID_IWmiObjectMarshal,
  114. (void**)&pUnmrsh );
  115. if ( FAILED(hr) )
  116. {
  117. printf("Failed CoCIing WmiSmartObjectMarshal. HR = 0x%x\n", hr);
  118. return 1;
  119. }
  120. if ( MarshalTest( pMrsh, pUnmrsh, pClass, WMIMSG_FLAG_MRSH_FULL ) )
  121. {
  122. printf( "Successful Marshal Test w/ Class Object and FULL Flag\n" );
  123. }
  124. else
  125. {
  126. printf( "Failed Marshal Test w/ Class Object and FULL Flag\n" );
  127. }
  128. if ( MarshalTest( pMrsh, pUnmrsh, pObj, WMIMSG_FLAG_MRSH_FULL ) )
  129. {
  130. printf( "Successful Marshal Test w/ Instance and FULL Flag\n" );
  131. }
  132. else
  133. {
  134. printf( "Failed Marshal Test w/ Instance and FULL Flag\n" );
  135. }
  136. if ( MarshalTest( pMrsh, pUnmrsh, pObj, WMIMSG_FLAG_MRSH_PARTIAL ) )
  137. {
  138. printf( "Successful Marshal Test w/ Instance and PARTIAL Flag\n" );
  139. }
  140. else
  141. {
  142. printf( "Failed Marshal Test w/ Instance and PARTIAL Flag\n" );
  143. }
  144. if ( MarshalTest( pMrsh, pUnmrsh, pObj, WMIMSG_FLAG_MRSH_PARTIAL ) )
  145. {
  146. printf( "Successful Repeated Marshal Test w/ Instance and PARTIAL Flag\n" );
  147. }
  148. else
  149. {
  150. printf( "Failed Repeated Marshal Test w/ Instance and PARTIAL Flag\n" );
  151. }
  152. if ( MarshalTest( pMrsh, pUnmrsh, pObj, WMIMSG_FLAG_MRSH_FULL_ONCE ) )
  153. {
  154. printf( "Successful Marshal Test w/ Instance and FULL_ONCE Flag\n" );
  155. }
  156. else
  157. {
  158. printf( "Failed Marshal Test w/ Instance and FULL_ONCE Flag\n" );
  159. }
  160. if ( MarshalTest( pMrsh, pUnmrsh, pObj, WMIMSG_FLAG_MRSH_FULL_ONCE ) )
  161. {
  162. printf( "Successful Repeated Marshal Test w/ Instance and FULL_ONCE Flag\n" );
  163. }
  164. else
  165. {
  166. printf( "Failed Repeated Marshal Test w/ Instance and FULL_ONCE Flag\n" );
  167. }
  168. return 0;
  169. }
  170. extern "C" int __cdecl wmain( int argc, WCHAR** argv )
  171. {
  172. CoInitialize( NULL );
  173. TestMain();
  174. CoUninitialize();
  175. }