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.

698 lines
20 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. //***************************************************************************
  8. //
  9. // PERFSAMP.CPP
  10. //
  11. // Sample NT5 Perf Counter Provider
  12. //
  13. // raymcc 02-Dec-97 Created
  14. // raymcc 20-Feb-98 Updated to use new initializer
  15. //
  16. //***************************************************************************
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include <wbemidl.h>
  20. #include <stdprov.h>
  21. #include <oahelp.inl>
  22. #define MAX_INSTANCES 40000
  23. //***************************************************************************
  24. //
  25. // CStdProvider constructor
  26. //
  27. //***************************************************************************
  28. // ok
  29. CStdProvider::CStdProvider()
  30. {
  31. m_lRef = 0;
  32. m_pClassDef = 0;
  33. }
  34. //***************************************************************************
  35. //
  36. // CStdProvider destructor
  37. //
  38. //***************************************************************************
  39. // ok
  40. CStdProvider::~CStdProvider()
  41. {
  42. if (m_pClassDef)
  43. m_pClassDef->Release();
  44. }
  45. //***************************************************************************
  46. //
  47. // CNt5Refresher::AddRef
  48. //
  49. // Standard COM AddRef().
  50. //
  51. //***************************************************************************
  52. // ok
  53. ULONG CStdProvider::AddRef()
  54. {
  55. return InterlockedIncrement(&m_lRef);
  56. }
  57. //***************************************************************************
  58. //
  59. // CNt5Refresher::Release
  60. //
  61. // Standard COM Release().
  62. //
  63. //***************************************************************************
  64. // ok
  65. ULONG CStdProvider::Release()
  66. {
  67. long lRef = InterlockedDecrement(&m_lRef);
  68. if(lRef == 0)
  69. delete this;
  70. return lRef;
  71. }
  72. //***************************************************************************
  73. //
  74. // CNt5Refresher::QueryInterface
  75. //
  76. // Standard COM QueryInterface(). We have to support two interfaces,
  77. // the IWbemHiPerfProvider interface itself to provide the objects and
  78. // the IWbemProviderInit interface to initialize the provider.
  79. //
  80. //***************************************************************************
  81. // ok
  82. HRESULT CStdProvider::QueryInterface(REFIID riid, void** ppv)
  83. {
  84. if(riid == IID_IUnknown || riid == IID_IWbemServices)
  85. {
  86. *ppv = (IWbemServices *) this;
  87. AddRef();
  88. return S_OK;
  89. }
  90. else if (riid == IID_IWbemProviderInit)
  91. {
  92. *ppv = (IWbemProviderInit *) this;
  93. AddRef();
  94. return S_OK;
  95. }
  96. else return E_NOINTERFACE;
  97. }
  98. //***************************************************************************
  99. //
  100. // CNt5Refresher::Initialize
  101. //
  102. // Called once during startup. Insdicates to the provider which
  103. // namespace it is being invoked for and which User. It also supplies
  104. // a back pointer to WINMGMT so that class definitions can be retrieved.
  105. //
  106. // We perform any one-time initialization in this routine. The
  107. // final call to Release() is for any cleanup.
  108. //
  109. // <wszUser> The current user.
  110. // <lFlags> Reserved.
  111. // <wszNamespace> The namespace for which we are being activated.
  112. // <wszLocale> The locale under which we are to be running.
  113. // <pNamespace> An active pointer back into the current namespace
  114. // from which we can retrieve schema objects.
  115. // <pCtx> The user's context object. We simply reuse this
  116. // during any reentrant operations into WINMGMT.
  117. // <pInitSink> The sink to which we indicate our readiness.
  118. //
  119. //***************************************************************************
  120. // ok
  121. HRESULT CStdProvider::Initialize(
  122. /* [unique][in] */ LPWSTR wszUser,
  123. /* [in] */ LONG lFlags,
  124. /* [in] */ LPWSTR wszNamespace,
  125. /* [unique][in] */ LPWSTR wszLocale,
  126. /* [in] */ IWbemServices __RPC_FAR *pNamespace,
  127. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  128. /* [in] */ IWbemProviderInitSink __RPC_FAR *pInitSink
  129. )
  130. {
  131. // Get the class definition.
  132. // =========================
  133. HRESULT hRes = pNamespace->GetObject(BSTR(L"TestClass"),
  134. 0, pCtx, &m_pClassDef, 0
  135. );
  136. if (hRes)
  137. return hRes;
  138. pInitSink->SetStatus(0, WBEM_S_INITIALIZED);
  139. return NO_ERROR;
  140. }
  141. //*****************************************************************************
  142. //
  143. //*****************************************************************************
  144. HRESULT CStdProvider::OpenNamespace(
  145. /* [in] */ BSTR strNamespace,
  146. /* [in] */ long lFlags,
  147. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  148. /* [unique][in][out] */ IWbemServices __RPC_FAR *__RPC_FAR *ppWorkingNamespace,
  149. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppResult
  150. )
  151. {
  152. return WBEM_E_NOT_SUPPORTED;
  153. }
  154. //*****************************************************************************
  155. //
  156. //*****************************************************************************
  157. HRESULT CStdProvider::CancelAsyncCall(
  158. /* [in] */ IWbemObjectSink __RPC_FAR *pSink
  159. )
  160. {
  161. return WBEM_E_NOT_SUPPORTED;
  162. }
  163. //*****************************************************************************
  164. //
  165. //*****************************************************************************
  166. HRESULT CStdProvider::QueryObjectSink(
  167. /* [in] */ long lFlags,
  168. /* [out] */ IWbemObjectSink __RPC_FAR *__RPC_FAR *ppResponseHandler
  169. )
  170. {
  171. return WBEM_E_NOT_SUPPORTED;
  172. }
  173. //*****************************************************************************
  174. //
  175. //*****************************************************************************
  176. HRESULT CStdProvider::GetObject(
  177. /* [in] */ BSTR strObjectPath,
  178. /* [in] */ long lFlags,
  179. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  180. /* [unique][in][out] */ IWbemClassObject __RPC_FAR *__RPC_FAR *ppObject,
  181. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult
  182. )
  183. {
  184. return WBEM_E_NOT_SUPPORTED;
  185. }
  186. //*****************************************************************************
  187. //
  188. //*****************************************************************************
  189. HRESULT CStdProvider::GetObjectAsync(
  190. /* [in] */ BSTR strObjectPath,
  191. /* [in] */ long lFlags,
  192. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  193. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler
  194. )
  195. {
  196. IWbemClassObject *pInst = 0;
  197. HRESULT hRes = m_pClassDef->SpawnInstance(0, &pInst);
  198. if (hRes)
  199. return hRes;
  200. // Copy the key value back.
  201. VARIANT v;
  202. VariantInit(&v);
  203. BSTR PropName = SysAllocString(L"KeyVal");
  204. pInst->Put(PropName, 0, &v, 0);
  205. SysFreeString(PropName);
  206. VariantClear(&v);
  207. // IntVal1.
  208. // ========
  209. V_VT(&v) = VT_I4;
  210. V_I4(&v) = 100;
  211. PropName = SysAllocString(L"IntVal1");
  212. pInst->Put(PropName, 0, &v, 0);
  213. SysFreeString(PropName);
  214. VariantClear(&v);
  215. // IntVal2.
  216. // ========
  217. V_VT(&v) = VT_I4;
  218. V_I4(&v) = 200;
  219. PropName = SysAllocString(L"IntVal2");
  220. pInst->Put(PropName, 0, &v, 0);
  221. SysFreeString(PropName);
  222. VariantClear(&v);
  223. // IntVal3.
  224. // ========
  225. V_VT(&v) = VT_I4;
  226. V_I4(&v) = 300;
  227. PropName = SysAllocString(L"IntVal3");
  228. pInst->Put(PropName, 0, &v, 0);
  229. SysFreeString(PropName);
  230. VariantClear(&v);
  231. // IntVal4.
  232. // ========
  233. V_VT(&v) = VT_I4;
  234. V_I4(&v) = 400;
  235. PropName = SysAllocString(L"IntVal4");
  236. pInst->Put(PropName, 0, &v, 0);
  237. SysFreeString(PropName);
  238. VariantClear(&v);
  239. // Fill in props
  240. // string StrVal1;
  241. // string StrVal2;
  242. // string StrVal3;
  243. // string StrVal4;
  244. //
  245. // sint32 IntVal1;
  246. // sint32 IntVal2;
  247. // sint32 IntVal3;
  248. // sint32 IntVal4;
  249. //
  250. // sint32 IntArray1[];
  251. // sint32 IntArray2[];
  252. pResponseHandler->Indicate(1, &pInst);
  253. pResponseHandler->SetStatus(0, WBEM_NO_ERROR, 0, 0);
  254. return WBEM_NO_ERROR;
  255. }
  256. //*****************************************************************************
  257. //
  258. //*****************************************************************************
  259. HRESULT CStdProvider::PutClass(
  260. /* [in] */ IWbemClassObject __RPC_FAR *pObject,
  261. /* [in] */ long lFlags,
  262. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  263. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult
  264. )
  265. {
  266. return WBEM_E_NOT_SUPPORTED;
  267. }
  268. //*****************************************************************************
  269. //
  270. //*****************************************************************************
  271. HRESULT CStdProvider::PutClassAsync(
  272. /* [in] */ IWbemClassObject __RPC_FAR *pObject,
  273. /* [in] */ long lFlags,
  274. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  275. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler
  276. )
  277. {
  278. return WBEM_E_NOT_SUPPORTED;
  279. }
  280. //*****************************************************************************
  281. //
  282. //*****************************************************************************
  283. HRESULT CStdProvider::DeleteClass(
  284. /* [in] */ BSTR strClass,
  285. /* [in] */ long lFlags,
  286. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  287. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult
  288. )
  289. {
  290. return WBEM_E_NOT_SUPPORTED;
  291. }
  292. //*****************************************************************************
  293. //
  294. //*****************************************************************************
  295. HRESULT CStdProvider::DeleteClassAsync(
  296. /* [in] */ BSTR strClass,
  297. /* [in] */ long lFlags,
  298. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  299. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler
  300. )
  301. {
  302. return WBEM_E_NOT_SUPPORTED;
  303. }
  304. //*****************************************************************************
  305. //
  306. //*****************************************************************************
  307. HRESULT CStdProvider::CreateClassEnum(
  308. /* [in] */ BSTR strSuperclass,
  309. /* [in] */ long lFlags,
  310. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  311. /* [out] */ IEnumWbemClassObject __RPC_FAR *__RPC_FAR *ppEnum
  312. )
  313. {
  314. return WBEM_E_NOT_SUPPORTED;
  315. }
  316. //*****************************************************************************
  317. //
  318. //*****************************************************************************
  319. HRESULT CStdProvider::CreateClassEnumAsync(
  320. /* [in] */ BSTR strSuperclass,
  321. /* [in] */ long lFlags,
  322. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  323. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler
  324. )
  325. {
  326. return WBEM_E_NOT_SUPPORTED;
  327. }
  328. //*****************************************************************************
  329. //
  330. //*****************************************************************************
  331. HRESULT CStdProvider::PutInstance(
  332. /* [in] */ IWbemClassObject __RPC_FAR *pInst,
  333. /* [in] */ long lFlags,
  334. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  335. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult
  336. )
  337. {
  338. return WBEM_E_NOT_SUPPORTED;
  339. }
  340. //*****************************************************************************
  341. //
  342. //*****************************************************************************
  343. HRESULT CStdProvider::PutInstanceAsync(
  344. /* [in] */ IWbemClassObject __RPC_FAR *pInst,
  345. /* [in] */ long lFlags,
  346. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  347. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler
  348. )
  349. {
  350. return WBEM_E_NOT_SUPPORTED;
  351. }
  352. //*****************************************************************************
  353. //
  354. //*****************************************************************************
  355. HRESULT CStdProvider::DeleteInstance(
  356. /* [in] */ BSTR strObjectPath,
  357. /* [in] */ long lFlags,
  358. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  359. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult
  360. )
  361. {
  362. return WBEM_E_NOT_SUPPORTED;
  363. }
  364. //*****************************************************************************
  365. //
  366. //*****************************************************************************
  367. HRESULT CStdProvider::DeleteInstanceAsync(
  368. /* [in] */ BSTR strObjectPath,
  369. /* [in] */ long lFlags,
  370. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  371. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler
  372. )
  373. {
  374. return WBEM_E_NOT_SUPPORTED;
  375. }
  376. //*****************************************************************************
  377. //
  378. //*****************************************************************************
  379. HRESULT CStdProvider::CreateInstanceEnum(
  380. /* [in] */ BSTR strClass,
  381. /* [in] */ long lFlags,
  382. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  383. /* [out] */ IEnumWbemClassObject __RPC_FAR *__RPC_FAR *ppEnum
  384. )
  385. {
  386. return WBEM_E_NOT_SUPPORTED;
  387. }
  388. //*****************************************************************************
  389. //
  390. //*****************************************************************************
  391. HRESULT CStdProvider::CreateInstanceEnumAsync(
  392. /* [in] */ BSTR strClass,
  393. /* [in] */ long lFlags,
  394. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  395. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler
  396. )
  397. {
  398. wchar_t buf[256];
  399. IWbemClassObject *BufArray[10];
  400. int nLoad = 0;
  401. IWbemClassObject *pInst = 0;
  402. HRESULT hRes = m_pClassDef->SpawnInstance(0, &pInst);
  403. if (hRes)
  404. return hRes;
  405. for (int i = 0; i < MAX_INSTANCES; i++)
  406. {
  407. VARIANT v;
  408. VariantInit(&v);
  409. swprintf(buf, L"Inst%d", i);
  410. V_VT(&v) = VT_BSTR;
  411. V_BSTR(&v) = SysAllocString(buf);
  412. BSTR PropName = SysAllocString(L"KeyVal");
  413. pInst->Put(PropName, 0, &v, 0);
  414. SysFreeString(PropName);
  415. VariantClear(&v);
  416. // IntVal1.
  417. // ========
  418. V_VT(&v) = VT_I4;
  419. V_I4(&v) = 100 + i;
  420. PropName = SysAllocString(L"IntVal1");
  421. pInst->Put(PropName, 0, &v, 0);
  422. SysFreeString(PropName);
  423. VariantClear(&v);
  424. // IntVal2.
  425. // ========
  426. V_VT(&v) = VT_I4;
  427. V_I4(&v) = 200 + i;
  428. PropName = SysAllocString(L"IntVal2");
  429. pInst->Put(PropName, 0, &v, 0);
  430. SysFreeString(PropName);
  431. VariantClear(&v);
  432. // IntVal3.
  433. // ========
  434. V_VT(&v) = VT_I4;
  435. V_I4(&v) = 300 + i;
  436. PropName = SysAllocString(L"IntVal3");
  437. pInst->Put(PropName, 0, &v, 0);
  438. SysFreeString(PropName);
  439. VariantClear(&v);
  440. // IntVal4.
  441. // ========
  442. V_VT(&v) = VT_I4;
  443. V_I4(&v) = 400 + i;
  444. PropName = SysAllocString(L"IntVal4");
  445. pInst->Put(PropName, 0, &v, 0);
  446. SysFreeString(PropName);
  447. VariantClear(&v);
  448. // StrVal1
  449. // =======
  450. V_VT(&v) = VT_BSTR;
  451. swprintf(buf, L"StrVal1=%d", i);
  452. V_BSTR(&v) = SysAllocString(buf);
  453. PropName = SysAllocString(L"StrVal1");
  454. pInst->Put(PropName, 0, &v, 0);
  455. SysFreeString(PropName);
  456. VariantClear(&v);
  457. // StrVal2
  458. // =======
  459. V_VT(&v) = VT_BSTR;
  460. swprintf(buf, L"StrVal2=%d !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", i);
  461. V_BSTR(&v) = SysAllocString(buf);
  462. PropName = SysAllocString(L"StrVal2");
  463. pInst->Put(PropName, 0, &v, 0);
  464. SysFreeString(PropName);
  465. VariantClear(&v);
  466. // StrVal3
  467. // =======
  468. V_VT(&v) = VT_BSTR;
  469. swprintf(buf, L"StrVal3=%d", i);
  470. V_BSTR(&v) = SysAllocString(buf);
  471. PropName = SysAllocString(L"StrVal3");
  472. pInst->Put(PropName, 0, &v, 0);
  473. SysFreeString(PropName);
  474. VariantClear(&v);
  475. // StrVal4
  476. // =======
  477. V_VT(&v) = VT_BSTR;
  478. swprintf(buf, L"StrVal4=%d &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&", i);
  479. V_BSTR(&v) = SysAllocString(buf);
  480. PropName = SysAllocString(L"StrVal4");
  481. pInst->Put(PropName, 0, &v, 0);
  482. SysFreeString(PropName);
  483. VariantClear(&v);
  484. pResponseHandler->Indicate(1, &pInst);
  485. // pInst->Release();
  486. }
  487. pInst-Release();
  488. pResponseHandler->SetStatus(0, WBEM_NO_ERROR, 0, 0);
  489. return WBEM_NO_ERROR;
  490. }
  491. //*****************************************************************************
  492. //
  493. //*****************************************************************************
  494. HRESULT CStdProvider::ExecQuery(
  495. /* [in] */ BSTR strQueryLanguage,
  496. /* [in] */ BSTR strQuery,
  497. /* [in] */ long lFlags,
  498. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  499. /* [out] */ IEnumWbemClassObject __RPC_FAR *__RPC_FAR *ppEnum
  500. )
  501. {
  502. return WBEM_E_NOT_SUPPORTED;
  503. }
  504. //*****************************************************************************
  505. //
  506. //*****************************************************************************
  507. HRESULT CStdProvider::ExecQueryAsync(
  508. /* [in] */ BSTR strQueryLanguage,
  509. /* [in] */ BSTR strQuery,
  510. /* [in] */ long lFlags,
  511. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  512. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler
  513. )
  514. {
  515. // pResponseHandler->Indicate(1, &pInst);
  516. pResponseHandler->SetStatus(0, WBEM_NO_ERROR, 0, 0);
  517. return WBEM_NO_ERROR;
  518. }
  519. //*****************************************************************************
  520. //
  521. //*****************************************************************************
  522. HRESULT CStdProvider::ExecNotificationQuery(
  523. /* [in] */ BSTR strQueryLanguage,
  524. /* [in] */ BSTR strQuery,
  525. /* [in] */ long lFlags,
  526. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  527. /* [out] */ IEnumWbemClassObject __RPC_FAR *__RPC_FAR *ppEnum
  528. )
  529. {
  530. return WBEM_E_NOT_SUPPORTED;
  531. }
  532. //*****************************************************************************
  533. //
  534. //*****************************************************************************
  535. HRESULT CStdProvider::ExecNotificationQueryAsync(
  536. /* [in] */ BSTR strQueryLanguage,
  537. /* [in] */ BSTR strQuery,
  538. /* [in] */ long lFlags,
  539. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  540. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler
  541. )
  542. {
  543. return WBEM_E_NOT_SUPPORTED;
  544. }
  545. //*****************************************************************************
  546. //
  547. //*****************************************************************************
  548. HRESULT CStdProvider::ExecMethod(
  549. /* [in] */ BSTR strObjectPath,
  550. /* [in] */ BSTR strMethodName,
  551. /* [in] */ long lFlags,
  552. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  553. /* [in] */ IWbemClassObject __RPC_FAR *pInParams,
  554. /* [unique][in][out] */ IWbemClassObject __RPC_FAR *__RPC_FAR *ppOutParams,
  555. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult
  556. )
  557. {
  558. return WBEM_E_NOT_SUPPORTED;
  559. }
  560. //*****************************************************************************
  561. //
  562. //*****************************************************************************
  563. HRESULT CStdProvider::ExecMethodAsync(
  564. /* [in] */ BSTR strObjectPath,
  565. /* [in] */ BSTR strMethodName,
  566. /* [in] */ long lFlags,
  567. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  568. /* [in] */ IWbemClassObject __RPC_FAR *pInParams,
  569. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler
  570. )
  571. {
  572. return WBEM_E_NOT_SUPPORTED;
  573. }