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.

492 lines
11 KiB

  1. #include <ntrkcomm.h>
  2. #include <objbase.h>
  3. CWbemServices::CWbemServices(
  4. IWbemServices* pNamespace)
  5. :m_pWbemServices(NULL)
  6. {
  7. m_pWbemServices = pNamespace;
  8. if(m_pWbemServices != NULL)
  9. m_pWbemServices->AddRef();
  10. }
  11. CWbemServices::~CWbemServices()
  12. {
  13. if(m_pWbemServices != NULL)
  14. m_pWbemServices->Release();
  15. }
  16. // CLD: 30 March 2001
  17. // This appears to be dead code. Remove and test to see if this is true.
  18. HRESULT
  19. CWbemServices::CreateClassEnum(
  20. /* [in] */ BSTR Superclass,
  21. /* [in] */ long lFlags,
  22. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  23. /* [out] */ IEnumWbemClassObject __RPC_FAR *__RPC_FAR *ppEnum
  24. )
  25. {
  26. HRESULT hr = m_pWbemServices->CreateClassEnum(
  27. Superclass,
  28. lFlags,
  29. pCtx,
  30. ppEnum);
  31. return hr;
  32. }
  33. // CLD: 30 March 2001
  34. // This appears to be dead code. Remove and test to see if this is true.
  35. HRESULT
  36. CWbemServices::CreateInstanceEnum(
  37. /* [in] */ BSTR Class,
  38. /* [in] */ long lFlags,
  39. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  40. /* [out] */ IEnumWbemClassObject __RPC_FAR *__RPC_FAR *ppEnum)
  41. {
  42. HRESULT hr = m_pWbemServices->CreateInstanceEnum(
  43. Class,
  44. lFlags,
  45. pCtx,
  46. ppEnum);
  47. return hr;
  48. }
  49. // CLD: 30 March 2001
  50. // This appears to be dead code. Remove and test to see if this is true.
  51. HRESULT
  52. CWbemServices::DeleteClass(
  53. /* [in] */ BSTR Class,
  54. /* [in] */ long lFlags,
  55. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  56. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult)
  57. {
  58. HRESULT hr = m_pWbemServices->DeleteClass(
  59. Class,
  60. lFlags,
  61. pCtx,
  62. ppCallResult);
  63. return hr;
  64. }
  65. // CLD: 30 March 2001
  66. // This appears to be dead code. Remove and test to see if this is true.
  67. HRESULT
  68. CWbemServices::DeleteInstance(
  69. /* [in] */ BSTR ObjectPath,
  70. /* [in] */ long lFlags,
  71. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  72. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult)
  73. {
  74. HRESULT hr = m_pWbemServices->DeleteInstance(
  75. ObjectPath,
  76. lFlags,
  77. pCtx,
  78. ppCallResult);
  79. return hr;
  80. }
  81. // CLD: 30 March 2001
  82. // This appears to be dead code. Remove and test to see if this is true.
  83. HRESULT
  84. CWbemServices::ExecMethod(
  85. BSTR strObjectPath,
  86. BSTR MethodName,
  87. long lFlags,
  88. IWbemContext* pCtx,
  89. IWbemClassObject* pInParams,
  90. IWbemClassObject** ppOurParams,
  91. IWbemCallResult** ppCallResult)
  92. {
  93. HRESULT hr = m_pWbemServices->ExecMethod(
  94. strObjectPath,
  95. MethodName,
  96. lFlags,
  97. pCtx,
  98. pInParams,
  99. ppOurParams,
  100. ppCallResult) ;
  101. return hr;
  102. }
  103. // CLD: 30 March 2001
  104. // This appears to be dead code. Remove and test to see if this is true.
  105. HRESULT
  106. CWbemServices::ExecNotificationQuery(
  107. /* [in] */ BSTR QueryLanguage,
  108. /* [in] */ BSTR Query,
  109. /* [in] */ long lFlags,
  110. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  111. /* [out] */ IEnumWbemClassObject __RPC_FAR *__RPC_FAR *ppEnum)
  112. {
  113. HRESULT hr = m_pWbemServices->ExecNotificationQuery(
  114. QueryLanguage,
  115. Query,
  116. lFlags,
  117. pCtx,
  118. ppEnum);
  119. return hr;
  120. }
  121. // CLD: 30 March 2001
  122. // This appears to be dead code. Remove and test to see if this is true.
  123. HRESULT
  124. CWbemServices::ExecQuery(
  125. /* [in] */ BSTR QueryLanguage,
  126. /* [in] */ BSTR Query,
  127. /* [in] */ long lFlags,
  128. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  129. /* [out] */ IEnumWbemClassObject __RPC_FAR *__RPC_FAR *ppEnum)
  130. {
  131. HRESULT hr = m_pWbemServices->ExecQuery(
  132. QueryLanguage,
  133. Query,
  134. lFlags,
  135. pCtx,
  136. ppEnum);
  137. return hr;
  138. }
  139. HRESULT
  140. CWbemServices::GetObject(
  141. /* [in] */ BSTR ObjectPath,
  142. /* [in] */ long lFlags,
  143. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  144. /* [unique][in][out] */ IWbemClassObject __RPC_FAR *__RPC_FAR *ppObject,
  145. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult)
  146. {
  147. HRESULT hr = m_pWbemServices->GetObject(
  148. ObjectPath,
  149. lFlags,
  150. pCtx,
  151. ppObject,
  152. ppCallResult);
  153. return hr;
  154. }
  155. // CLD: 30 March 2001
  156. // This appears to be dead code. Remove and test to see if this is true.
  157. HRESULT
  158. CWbemServices::PutClass(
  159. /* [in] */ IWbemClassObject __RPC_FAR *pObject,
  160. /* [in] */ long lFlags,
  161. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  162. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult)
  163. {
  164. HRESULT hr = m_pWbemServices->PutClass(
  165. pObject,
  166. lFlags,
  167. pCtx,
  168. ppCallResult);
  169. return hr;
  170. }
  171. // CLD: 30 March 2001
  172. // This appears to be dead code. Remove and test to see if this is true.
  173. HRESULT
  174. CWbemServices::PutInstance(
  175. /* [in] */ IWbemClassObject __RPC_FAR *pInst,
  176. /* [in] */ long lFlags,
  177. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  178. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult)
  179. {
  180. HRESULT hr = m_pWbemServices->PutInstance(
  181. pInst,
  182. lFlags,
  183. pCtx,
  184. ppCallResult);
  185. return hr;
  186. }
  187. /*CImpersonatedProvider
  188. * Purpose: provide a general solution for impersonate client for
  189. * Wbem providers.
  190. * USAGE:
  191. * Inherit from this class, and implement abstact virtual functions.
  192. * child class should implement function prefixed with "Do".
  193. * ******************************************/
  194. CImpersonatedProvider::CImpersonatedProvider(
  195. BSTR ObjectPath,
  196. BSTR User,
  197. BSTR Password,
  198. IWbemContext * pCtx)
  199. :m_cRef(0), m_pNamespace(NULL)
  200. {
  201. }
  202. CImpersonatedProvider::~CImpersonatedProvider()
  203. {
  204. if(m_pNamespace)
  205. delete m_pNamespace;
  206. }
  207. STDMETHODIMP_(ULONG)
  208. CImpersonatedProvider::AddRef(void)
  209. {
  210. return InterlockedIncrement((long *)&m_cRef);
  211. }
  212. STDMETHODIMP_(ULONG)
  213. CImpersonatedProvider::Release(void)
  214. {
  215. ULONG nNewCount = InterlockedDecrement((long *)&m_cRef);
  216. if (0L == nNewCount)
  217. delete this;
  218. return nNewCount;
  219. }
  220. STDMETHODIMP
  221. CImpersonatedProvider::QueryInterface(
  222. REFIID riid,
  223. PPVOID ppv)
  224. {
  225. *ppv=NULL;
  226. // Since we have dual inheritance, it is necessary to cast the return type
  227. if(riid== IID_IWbemServices)
  228. *ppv=(IWbemServices*)this;
  229. if(IID_IUnknown==riid || riid== IID_IWbemProviderInit)
  230. *ppv=(IWbemProviderInit*)this;
  231. if (NULL!=*ppv) {
  232. AddRef();
  233. return NOERROR;
  234. }
  235. else
  236. return E_NOINTERFACE;
  237. }
  238. STDMETHODIMP
  239. CImpersonatedProvider::Initialize(
  240. LPWSTR pszUser, LONG lFlags,
  241. LPWSTR pszNamespace, LPWSTR pszLocale,
  242. IWbemServices *pNamespace,
  243. IWbemContext *pCtx,
  244. IWbemProviderInitSink *pInitSink)
  245. {
  246. HRESULT hr = WBEM_S_NO_ERROR;
  247. LONG lStatus = WBEM_S_INITIALIZED;
  248. m_pNamespace = new CWbemServices(pNamespace);
  249. if(m_pNamespace == NULL)
  250. {
  251. hr = WBEM_E_OUT_OF_MEMORY;
  252. lStatus = WBEM_E_FAILED;
  253. }
  254. //Let CIMOM know you are initialized
  255. //==================================
  256. pInitSink->SetStatus(lStatus,0);
  257. return hr;
  258. }
  259. HRESULT
  260. CImpersonatedProvider::CreateInstanceEnumAsync(
  261. /* [in] */ const BSTR Class,
  262. /* [in] */ long lFlags,
  263. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  264. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler)
  265. {
  266. HRESULT hr = CoImpersonateClient();
  267. if (S_OK == hr)
  268. {
  269. hr = DoCreateInstanceEnumAsync(
  270. Class,
  271. lFlags,
  272. pCtx,
  273. pResponseHandler);
  274. }
  275. return hr;
  276. }
  277. HRESULT
  278. CImpersonatedProvider::DeleteInstanceAsync(
  279. /* [in] */ const BSTR ObjectPath,
  280. /* [in] */ long lFlags,
  281. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  282. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler)
  283. {
  284. HRESULT hr = CoImpersonateClient();
  285. if (S_OK == hr)
  286. {
  287. hr = DoDeleteInstanceAsync(
  288. ObjectPath,
  289. lFlags,
  290. pCtx,
  291. pResponseHandler);
  292. }
  293. return hr;
  294. }
  295. HRESULT
  296. CImpersonatedProvider::ExecMethodAsync(
  297. const BSTR strObjectPath,
  298. const BSTR MethodName,
  299. long lFlags,
  300. IWbemContext* pCtx,
  301. IWbemClassObject* pInParams,
  302. IWbemObjectSink* pResponseHandler)
  303. {
  304. HRESULT hr = CoImpersonateClient();
  305. if (S_OK == hr)
  306. {
  307. hr = DoExecMethodAsync(
  308. strObjectPath,
  309. MethodName,
  310. lFlags,
  311. pCtx,
  312. pInParams,
  313. pResponseHandler);
  314. }
  315. return hr;
  316. }
  317. HRESULT
  318. CImpersonatedProvider::ExecQueryAsync(
  319. /* [in] */ const BSTR QueryLanguage,
  320. /* [in] */ const BSTR Query,
  321. /* [in] */ long lFlags,
  322. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  323. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler)
  324. {
  325. HRESULT hr = CoImpersonateClient();
  326. if (S_OK == hr)
  327. {
  328. hr = DoExecQueryAsync(
  329. QueryLanguage,
  330. Query,
  331. lFlags,
  332. pCtx,
  333. pResponseHandler);
  334. }
  335. return hr;
  336. }
  337. HRESULT
  338. CImpersonatedProvider::GetObjectAsync(
  339. /* [in] */ const BSTR ObjectPath,
  340. /* [in] */ long lFlags,
  341. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  342. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler)
  343. {
  344. HRESULT hr = CoImpersonateClient();
  345. if (S_OK == hr)
  346. {
  347. hr = DoGetObjectAsync(
  348. ObjectPath,
  349. lFlags,
  350. pCtx,
  351. pResponseHandler);
  352. }
  353. return hr;
  354. }
  355. HRESULT
  356. CImpersonatedProvider::PutInstanceAsync(
  357. /* [in] */ IWbemClassObject __RPC_FAR *pInst,
  358. /* [in] */ long lFlags,
  359. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  360. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler)
  361. {
  362. HRESULT hr = CoImpersonateClient();
  363. if (S_OK == hr)
  364. {
  365. hr = DoPutInstanceAsync(
  366. pInst,
  367. lFlags,
  368. pCtx,
  369. pResponseHandler);
  370. }
  371. return hr;
  372. }
  373. // CWbemInstanceMgr
  374. CWbemInstanceMgr::CWbemInstanceMgr(
  375. IWbemObjectSink* pHandler,
  376. DWORD dwSize)
  377. :m_pSink(NULL), m_ppInst(NULL), m_dwIndex(0)
  378. {
  379. m_pSink = pHandler;
  380. if(m_pSink != NULL)
  381. m_pSink->AddRef();
  382. m_dwThreshHold = dwSize;
  383. m_ppInst = new IWbemClassObject*[dwSize];
  384. for(DWORD i = 0; i < dwSize; i++)
  385. m_ppInst[i] = NULL;
  386. }
  387. CWbemInstanceMgr::~CWbemInstanceMgr()
  388. {
  389. if(m_ppInst != NULL)
  390. {
  391. if(m_dwIndex >0)
  392. {
  393. m_pSink->Indicate(
  394. m_dwIndex,
  395. m_ppInst);
  396. }
  397. for(DWORD i =0; i<m_dwIndex; i++)
  398. {
  399. if(m_ppInst[i] != NULL)
  400. (m_ppInst[i])->Release();
  401. }
  402. delete [] m_ppInst;
  403. }
  404. if(m_pSink != NULL)
  405. m_pSink->Release();
  406. }
  407. void
  408. CWbemInstanceMgr::Indicate(IWbemClassObject* pInst)
  409. {
  410. if(pInst == NULL)
  411. throw WBEM_E_INVALID_PARAMETER;
  412. m_ppInst[m_dwIndex++] = pInst;
  413. pInst->AddRef();
  414. if(m_dwIndex == m_dwThreshHold)
  415. {
  416. SCODE sc = m_pSink->Indicate(
  417. m_dwIndex,
  418. m_ppInst);
  419. if(sc != S_OK)
  420. throw sc;
  421. // reset state
  422. for(DWORD i=0; i< m_dwThreshHold; i++)
  423. {
  424. if(m_ppInst[i] != NULL)
  425. (m_ppInst[i])->Release();
  426. m_ppInst[i] = NULL;
  427. }
  428. m_dwIndex = 0;
  429. }
  430. return;
  431. }
  432. void
  433. CWbemInstanceMgr::SetStatus(
  434. LONG lFlags,
  435. HRESULT hr,
  436. BSTR strParam,
  437. IWbemClassObject* pObjParam)
  438. {
  439. m_pSink->SetStatus(
  440. lFlags,
  441. hr,
  442. strParam,
  443. pObjParam);
  444. }