/WMI.cpp

https://bitbucket.org/jimmyD/open-hardware-monitor-applet · C++ · 1090 lines · 755 code · 264 blank · 71 comment · 109 complexity · fcdccd4a25434bab7a7a02f61df2e6ed MD5 · raw file

  1. //-----------------------------------------------------------------
  2. // WMI Object
  3. // C++ Source - WMI.cpp - version v1.0 (2012-08-01)
  4. //
  5. //-----------------------------------------------------------------
  6. //-----------------------------------------------------------------
  7. // Include Files
  8. //-----------------------------------------------------------------
  9. #include "stdafx.h"
  10. #include "WMI.h"
  11. //-----------------------------------------------------------------
  12. // WMI methods
  13. //-----------------------------------------------------------------
  14. WMI::WMI()
  15. {
  16. pEnumerator = NULL;
  17. pLoc = 0;
  18. pSvc = 0;
  19. pclsObj = 0;
  20. connectToWMI();
  21. CPUName = "";
  22. CPUIdentifier = "";
  23. CPUClock.clear();
  24. cpuLoad.clear();
  25. cpuTemp.clear();
  26. GPUName.clear();
  27. GPUIdentifier.clear();
  28. GPULoad.clear();
  29. GPUTemp.clear();
  30. GPUFan.clear();
  31. GPUClock.clear();
  32. GPUMemoryClock.clear();
  33. HDDName.clear();
  34. HDDIdentifier.clear();
  35. HDDTemperature.clear();
  36. HDDLoad.clear();
  37. }
  38. WMI::~WMI(void)
  39. {
  40. pSvc->Release();
  41. pLoc->Release();
  42. CoUninitialize();
  43. }
  44. void WMI::connectToWMI()
  45. {
  46. // Initialize COM. ------------------------------------------
  47. hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  48. if (!FAILED(hres))
  49. {
  50. // Set general COM security levels --------------------------
  51. // Note: If you are using Windows 2000, you need to specify -
  52. // the default authentication credentials for a user by using
  53. // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
  54. // parameter of CoInitializeSecurity ------------------------
  55. hres = CoInitializeSecurity(
  56. NULL,
  57. -1, // COM authentication
  58. NULL, // Authentication services
  59. NULL, // Reserved
  60. RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
  61. RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
  62. NULL, // Authentication info
  63. EOAC_NONE, // Additional capabilities
  64. NULL // Reserved
  65. );
  66. if (!FAILED(hres))
  67. {
  68. // Obtain the initial locator to WMI -------------------------
  69. hres = CoCreateInstance(
  70. CLSID_WbemLocator,
  71. 0,
  72. CLSCTX_INPROC_SERVER,
  73. IID_IWbemLocator, (LPVOID *) &pLoc);
  74. if (!FAILED(hres))
  75. {
  76. // Connect to WMI through the IWbemLocator::ConnectServer method
  77. // Connect to the root\cimv2 namespace with
  78. // the current user and obtain pointer pSvc
  79. // to make IWbemServices calls.
  80. hres = pLoc->ConnectServer(
  81. _bstr_t(L"ROOT\\OpenHardwareMonitor"), // Object path of WMI namespace
  82. NULL, // User name. NULL = current user
  83. NULL, // User password. NULL = current
  84. 0, // Locale. NULL indicates current
  85. NULL, // Security flags.
  86. 0, // Authority (e.g. Kerberos)
  87. 0, // Context object
  88. &pSvc // pointer to IWbemServices proxy
  89. );
  90. if (!FAILED(hres))
  91. {
  92. // Set security levels on the proxy -------------------------
  93. hres = CoSetProxyBlanket(
  94. pSvc, // Indicates the proxy to set
  95. RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
  96. RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
  97. NULL, // Server principal name
  98. RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
  99. RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
  100. NULL, // client identity
  101. EOAC_NONE // proxy capabilities
  102. );
  103. }
  104. }
  105. }
  106. }
  107. }
  108. void WMI::queryCPUName()
  109. {
  110. if(pSvc != 0)
  111. {
  112. // Use the IWbemServices pointer to make requests of WMI ----
  113. // For example, get the name of the operating system
  114. hres = pSvc->ExecQuery(
  115. bstr_t("WQL"),
  116. bstr_t("select * from Hardware WHERE HardwareType = 'CPU'"),
  117. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  118. NULL,
  119. &pEnumerator);
  120. if (!FAILED(hres))
  121. {
  122. // Get the data from the query
  123. ULONG uReturn = 0;
  124. while (pEnumerator)
  125. {
  126. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  127. &pclsObj, &uReturn);
  128. if(0 == uReturn)
  129. {
  130. break;
  131. }
  132. VARIANT vtProp;
  133. // Get the value of the Name property
  134. hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
  135. wstring ws(vtProp.bstrVal, SysStringLen(vtProp.bstrVal));
  136. CPUName = string( ws.begin(), ws.end() );
  137. ws.clear();
  138. VariantClear(&vtProp);
  139. hr = pclsObj->Get(L"Identifier", 0, &vtProp, 0, 0);
  140. ws = wstring(vtProp.bstrVal, SysStringLen(vtProp.bstrVal));
  141. CPUIdentifier = string( ws.begin(), ws.end() );
  142. ws.clear();
  143. VariantClear(&vtProp);
  144. pclsObj->Release();
  145. }
  146. pEnumerator->Release();
  147. uReturn = 0;
  148. }
  149. }
  150. }
  151. void WMI::queryGPUName()
  152. {
  153. GPUName.clear();
  154. GPUIdentifier.clear();
  155. if(pSvc != 0)
  156. {
  157. // Use the IWbemServices pointer to make requests of WMI ----
  158. // For example, get the name of the operating system
  159. hres = pSvc->ExecQuery(
  160. bstr_t("WQL"),
  161. bstr_t("select * from Hardware WHERE HardwareType = 'GpuAti' or HardwareType = 'GpuNvidia'"),
  162. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  163. NULL,
  164. &pEnumerator);
  165. if (!FAILED(hres))
  166. {
  167. // Get the data from the query
  168. ULONG uReturn = 0;
  169. while (pEnumerator)
  170. {
  171. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  172. &pclsObj, &uReturn);
  173. if(0 == uReturn)
  174. {
  175. break;
  176. }
  177. VARIANT vtProp;
  178. // Get the value of the Name property
  179. hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
  180. wstring ws(vtProp.bstrVal, SysStringLen(vtProp.bstrVal));
  181. GPUName.push_back(string( ws.begin(), ws.end() ));
  182. VariantClear(&vtProp);
  183. ws.clear();
  184. hr = pclsObj->Get(L"Identifier", 0, &vtProp, 0, 0);
  185. ws = wstring(vtProp.bstrVal, SysStringLen(vtProp.bstrVal));
  186. GPUIdentifier.push_back(string( ws.begin(), ws.end() ));
  187. VariantClear(&vtProp);
  188. uReturn =0;
  189. pclsObj->Release();
  190. }
  191. pEnumerator->Release();
  192. }
  193. }
  194. }
  195. void WMI::queryHDName()
  196. {
  197. HDDName.clear();
  198. HDDIdentifier.clear();
  199. if(pSvc != 0)
  200. {
  201. // Use the IWbemServices pointer to make requests of WMI ----
  202. // For example, get the name of the operating system
  203. hres = pSvc->ExecQuery(
  204. bstr_t("WQL"),
  205. bstr_t("select * from Hardware WHERE HardwareType = 'HDD'"),
  206. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  207. NULL,
  208. &pEnumerator);
  209. if (!FAILED(hres))
  210. {
  211. // Get the data from the query
  212. ULONG uReturn = 0;
  213. while (pEnumerator)
  214. {
  215. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  216. &pclsObj, &uReturn);
  217. if(0 == uReturn)
  218. {
  219. break;
  220. }
  221. VARIANT vtProp;
  222. // Get the value of the Name property
  223. hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
  224. wstring ws(vtProp.bstrVal, SysStringLen(vtProp.bstrVal));
  225. HDDName.push_back(string( ws.begin(), ws.end() ));
  226. VariantClear(&vtProp);
  227. ws.clear();
  228. hr = pclsObj->Get(L"Identifier", 0, &vtProp, 0, 0);
  229. ws = wstring(vtProp.bstrVal, SysStringLen(vtProp.bstrVal));
  230. HDDIdentifier.push_back(string( ws.begin(), ws.end() ));
  231. VariantClear(&vtProp);
  232. uReturn =0;
  233. pclsObj->Release();
  234. }
  235. pEnumerator->Release();
  236. }
  237. }
  238. }
  239. void WMI::queryCPULoad()
  240. {
  241. cpuLoad.clear();
  242. if(pSvc != 0)
  243. {
  244. // Use the IWbemServices pointer to make requests of WMI ----
  245. // For example, get the name of the operating system
  246. string query = "select * from Sensor where Parent = '";
  247. query.append(CPUIdentifier);
  248. query.append("' and SensorType='Load' and Name != 'CPU Total'");
  249. char *a=new char[query.size()+1];
  250. a[query.size()]=0;
  251. memcpy(a,query.c_str(),query.size());
  252. hres = pSvc->ExecQuery(
  253. bstr_t("WQL"),
  254. bstr_t(a),
  255. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  256. NULL,
  257. &pEnumerator);
  258. if (!FAILED(hres))
  259. {
  260. // Get the data from the query
  261. ULONG uReturn = 0;
  262. while (pEnumerator)
  263. {
  264. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  265. &pclsObj, &uReturn);
  266. if(0 == uReturn)
  267. {
  268. break;
  269. }
  270. VARIANT vtProp;
  271. // Get the value of the Name property
  272. hr = pclsObj->Get(L"Value", 0, &vtProp, 0, 0);
  273. int intValue = (int)vtProp.fltVal;
  274. stringstream stringStream;
  275. stringStream << intValue;
  276. cpuLoad.push_back(stringStream.str());
  277. stringStream.clear();
  278. VariantClear(&vtProp);
  279. uReturn =0;
  280. pclsObj->Release();
  281. }
  282. pEnumerator->Release();
  283. }
  284. delete a;
  285. }
  286. }
  287. void WMI::queryCPUTemp()
  288. {
  289. cpuTemp.clear();
  290. if(pSvc != 0)
  291. {
  292. // Use the IWbemServices pointer to make requests of WMI ----
  293. // For example, get the name of the operating system
  294. string query = "select * from Sensor where Parent = '";
  295. query.append(CPUIdentifier);
  296. query.append("' and SensorType='Temperature'");
  297. char *a=new char[query.size()+1];
  298. a[query.size()]=0;
  299. memcpy(a,query.c_str(),query.size());
  300. hres = pSvc->ExecQuery(
  301. bstr_t("WQL"),
  302. bstr_t(a),
  303. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  304. NULL,
  305. &pEnumerator);
  306. if (!FAILED(hres))
  307. {
  308. // Get the data from the query
  309. ULONG uReturn = 0;
  310. while (pEnumerator)
  311. {
  312. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  313. &pclsObj, &uReturn);
  314. if(0 == uReturn)
  315. {
  316. break;
  317. }
  318. VARIANT vtProp;
  319. // Get the value of the Name property
  320. hr = pclsObj->Get(L"Value", 0, &vtProp, 0, 0);
  321. int intValue = (int)vtProp.fltVal;
  322. stringstream stringStream;
  323. stringStream << intValue;
  324. cpuTemp.push_back(stringStream.str());
  325. stringStream.clear();
  326. VariantClear(&vtProp);
  327. uReturn = 0;
  328. pclsObj->Release();
  329. }
  330. pEnumerator->Release();
  331. }
  332. delete a;
  333. }
  334. }
  335. void WMI::queryCPUClock()
  336. {
  337. CPUClock.clear();
  338. if(pSvc != 0)
  339. {
  340. // Use the IWbemServices pointer to make requests of WMI ----
  341. // For example, get the name of the operating system
  342. string query = "select * from Sensor where Parent = '";
  343. query.append(CPUIdentifier);
  344. query.append("' and SensorType='Clock' and Name != 'Bus Speed'");
  345. char *a=new char[query.size()+1];
  346. a[query.size()]=0;
  347. memcpy(a,query.c_str(),query.size());
  348. hres = pSvc->ExecQuery(
  349. bstr_t("WQL"),
  350. bstr_t(a),
  351. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  352. NULL,
  353. &pEnumerator);
  354. if (!FAILED(hres))
  355. {
  356. // Get the data from the query
  357. ULONG uReturn = 0;
  358. while (pEnumerator)
  359. {
  360. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  361. &pclsObj, &uReturn);
  362. if(0 == uReturn)
  363. {
  364. break;
  365. }
  366. VARIANT vtProp;
  367. // Get the value of the Name property
  368. hr = pclsObj->Get(L"Value", 0, &vtProp, 0, 0);
  369. stringstream stringStream;
  370. stringStream << floor(vtProp.fltVal + 0.5);
  371. CPUClock.push_back(stringStream.str());
  372. stringStream.clear();
  373. VariantClear(&vtProp);
  374. uReturn = 0;
  375. pclsObj->Release();
  376. }
  377. pEnumerator->Release();
  378. }
  379. delete a;
  380. }
  381. }
  382. void WMI::queryGPULoad()
  383. {
  384. GPULoad.clear();
  385. if(pSvc != 0)
  386. {
  387. for(int count =0; count < GPUIdentifier.size(); count++)
  388. {
  389. // Use the IWbemServices pointer to make requests of WMI ----
  390. // For example, get the name of the operating system
  391. string query = "select * from Sensor where Parent = '";
  392. query.append(GPUIdentifier[count]);
  393. query.append("' and SensorType='Load'");
  394. char *a=new char[query.size()+1];
  395. a[query.size()]=0;
  396. memcpy(a,query.c_str(),query.size());
  397. hres = pSvc->ExecQuery(
  398. bstr_t("WQL"),
  399. bstr_t(a),
  400. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  401. NULL,
  402. &pEnumerator);
  403. if (!FAILED(hres))
  404. {
  405. // Get the data from the query
  406. ULONG uReturn = 0;
  407. while (pEnumerator)
  408. {
  409. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  410. &pclsObj, &uReturn);
  411. if(0 == uReturn)
  412. {
  413. break;
  414. }
  415. VARIANT vtProp;
  416. // Get the value of the Name property
  417. hr = pclsObj->Get(L"Value", 0, &vtProp, 0, 0);
  418. stringstream stringStream;
  419. stringStream << vtProp.fltVal;
  420. GPULoad.push_back(stringStream.str());
  421. stringStream.clear();
  422. VariantClear(&vtProp);
  423. uReturn = 0;
  424. pclsObj->Release();
  425. }
  426. pEnumerator->Release();
  427. }
  428. delete a;
  429. }
  430. }
  431. }
  432. void WMI::queryGPUTemp()
  433. {
  434. GPUTemp.clear();
  435. if(pSvc != 0)
  436. {
  437. for(int count =0; count < GPUIdentifier.size(); count++)
  438. {
  439. // Use the IWbemServices pointer to make requests of WMI ----
  440. // For example, get the name of the operating system
  441. string query = "select * from Sensor where Parent = '";
  442. query.append(GPUIdentifier[count]);
  443. query.append("' and SensorType='Temperature'");
  444. char *a=new char[query.size()+1];
  445. a[query.size()]=0;
  446. memcpy(a,query.c_str(),query.size());
  447. hres = pSvc->ExecQuery(
  448. bstr_t("WQL"),
  449. bstr_t(a),
  450. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  451. NULL,
  452. &pEnumerator);
  453. if (!FAILED(hres))
  454. {
  455. // Get the data from the query
  456. ULONG uReturn = 0;
  457. while (pEnumerator)
  458. {
  459. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  460. &pclsObj, &uReturn);
  461. if(0 == uReturn)
  462. {
  463. break;
  464. }
  465. VARIANT vtProp;
  466. // Get the value of the Name property
  467. hr = pclsObj->Get(L"Value", 0, &vtProp, 0, 0);
  468. stringstream stringStream;
  469. stringStream << vtProp.fltVal;
  470. GPUTemp.push_back(stringStream.str());
  471. stringStream.clear();
  472. VariantClear(&vtProp);
  473. uReturn = 0;
  474. pclsObj->Release();
  475. }
  476. pEnumerator->Release();
  477. }
  478. delete a;
  479. }
  480. }
  481. }
  482. void WMI::queryGPUFan()
  483. {
  484. GPUFan.clear();
  485. if(pSvc != 0)
  486. {
  487. for(int count =0; count < GPUIdentifier.size(); count++)
  488. {
  489. // Use the IWbemServices pointer to make requests of WMI ----
  490. // For example, get the name of the operating system
  491. string query = "select * from Sensor where Parent = '";
  492. query.append(GPUIdentifier[count]);
  493. query.append("' and SensorType='Control'");
  494. char *a=new char[query.size()+1];
  495. a[query.size()]=0;
  496. memcpy(a,query.c_str(),query.size());
  497. hres = pSvc->ExecQuery(
  498. bstr_t("WQL"),
  499. bstr_t(a),
  500. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  501. NULL,
  502. &pEnumerator);
  503. if (!FAILED(hres))
  504. {
  505. // Get the data from the query
  506. ULONG uReturn = 0;
  507. while (pEnumerator)
  508. {
  509. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  510. &pclsObj, &uReturn);
  511. if(0 == uReturn)
  512. {
  513. break;
  514. }
  515. VARIANT vtProp;
  516. // Get the value of the Name property
  517. hr = pclsObj->Get(L"Value", 0, &vtProp, 0, 0);
  518. stringstream stringStream;
  519. stringStream << vtProp.fltVal;
  520. GPUFan.push_back(stringStream.str());
  521. stringStream.clear();
  522. VariantClear(&vtProp);
  523. uReturn =0;
  524. pclsObj->Release();
  525. }
  526. if(count != GPUFan.size()-1)
  527. {
  528. GPUFan.push_back("");
  529. }
  530. pEnumerator->Release();
  531. }
  532. delete a;
  533. }
  534. }
  535. }
  536. void WMI::queryGPUClock()
  537. {
  538. GPUClock.clear();
  539. GPUMemoryClock.clear();
  540. if(pSvc != 0)
  541. {
  542. for(int count =0; count < GPUIdentifier.size(); count++)
  543. {
  544. // Use the IWbemServices pointer to make requests of WMI ----
  545. // For example, get the name of the operating system
  546. string query = "select * from Sensor where Parent = '";
  547. query.append(GPUIdentifier[count]);
  548. query.append("' and SensorType='Clock'");
  549. char *a=new char[query.size()+1];
  550. a[query.size()]=0;
  551. memcpy(a,query.c_str(),query.size());
  552. hres = pSvc->ExecQuery(
  553. bstr_t("WQL"),
  554. bstr_t(a),
  555. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  556. NULL,
  557. &pEnumerator);
  558. if (!FAILED(hres))
  559. {
  560. // Get the data from the query
  561. ULONG uReturn = 0;
  562. while (pEnumerator)
  563. {
  564. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  565. &pclsObj, &uReturn);
  566. if(0 == uReturn)
  567. {
  568. break;
  569. }
  570. VARIANT vtProp;
  571. // Get the value of the Name property
  572. hr = pclsObj->Get(L"Index", 0, &vtProp, 0, 0);
  573. if(vtProp.intVal == 0)
  574. {
  575. VariantClear(&vtProp);
  576. hr = pclsObj->Get(L"Value", 0, &vtProp, 0, 0);
  577. stringstream stringStream;
  578. stringStream << vtProp.fltVal;
  579. GPUClock.push_back(stringStream.str());
  580. stringStream.clear();
  581. }
  582. else if(vtProp.intVal == 1)
  583. {
  584. VariantClear(&vtProp);
  585. hr = pclsObj->Get(L"Value", 0, &vtProp, 0, 0);
  586. stringstream stringStream;
  587. stringStream << vtProp.fltVal;
  588. GPUMemoryClock.push_back(stringStream.str());
  589. stringStream.clear();
  590. }
  591. VariantClear(&vtProp);
  592. uReturn =0;
  593. pclsObj->Release();
  594. }
  595. pEnumerator->Release();
  596. }
  597. delete a;
  598. }
  599. }
  600. }
  601. void WMI::queryHDData()
  602. {
  603. HDDTemperature.clear();
  604. HDDLoad.clear();
  605. if(pSvc != 0)
  606. {
  607. for(int count =0; count < HDDIdentifier.size(); count++)
  608. {
  609. // Use the IWbemServices pointer to make requests of WMI ----
  610. // For example, get the name of the operating system
  611. string query = "select * from Sensor where Parent = '";
  612. query.append(HDDIdentifier[count]);
  613. query.append("' and (SensorType='Load' or SensorType='Temperature')");
  614. char *a=new char[query.size()+1];
  615. a[query.size()]=0;
  616. memcpy(a,query.c_str(),query.size());
  617. hres = pSvc->ExecQuery(
  618. bstr_t("WQL"),
  619. bstr_t(a),
  620. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  621. NULL,
  622. &pEnumerator);
  623. if (!FAILED(hres))
  624. {
  625. // Get the data from the query
  626. ULONG uReturn = 0;
  627. while (pEnumerator)
  628. {
  629. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  630. &pclsObj, &uReturn);
  631. if(0 == uReturn)
  632. {
  633. break;
  634. }
  635. VARIANT vtProp;
  636. // Get the value of the Name property
  637. hr = pclsObj->Get(L"SensorType", 0, &vtProp, 0, 0);
  638. wstring ws(vtProp.bstrVal, SysStringLen(vtProp.bstrVal));
  639. string sensortype = string( ws.begin(), ws.end() );
  640. VariantClear(&vtProp);
  641. ws.clear();
  642. if(sensortype.compare("Load") ==0)
  643. {
  644. hr = pclsObj->Get(L"Value", 0, &vtProp, 0, 0);
  645. int load = (int)vtProp.fltVal;
  646. stringstream stringStream;
  647. stringStream << load;
  648. HDDLoad.push_back(stringStream.str());
  649. stringStream.clear();
  650. }
  651. else if(sensortype.compare("Temperature") == 0)
  652. {
  653. hr = pclsObj->Get(L"Value", 0, &vtProp, 0, 0);
  654. int temperature = (int)vtProp.fltVal;
  655. stringstream stringStream;
  656. stringStream << temperature;
  657. HDDTemperature.push_back(stringStream.str());
  658. stringStream.clear();
  659. }
  660. VariantClear(&vtProp);
  661. uReturn = 0;
  662. pclsObj->Release();
  663. }
  664. pEnumerator->Release();
  665. }
  666. if(HDDTemperature.size() != HDDLoad.size())
  667. {
  668. HDDTemperature.push_back("");
  669. }
  670. delete a;
  671. }
  672. }
  673. }
  674. void WMI::queryMemory()
  675. {
  676. memoryLoad = "";
  677. if(pSvc != 0)
  678. {
  679. // Use the IWbemServices pointer to make requests of WMI ----
  680. // For example, get the name of the operating system
  681. hres = pSvc->ExecQuery(
  682. bstr_t("WQL"),
  683. bstr_t("select * from sensor WHERE name='Memory' and SensorType = 'Load'"),
  684. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  685. NULL,
  686. &pEnumerator);
  687. if (!FAILED(hres))
  688. {
  689. // Get the data from the query
  690. ULONG uReturn = 0;
  691. while (pEnumerator)
  692. {
  693. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  694. &pclsObj, &uReturn);
  695. if(0 == uReturn)
  696. {
  697. break;
  698. }
  699. VARIANT vtProp;
  700. // Get the value of the Name property
  701. hr = pclsObj->Get(L"Value", 0, &vtProp, 0, 0);
  702. int memory = (int)vtProp.fltVal;
  703. stringstream stringStream;
  704. stringStream << memory;
  705. memoryLoad = (string)stringStream.str();
  706. stringStream.clear();
  707. VariantClear(&vtProp);
  708. pclsObj->Release();
  709. }
  710. pEnumerator->Release();
  711. uReturn = 0;
  712. }
  713. }
  714. }
  715. vector<string> WMI::getmemoryText()
  716. {
  717. if(pSvc == 0)
  718. {
  719. this->connectToWMI();
  720. }
  721. text.clear();
  722. queryMemory();
  723. if(!memoryLoad.empty())
  724. {
  725. text.push_back(string("Memory: ").append(memoryLoad).append("% Used"));
  726. }
  727. time_t now = time(0);
  728. struct tm tstruct;
  729. char buf[80];
  730. tstruct = *localtime(&now);
  731. strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
  732. text.push_back(buf);
  733. return text;
  734. }
  735. vector<string> WMI::getCPUText()
  736. {
  737. if(pSvc == 0)
  738. {
  739. this->connectToWMI();
  740. }
  741. text.clear();
  742. queryCPUName();
  743. queryCPULoad();
  744. queryCPUTemp();
  745. queryCPUClock();
  746. text.push_back(CPUName);
  747. int tempsize = cpuTemp.size();
  748. for(int i =0; i < cpuLoad.size(); i++)
  749. {
  750. stringstream ss;//create a stringstream
  751. ss << i;//add number to the stream
  752. string tempText = "";
  753. tempText = tempText.append(ss.str());
  754. if(i < tempsize)
  755. {
  756. tempText = tempText.append(": ").append(cpuTemp[i]).append("°C - ").append(cpuLoad[i]).append("% - ").append(CPUClock[i]).append("MHz");
  757. }
  758. else
  759. {
  760. tempText = tempText.append(": ").append(cpuTemp[tempsize-1]).append("°C - ").append(cpuLoad[i]).append("% - ").append(CPUClock[i]).append("MHz");
  761. }
  762. text.push_back(tempText);
  763. ss.clear();
  764. }
  765. return text;
  766. }
  767. vector<string> WMI::getGPUText()
  768. {
  769. if(pSvc == 0)
  770. {
  771. this->connectToWMI();
  772. }
  773. text.clear();
  774. queryGPUName();
  775. queryGPUClock();
  776. queryGPUFan();
  777. queryGPULoad();
  778. queryGPUTemp();
  779. for(int i =0; i< GPUIdentifier.size(); i++)
  780. {
  781. stringstream ss;//create a stringstream
  782. ss << i;//add number to the stream
  783. text.push_back(GPUName[i]);
  784. text.push_back(string("CC/MC: ").append(GPUClock[i]).append(" / ").append(GPUMemoryClock[i]).append(" MHz"));
  785. string gpuTemp = "";
  786. gpuTemp = gpuTemp.append(GPUTemp[i]);
  787. gpuTemp = gpuTemp.append(" °C - ");
  788. gpuTemp = gpuTemp.append(GPULoad[i]);
  789. if(!GPUFan[i].empty())
  790. {
  791. gpuTemp = gpuTemp.append("% - ");
  792. gpuTemp = gpuTemp.append(GPUFan[i]);
  793. gpuTemp = gpuTemp.append("% fan");
  794. }
  795. else
  796. {
  797. gpuTemp = gpuTemp.append("%");
  798. }
  799. text.push_back(gpuTemp);
  800. ss.clear();
  801. }
  802. return text;
  803. }
  804. vector<string> WMI::getHDDText()
  805. {
  806. if(pSvc == 0)
  807. {
  808. this->connectToWMI();
  809. }
  810. text.clear();
  811. queryHDName();
  812. queryHDData();
  813. for(int i=0; i< HDDIdentifier.size(); i++)
  814. {
  815. if(HDDTemperature[i].empty())
  816. {
  817. text.push_back(HDDIdentifier[i].append(": ").append(HDDLoad[i]).append("% Used"));
  818. }
  819. else
  820. {
  821. text.push_back(HDDIdentifier[i].append(": ").append(HDDTemperature[i]).append("C - ").append(HDDLoad[i]).append("% Used"));
  822. }
  823. }
  824. sort(text.begin(), text.end());
  825. return text;
  826. }