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.

334 lines
9.5 KiB

  1. // Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  2. // JobObjectProv.h
  3. #include "precomp.h"
  4. #include <wbemprov.h>
  5. #include "FRQueryEx.h"
  6. #include "helpers.h"
  7. #include "globals.h"
  8. #include <map>
  9. #include <vector>
  10. #include "CVARIANT.h"
  11. #include "CObjProps.h"
  12. #include <crtdbg.h>
  13. #ifndef PROP_NONE_REQUIRED
  14. #define PROP_NONE_REQUIRED 0x00000000
  15. #endif
  16. CObjProps::CObjProps(CHString& chstrNamespace)
  17. {
  18. m_chstrNamespace = chstrNamespace;
  19. }
  20. CObjProps::~CObjProps()
  21. {
  22. // Clean the property map.
  23. ClearProps();
  24. }
  25. void CObjProps::ClearProps()
  26. {
  27. // Clean the property map.
  28. SHORT2PVARIANT::iterator theIterator;
  29. for(theIterator = m_PropMap.begin();
  30. theIterator != m_PropMap.end();
  31. theIterator++)
  32. {
  33. if(theIterator->second != NULL)
  34. {
  35. delete theIterator->second;
  36. }
  37. }
  38. m_PropMap.clear();
  39. }
  40. // Accessors to the requested properties member.
  41. void CObjProps::SetReqProps(DWORD dwProps)
  42. {
  43. m_dwReqProps = dwProps;
  44. }
  45. DWORD CObjProps::GetReqProps()
  46. {
  47. return m_dwReqProps;
  48. }
  49. //***************************************************************************
  50. //
  51. // Function: SetKeysFromPath
  52. //
  53. // called by the DeleteInstance and GetObject in order to load a
  54. // IWbemClassObject* with the key values in an object path.
  55. //
  56. // Inputs: IWbemClassObject* pInstance - Instance to store
  57. // key values in.
  58. // ParsedObjectPath* pParsedObjectPath - All the news
  59. // thats fit to print.
  60. // rgKeyNameArray An array of CHStrings containing
  61. // the names of the key properties.
  62. // sKeyNum An array of the key property
  63. // reference numbers.
  64. //
  65. // Outputs:
  66. //
  67. // Return: HRESULT Success/Failure
  68. //
  69. // Comments: The number of elements in rgKeyNameArray and sKeyNum must be
  70. // the same.
  71. //
  72. //***************************************************************************
  73. HRESULT CObjProps::SetKeysFromPath(
  74. const BSTR ObjectPath,
  75. IWbemContext __RPC_FAR *pCtx,
  76. LPCWSTR wstrClassName,
  77. CHStringArray& rgKeyNameArray,
  78. short sKeyNum[])
  79. {
  80. HRESULT hr = WBEM_S_NO_ERROR;
  81. _ASSERT(sKeyNum);
  82. CObjectPathParser objpathParser;
  83. ParsedObjectPath* pParsedPath = NULL;
  84. try
  85. {
  86. int iParseResult = objpathParser.Parse(
  87. ObjectPath,
  88. &pParsedPath);
  89. if(CObjectPathParser::NoError == iParseResult)
  90. {
  91. CFrameworkQueryEx cfwqe;
  92. cfwqe.Init(
  93. (ParsedObjectPath*) pParsedPath,
  94. (IWbemContext*) pCtx,
  95. wstrClassName,
  96. (CHString&) m_chstrNamespace);
  97. if(rgKeyNameArray.GetSize() == pParsedPath->m_dwNumKeys)
  98. {
  99. // populate key props...
  100. for (DWORD i = 0;
  101. SUCCEEDED(hr) && i < (pParsedPath->m_dwNumKeys);
  102. i++)
  103. {
  104. if (pParsedPath->m_paKeys[i])
  105. {
  106. // If a name was specified in the form class.keyname=value
  107. if (pParsedPath->m_paKeys[i]->m_pName != NULL)
  108. {
  109. if(_wcsicmp(pParsedPath->m_paKeys[i]->m_pName,
  110. rgKeyNameArray[i]) == 0)
  111. {
  112. // Store the value...
  113. m_PropMap.insert(SHORT2PVARIANT::value_type(
  114. sKeyNum[i],
  115. new CVARIANT(pParsedPath->m_paKeys[i]->m_vValue)));
  116. }
  117. }
  118. else
  119. {
  120. // There is a special case that you can say class=value
  121. // only one key allowed in the format. Check the names
  122. // on the path
  123. if (pParsedPath->m_dwNumKeys == 1)
  124. {
  125. // Store the value...
  126. m_PropMap.insert(SHORT2PVARIANT::value_type(
  127. sKeyNum[i],
  128. new CVARIANT(pParsedPath->m_paKeys[i]->m_vValue)));
  129. }
  130. else
  131. {
  132. hr = WBEM_E_INVALID_OBJECT_PATH;
  133. _ASSERT(0); // somebody lied about the number
  134. // of keys or the datatype was wrong
  135. }
  136. }
  137. }
  138. else
  139. {
  140. hr = WBEM_E_INVALID_OBJECT_PATH;
  141. _ASSERT(0); // somebody lied about the number of keys!
  142. }
  143. }
  144. }
  145. else
  146. {
  147. hr = WBEM_E_INVALID_OBJECT_PATH;
  148. _ASSERT(0); // somebody lied about the number of keys!
  149. }
  150. }
  151. else
  152. {
  153. hr = WBEM_E_INVALID_OBJECT_PATH;
  154. _ASSERT(0);
  155. }
  156. if (pParsedPath)
  157. {
  158. objpathParser.Free( pParsedPath );
  159. }
  160. }
  161. catch(CVARIANTError& cve)
  162. {
  163. hr = cve.GetWBEMError();
  164. if (pParsedPath)
  165. {
  166. objpathParser.Free( pParsedPath );
  167. }
  168. }
  169. catch(...)
  170. {
  171. if (pParsedPath)
  172. {
  173. objpathParser.Free( pParsedPath );
  174. }
  175. throw;
  176. }
  177. return hr;
  178. }
  179. // Allows direct setting of key properties.
  180. // Key property values are stored in vecvKeys.
  181. // sKeyNum is an array of the key property
  182. // positions in m_PropMap. The elements of
  183. // these two arrays map to each other (e.g.,
  184. // the first element in vecvKeys should be
  185. // associated with the first element in sKeyNum,
  186. // and so on).
  187. HRESULT CObjProps::SetKeysDirect(
  188. std::vector<CVARIANT>& vecvKeys,
  189. short sKeyNum[])
  190. {
  191. HRESULT hr = S_OK;
  192. UINT uiCount = vecvKeys.size();
  193. try // CVARIANT can throw and I want the error...
  194. {
  195. for (UINT u = 0; u < uiCount; u++)
  196. {
  197. // Store the value...
  198. m_PropMap.insert(SHORT2PVARIANT::value_type(
  199. sKeyNum[u],
  200. new CVARIANT(vecvKeys[u])));
  201. }
  202. }
  203. catch(CVARIANTError& cve)
  204. {
  205. hr = cve.GetWBEMError();
  206. }
  207. return hr;
  208. }
  209. HRESULT CObjProps::GetWhichPropsReq(
  210. CFrameworkQuery& cfwq,
  211. PFN_CHECK_PROPS pfnChk)
  212. {
  213. // Get the requested properties for this
  214. // specific object via derived class fn...
  215. m_dwReqProps = pfnChk(cfwq);
  216. return WBEM_S_NO_ERROR;
  217. }
  218. // Loads all properties stored in this
  219. // object into a new IWbemClassObject instance.
  220. HRESULT CObjProps::LoadPropertyValues(
  221. LPCWSTR rgwstrPropNames[],
  222. IWbemClassObject* pIWCO)
  223. {
  224. HRESULT hr = WBEM_S_NO_ERROR;
  225. if(!pIWCO) return E_POINTER;
  226. SHORT2PVARIANT::iterator theIterator;
  227. // Our map will only contain entries for properties
  228. // that were set via SetNonKeyReqProps, which only
  229. // set properties that were requested.
  230. try // CVARIANT can throw and I want the error...
  231. {
  232. for(theIterator = m_PropMap.begin();
  233. theIterator != m_PropMap.end() && SUCCEEDED(hr);
  234. theIterator++)
  235. {
  236. // Because DWORDS and ULONGLONGs are not
  237. // automation compatible types (although
  238. // they are valid CIM types!), we need
  239. // to handle those two differently. Same
  240. // with the other types special cased below.
  241. LPCWSTR wstrFoo = rgwstrPropNames[theIterator->first];
  242. CVARIANT* pvFoo = theIterator->second;
  243. if(theIterator->second->GetType() == VT_UI4)
  244. {
  245. WCHAR wstrTmp[256] = { '\0' };
  246. _ultow(theIterator->second->GetDWORD(), wstrTmp, 10);
  247. CVARIANT vTmp(wstrTmp);
  248. hr = pIWCO->Put(
  249. rgwstrPropNames[theIterator->first],
  250. 0,
  251. &vTmp,
  252. NULL);
  253. }
  254. else if(theIterator->second->GetType() == VT_UI8)
  255. {
  256. WCHAR wstrTmp[256] = { '\0' };
  257. _ui64tow(theIterator->second->GetULONGLONG(), wstrTmp, 10);
  258. CVARIANT vTmp(wstrTmp);
  259. hr = pIWCO->Put(
  260. rgwstrPropNames[theIterator->first],
  261. 0,
  262. &vTmp,
  263. NULL);
  264. }
  265. else if(theIterator->second->GetType() == VT_I8)
  266. {
  267. WCHAR wstrTmp[256] = { '\0' };
  268. _i64tow(theIterator->second->GetLONGLONG(), wstrTmp, 10);
  269. CVARIANT vTmp(wstrTmp);
  270. hr = pIWCO->Put(
  271. rgwstrPropNames[theIterator->first],
  272. 0,
  273. &vTmp,
  274. NULL);
  275. }
  276. else
  277. {
  278. hr = pIWCO->Put(
  279. rgwstrPropNames[theIterator->first],
  280. 0,
  281. *(theIterator->second),
  282. NULL);
  283. }
  284. }
  285. }
  286. catch(CVARIANTError& cve)
  287. {
  288. hr = cve.GetWBEMError();
  289. }
  290. return hr;
  291. }