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.

326 lines
8.6 KiB

  1. // Copyright (c) Microsoft. All rights reserved.
  2. //
  3. // This is unpublished source code of Microsoft.
  4. // The copyright notice above does not evidence any
  5. // actual or intended publication of such source code.
  6. // OneLiner : Implementation of MWmiInstance
  7. // DevUnit : wlbstest
  8. // Author : Murtaza Hakim
  9. // include files
  10. #include "MWmiInstance.h"
  11. #include "MTrace.h"
  12. #include "MWmiError.h"
  13. #include "WTokens.h"
  14. #include <iostream>
  15. using namespace std;
  16. // default constructor is purposely left undefined. No one should use it.
  17. // constructor
  18. //
  19. MWmiInstance::MWmiInstance(
  20. const _bstr_t& objectName,
  21. const _bstr_t& path,
  22. IWbemLocatorPtr pwl,
  23. IWbemServicesPtr pws )
  24. :
  25. _objectName( objectName ),
  26. _path( path ),
  27. _pwl( pwl ),
  28. _pws( pws )
  29. {
  30. TRACE(MTrace::INFO, L"mwmiinstance constructor\n" );
  31. }
  32. // copy constructor
  33. //
  34. MWmiInstance::MWmiInstance( const MWmiInstance& obj)
  35. :
  36. _path( obj._path ),
  37. _pwl( obj._pwl ),
  38. _pws( obj._pws ),
  39. _objectName( obj._objectName )
  40. {
  41. TRACE(MTrace::INFO, L"mwmiinstance copy constructor\n" );
  42. }
  43. // assignment operator
  44. //
  45. MWmiInstance&
  46. MWmiInstance::operator=( const MWmiInstance& rhs )
  47. {
  48. _path = rhs._path;
  49. _pwl = rhs._pwl;
  50. _pws = rhs._pws;
  51. _objectName = rhs._objectName;
  52. TRACE(MTrace::INFO, L"mwmiinstance assignment operator\n" );
  53. return *this;
  54. }
  55. // destructor
  56. MWmiInstance::~MWmiInstance()
  57. {
  58. TRACE(MTrace::INFO, L"mwmiinstance destructor\n" );
  59. }
  60. // runMethod
  61. MWmiInstance::MWmiInstance_Error
  62. MWmiInstance::runMethod(
  63. const _bstr_t& methodToRun,
  64. const vector<MWmiParameter *>& inputParameters,
  65. vector<MWmiParameter *>& outputParameters
  66. )
  67. {
  68. HRESULT hr;
  69. _variant_t v_retVal;
  70. IWbemClassObjectPtr pwcoClass;
  71. IWbemClassObjectPtr pwcoOutput;
  72. IWbemClassObjectPtr pwcoInput;
  73. IWbemClassObjectPtr pwcoInputInstance;
  74. hr = _pws->GetObject( _objectName,
  75. 0,
  76. NULL,
  77. &pwcoClass,
  78. NULL );
  79. if( FAILED(hr) )
  80. {
  81. TRACE(MTrace::SEVERE_ERROR, L"IWbemServices::GetObject failure\n");
  82. throw _com_error( hr );
  83. }
  84. // check if any input parameters specified.
  85. if( inputParameters.size() != 0 )
  86. {
  87. hr = pwcoClass->GetMethod( methodToRun,
  88. 0,
  89. &pwcoInput,
  90. NULL );
  91. if( FAILED( hr) )
  92. {
  93. TRACE(MTrace::SEVERE_ERROR, L"IWbemClassObject::GetMethod failure\n");
  94. throw _com_error( hr );
  95. }
  96. hr = pwcoInput->SpawnInstance( 0, &pwcoInputInstance );
  97. if( FAILED( hr) )
  98. {
  99. TRACE( MTrace::SEVERE_ERROR, "IWbemClassObject::SpawnInstance failure. Unable to spawn instance.\n" );
  100. throw _com_error( hr );
  101. }
  102. for( int i = 0; i < inputParameters.size(); ++i )
  103. {
  104. hr = pwcoInputInstance->Put( inputParameters[i]->getName(),
  105. 0,
  106. &(inputParameters[i]->getValue() ),
  107. 0 );
  108. if( FAILED( hr ) )
  109. {
  110. TRACE(MTrace::SEVERE_ERROR, L"IWbemClassObject::Put failure\n");
  111. throw _com_error( hr );
  112. }
  113. }
  114. }
  115. // execute method.
  116. hr = _pws->ExecMethod( _path,
  117. methodToRun,
  118. 0,
  119. NULL,
  120. pwcoInputInstance,
  121. &pwcoOutput,
  122. NULL);
  123. if( FAILED( hr) )
  124. {
  125. TRACE(MTrace::SEVERE_ERROR, L"IWbemServices::ExecMethod failure\n");
  126. throw _com_error( hr );
  127. }
  128. // get output parameters
  129. for( int i = 0; i < outputParameters.size(); ++i )
  130. {
  131. hr = pwcoOutput->Get( outputParameters[i]->getName(),
  132. 0,
  133. &v_retVal,
  134. NULL,
  135. NULL );
  136. if( FAILED( hr) )
  137. {
  138. TRACE(MTrace::SEVERE_ERROR, L"IWbemClassObject::Get failure\n");
  139. throw _com_error( hr );
  140. }
  141. outputParameters[i]->setValue( v_retVal );
  142. v_retVal.Clear();
  143. }
  144. return MWmiInstance_SUCCESS;
  145. }
  146. // getParameters
  147. //
  148. MWmiInstance::MWmiInstance_Error
  149. MWmiInstance::getParameters( vector<MWmiParameter *>& parametersToGet )
  150. {
  151. HRESULT hr;
  152. IEnumWbemClassObjectPtr pewco;
  153. IWbemClassObjectPtr pwco;
  154. unsigned long count;
  155. _variant_t v_path;
  156. bool found;
  157. CIMTYPE vtype;
  158. _variant_t v_value;
  159. // get object corresponding to this path.
  160. hr = _pws->GetObject( _path,
  161. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  162. NULL,
  163. &pwco,
  164. NULL );
  165. if( hr == 0x8004100c || hr == 0x8004100a )
  166. {
  167. TRACE(MTrace::INFO, L"as this is not supported, trying different mechanism\n");
  168. hr = _pws->CreateInstanceEnum( _objectName,
  169. WBEM_FLAG_RETURN_IMMEDIATELY,
  170. NULL,
  171. &pewco );
  172. if ( FAILED(hr))
  173. {
  174. TRACE(MTrace::SEVERE_ERROR, L"IWbemServices::CreateInstanceEnum failure\n");
  175. throw _com_error( hr );
  176. }
  177. // there may be multiple instances.
  178. count = 1;
  179. while ( (hr = pewco->Next( INFINITE,
  180. 1,
  181. &pwco,
  182. &count ) ) == S_OK )
  183. {
  184. hr = pwco->Get( _bstr_t(L"__RELPATH"), 0, &v_path, NULL, NULL );
  185. if( FAILED(hr) )
  186. {
  187. TRACE(MTrace::SEVERE_ERROR, L"IWbemServices::CreateInstanceEnum failure\n");
  188. throw _com_error( hr );
  189. }
  190. if( _bstr_t( v_path ) == _path )
  191. {
  192. // required instance found
  193. found = true;
  194. v_path.Clear();
  195. break;
  196. }
  197. count = 1;
  198. v_path.Clear();
  199. }
  200. if( found == false )
  201. {
  202. TRACE( MTrace::SEVERE_ERROR, "unable to find instance with path specified\n");
  203. throw _com_error( WBEM_E_NOT_FOUND );
  204. }
  205. }
  206. else if ( FAILED(hr) )
  207. {
  208. TRACE( MTrace::SEVERE_ERROR, L"IWbemServices::GetObject failure\n");
  209. throw _com_error( hr );
  210. }
  211. for( int i = 0; i < parametersToGet.size(); ++i )
  212. {
  213. hr = pwco->Get( parametersToGet[i]->getName(),
  214. 0,
  215. &v_value,
  216. &vtype,
  217. NULL );
  218. if( FAILED( hr ) )
  219. {
  220. TRACE( MTrace::SEVERE_ERROR, "IWbemClassObject::Get failure\n");
  221. throw _com_error(hr);
  222. }
  223. parametersToGet[i]->setValue( v_value );
  224. v_value.Clear();
  225. }
  226. return MWmiInstance_SUCCESS;
  227. }
  228. // setParameters
  229. //
  230. MWmiInstance::MWmiInstance_Error
  231. MWmiInstance::setParameters( const vector<MWmiParameter *>& parametersToSet )
  232. {
  233. HRESULT hr;
  234. IWbemClassObjectPtr pwco = NULL;
  235. _variant_t v_value;
  236. // get object corresponding to this path.
  237. hr = _pws->GetObject( _path,
  238. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  239. NULL,
  240. &pwco,
  241. NULL );
  242. if( FAILED( hr ) )
  243. {
  244. TRACE( MTrace::SEVERE_ERROR, "IWbemServices::GetObject failure\n");
  245. throw _com_error( hr );
  246. }
  247. for( int i = 0; i < parametersToSet.size(); ++i )
  248. {
  249. v_value = parametersToSet[i]->getValue();
  250. hr = pwco->Put( parametersToSet[i]->getName(),
  251. 0,
  252. &v_value,
  253. NULL );
  254. if( FAILED( hr ) )
  255. {
  256. TRACE( MTrace::SEVERE_ERROR, "IWbemClassObject::Get failure\n");
  257. throw _com_error( hr );
  258. }
  259. v_value.Clear();
  260. }
  261. hr = _pws->PutInstance( pwco,
  262. WBEM_FLAG_UPDATE_ONLY,
  263. NULL,
  264. NULL );
  265. if( FAILED(hr) )
  266. {
  267. TRACE( MTrace::SEVERE_ERROR, "IWbemServices::PutInstance failure\n");
  268. throw _com_error( hr );
  269. }
  270. return MWmiInstance_SUCCESS;
  271. }