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.

335 lines
8.4 KiB

  1. // Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  2. // JOBase.cpp
  3. #include "precomp.h"
  4. //#include <windows.h>
  5. //#include <cominit.h>
  6. //#include <objbase.h>
  7. //#include <comdef.h>
  8. #include "CUnknown.h"
  9. #include <wbemprov.h>
  10. #include "FRQueryEx.h"
  11. #include "globals.h"
  12. #include "CVARIANT.h"
  13. #include "CObjProps.h"
  14. #include "JOBase.h"
  15. #include "Factory.h"
  16. #include "helpers.h"
  17. #include <map>
  18. #include <vector>
  19. #include "SmartHandle.h"
  20. #include "AssertBreak.h"
  21. HRESULT CJOBase::Initialize(
  22. LPWSTR pszUser,
  23. LONG lFlags,
  24. LPWSTR pszNamespace,
  25. LPWSTR pszLocale,
  26. IWbemServices *pNamespace,
  27. IWbemContext *pCtx,
  28. IWbemProviderInitSink *pInitSink)
  29. {
  30. if(pNamespace) pNamespace->AddRef();
  31. m_pNamespace = pNamespace;
  32. m_chstrNamespace = pszNamespace;
  33. //Let CIMOM know you are initialized
  34. pInitSink->SetStatus(
  35. WBEM_S_INITIALIZED,
  36. 0);
  37. return WBEM_S_NO_ERROR;
  38. }
  39. HRESULT CJOBase::GetObjectAsync(
  40. const BSTR ObjectPath,
  41. long lFlags,
  42. IWbemContext __RPC_FAR *pCtx,
  43. IWbemObjectSink __RPC_FAR *pResponseHandler,
  44. CObjProps& objprops,
  45. PFN_CHECK_PROPS pfnChk,
  46. LPWSTR wstrClassName,
  47. LPCWSTR wstrKeyProp)
  48. {
  49. HRESULT hr = WBEM_E_NOT_FOUND;
  50. // We need the name of the instance they requested...
  51. WCHAR wstrObjInstKeyVal[MAX_PATH];
  52. hr = GetObjInstKeyVal(
  53. ObjectPath,
  54. wstrClassName,
  55. wstrKeyProp,
  56. wstrObjInstKeyVal,
  57. sizeof(wstrObjInstKeyVal) - sizeof(WCHAR));
  58. if(SUCCEEDED(hr))
  59. {
  60. // wstrObjInstKeyVal now contains the name of the object. See if
  61. // it exists...
  62. SmartHandle hJob;
  63. hJob = ::OpenJobObject(
  64. MAXIMUM_ALLOWED,
  65. FALSE,
  66. wstrObjInstKeyVal);
  67. if(hJob)
  68. {
  69. // We seem to have found one matching the specified name,
  70. // so create a return instance...
  71. objprops.SetJobHandle(hJob);
  72. IWbemClassObjectPtr pIWCO = NULL;
  73. hr = CreateInst(
  74. m_pNamespace,
  75. &pIWCO,
  76. _bstr_t(wstrClassName),
  77. pCtx);
  78. if(SUCCEEDED(hr))
  79. {
  80. // see what properties are requested...
  81. hr = objprops.GetWhichPropsReq(
  82. ObjectPath,
  83. pCtx,
  84. wstrClassName,
  85. pfnChk);
  86. }
  87. if(SUCCEEDED(hr))
  88. {
  89. // set the key properties...
  90. hr = objprops.SetKeysFromPath(
  91. ObjectPath,
  92. pCtx);
  93. }
  94. if(SUCCEEDED(hr))
  95. {
  96. // set the non-key requested properties...
  97. hr = objprops.SetNonKeyReqProps();
  98. }
  99. if(SUCCEEDED(hr))
  100. {
  101. // Load requested non-key properties
  102. // to the instance...
  103. hr = objprops.LoadPropertyValues(
  104. pIWCO);
  105. // Commit the instance...
  106. if(SUCCEEDED(hr))
  107. {
  108. hr = pResponseHandler->Indicate(
  109. 1,
  110. &pIWCO);
  111. }
  112. }
  113. }
  114. }
  115. // Set Status
  116. pResponseHandler->SetStatus(0, hr, NULL, NULL);
  117. return hr;
  118. }
  119. HRESULT CJOBase::ExecQueryAsync(
  120. const BSTR QueryLanguage,
  121. const BSTR Query,
  122. long lFlags,
  123. IWbemContext __RPC_FAR *pCtx,
  124. IWbemObjectSink __RPC_FAR *pResponseHandler,
  125. CObjProps& objprops,
  126. LPCWSTR wstrClassName,
  127. LPCWSTR wstrKeyProp)
  128. {
  129. HRESULT hr = WBEM_S_NO_ERROR;
  130. // We will optimize for those cases in which
  131. // a particular set of named job objects
  132. // (e.g., 1 or more). Enumerate also
  133. // optimizes for the properties that were
  134. // requested.
  135. CFrameworkQuery cfwq;
  136. hr = cfwq.Init(
  137. QueryLanguage,
  138. Query,
  139. lFlags,
  140. m_chstrNamespace);
  141. std::vector<_bstr_t> rgNamedJOs;
  142. if(SUCCEEDED(hr))
  143. {
  144. hr = cfwq.GetValuesForProp(
  145. _bstr_t(wstrKeyProp),
  146. rgNamedJOs);
  147. }
  148. if(SUCCEEDED(hr))
  149. {
  150. hr = Enumerate(
  151. pCtx,
  152. pResponseHandler,
  153. rgNamedJOs,
  154. objprops,
  155. wstrClassName);
  156. }
  157. return hr;
  158. }
  159. HRESULT CJOBase::CreateInstanceEnumAsync(
  160. const BSTR Class,
  161. long lFlags,
  162. IWbemContext __RPC_FAR *pCtx,
  163. IWbemObjectSink __RPC_FAR *pResponseHandler,
  164. CObjProps& objprops,
  165. LPCWSTR wstrClassName)
  166. {
  167. HRESULT hr = WBEM_S_NO_ERROR;
  168. if(wcsicmp(
  169. Class,
  170. wstrClassName) != NULL)
  171. {
  172. hr = WBEM_E_INVALID_CLASS;
  173. }
  174. // For every job object, return all accounting
  175. // info properties...
  176. if(SUCCEEDED(hr))
  177. {
  178. // Get a list of named jobs...
  179. std::vector<_bstr_t> rgNamedJOs;
  180. hr = GetJobObjectList(rgNamedJOs);
  181. if(SUCCEEDED(hr))
  182. {
  183. hr = Enumerate(
  184. pCtx,
  185. pResponseHandler,
  186. rgNamedJOs,
  187. objprops,
  188. wstrClassName);
  189. }
  190. }
  191. return hr;
  192. }
  193. HRESULT CJOBase::Enumerate(
  194. IWbemContext __RPC_FAR *pCtx,
  195. IWbemObjectSink __RPC_FAR *pResponseHandler,
  196. std::vector<_bstr_t>& rgNamedJOs,
  197. CObjProps& objprops,
  198. LPCWSTR wstrClassName)
  199. {
  200. HRESULT hr = S_OK;
  201. long lNumJobs = rgNamedJOs.size();
  202. try // CVARIANT can throw and I want the error...
  203. {
  204. if(lNumJobs > 0)
  205. {
  206. // Create an object path...
  207. _bstr_t bstrtObjPath;
  208. bstrtObjPath = wstrClassName;
  209. // Get which props requested...
  210. hr = objprops.GetWhichPropsReq(
  211. bstrtObjPath,
  212. pCtx);
  213. if(SUCCEEDED(hr))
  214. {
  215. SmartHandle hJob;
  216. for(long m = 0L; m < lNumJobs; m++)
  217. {
  218. // We have the name of a JO; need to open it up
  219. // and get its properties...
  220. hJob = ::OpenJobObject(
  221. MAXIMUM_ALLOWED,
  222. FALSE,
  223. (LPCWSTR)(rgNamedJOs[m]));
  224. // (NOTE: hJob smarthandle class automatically
  225. // closes its handle on destruction and on
  226. // reassignment.)
  227. if(hJob)
  228. {
  229. // Set the handle...
  230. objprops.SetJobHandle(hJob);
  231. // Set the key properties directly...
  232. std::vector<CVARIANT> vecvKeys;
  233. CVARIANT vID(rgNamedJOs[m]);
  234. vecvKeys.push_back(vID);
  235. hr = objprops.SetKeysDirect(vecvKeys);
  236. if(SUCCEEDED(hr))
  237. {
  238. // set the non-key requested
  239. // properties...
  240. hr = objprops.SetNonKeyReqProps();
  241. }
  242. // Create a new outgoing instance...
  243. IWbemClassObjectPtr pIWCO = NULL;
  244. if(SUCCEEDED(hr))
  245. {
  246. hr = CreateInst(
  247. m_pNamespace,
  248. &pIWCO,
  249. _bstr_t(wstrClassName),
  250. pCtx);
  251. }
  252. // Load the properties of the
  253. // new outgoing instance...
  254. if(SUCCEEDED(hr))
  255. {
  256. hr = objprops.LoadPropertyValues(pIWCO);
  257. }
  258. // And send it out...
  259. if(SUCCEEDED(hr))
  260. {
  261. hr = pResponseHandler->Indicate(
  262. 1,
  263. &pIWCO);
  264. }
  265. }
  266. else
  267. {
  268. ASSERT_BREAK(0);
  269. }
  270. }
  271. }
  272. }
  273. }
  274. catch(CVARIANTError& cve)
  275. {
  276. hr = cve.GetWBEMError();
  277. }
  278. return hr;
  279. }