请选择 进入手机版 | 继续访问电脑版
查看: 403|回复: 0

[软件工程] wmi获取系统信息及性能信息

715

主题

715

帖子

2162

积分

猿er

Rank: 1

积分
2162
发表于 2016-8-14 17:54:13
wmi获取系统信息及性能信息 vs2008 windows平台
  1. #ifndef MY_WMI_CLASS_H__
  2. #define MY_WMI_CLASS_H__
  3. /*******************************************************************************
  4. * @file
  5. * @author def< qq group: 324164944 >
  6. * @blog http://www.cnblogs.com/itdef/
  7. * @brief
  8. /*******************************************************************************/
  9. #include <Wbemidl.h>
  10. namespace DEF
  11. {
  12. class CWMI
  13. {
  14. IWbemLocator *pLoc_;
  15. IWbemServices *pSvc_;
  16. public:
  17. CWMI():pLoc_(NULL),pSvc_(NULL){}
  18. ~CWMI();
  19. BOOL QueryInfo(const char* strSQL, const char* strAttr );
  20. BOOL Initialize();
  21. };
  22. } //namespace DEF
  23. #endif //MY_WMI_CLASS_H__
复制代码
  1. /*******************************************************************************
  2. * @file
  3. * @author def< qq group: 324164944 >
  4. * @blog http://www.cnblogs.com/itdef/
  5. * @brief
  6. /*******************************************************************************/
  7. #include "CMyWMIClass.h"
  8. #include <iostream>
  9. #include <comutil.h>
  10. using namespace DEF;
  11. using namespace std;
  12. #pragma comment(lib,"Wbemuuid")
  13. #pragma comment(lib,"comsuppw")
  14. BOOL CWMI::Initialize()
  15. {
  16. BOOL bRet = FALSE;
  17. HRESULT hres; //定义COM调用的返回
  18. try{
  19. hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  20. if (FAILED(hres))
  21. {
  22. throw exception("CoInitializeEx() error.");
  23. }
  24. hres = CoInitializeSecurity(
  25. NULL,
  26. -1, // COM authentication
  27. NULL, // Authentication services
  28. NULL, // Reserved
  29. RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
  30. RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
  31. NULL, // Authentication info
  32. EOAC_NONE, // Additional capabilities
  33. NULL // Reserved
  34. );
  35. if (FAILED(hres))
  36. {
  37. throw exception("CoInitializeEx() error.");
  38. }
  39. hres = CoCreateInstance(
  40. CLSID_WbemLocator,
  41. 0,
  42. CLSCTX_INPROC_SERVER,
  43. IID_IWbemLocator, (LPVOID *) &pLoc_);
  44. if (FAILED(hres))
  45. {
  46. throw exception("CoCreateInstance() error.");
  47. }
  48. // to make IWbemServices calls.
  49. hres = pLoc_->ConnectServer(
  50. _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
  51. NULL, // User name. NULL = current user
  52. NULL, // User password. NULL = current
  53. 0, // Locale. NULL indicates current
  54. NULL, // Security flags.
  55. 0, // Authority (e.g. Kerberos)
  56. 0, // Context object
  57. &pSvc_ // pointer to IWbemServices proxy
  58. );
  59. if (FAILED(hres))
  60. {
  61. throw exception("ConnectServer() error.");
  62. }
  63. hres = CoSetProxyBlanket(
  64. pSvc_, // Indicates the proxy to set
  65. RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
  66. RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
  67. NULL, // Server principal name
  68. RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
  69. RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
  70. NULL, // client identity
  71. EOAC_NONE // proxy capabilities
  72. );
  73. if (FAILED(hres))
  74. {
  75. throw exception("CoSetProxyBlanket() error.");
  76. }
  77. bRet = TRUE;
  78. }catch(exception& e)
  79. {
  80. cout << e.what() << endl;
  81. }
  82. return bRet;
  83. }
  84. CWMI::~CWMI()
  85. {
  86. if( NULL != pSvc_)
  87. {
  88. pSvc_->Release();
  89. pSvc_ = NULL;
  90. }
  91. if(pLoc_ != NULL )
  92. {
  93. pLoc_->Release();
  94. pLoc_ = NULL;
  95. }
  96. CoUninitialize();
  97. }
  98. BOOL CWMI::QueryInfo(const char* strSQL, const char* strAttr )
  99. {
  100. HRESULT hres; //定义COM调用的返回
  101. IWbemClassObject* pclsObj = NULL;
  102. IEnumWbemClassObject* pEnum = NULL;
  103. BOOL bRet = FALSE;
  104. if( NULL == strSQL ||
  105. NULL == strAttr)
  106. {
  107. return FALSE;
  108. }
  109. try{
  110. hres = pSvc_->ExecQuery(
  111. bstr_t("WQL"),
  112. bstr_t(strSQL),
  113. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  114. NULL,
  115. &pEnum);
  116. if (FAILED(hres))
  117. {
  118. throw exception("ExecQuery() error.");
  119. }
  120. ULONG uReturn = 0;
  121. while( pEnum )
  122. {
  123. HRESULT hr = pEnum->Next( WBEM_INFINITE, 1, &pclsObj, &uReturn );
  124. if( 0 == uReturn )
  125. {
  126. break;
  127. }
  128. VARIANT vtProp;
  129. VariantInit( &vtProp );
  130. hr = pclsObj->Get( bstr_t( strAttr ), 0, &vtProp, 0, 0 );
  131. if (SUCCEEDED(hr) && (V_VT(&vtProp) == VT_BSTR))
  132. {
  133. char* str = _com_util::ConvertBSTRToString( vtProp.bstrVal );
  134. //printf( "%s, length = %d \n", str, strlen( str ) );
  135. printf("%s \n",str);
  136. delete [] str;
  137. }
  138. VariantClear( &vtProp );
  139. }
  140. bRet = TRUE;
  141. }catch(exception& e)
  142. {
  143. cout << e.what() << endl;
  144. }
  145. if(pclsObj != NULL)
  146. {
  147. pclsObj->Release();
  148. pclsObj = NULL;
  149. }
  150. if(pEnum != NULL)
  151. {
  152. pEnum->Release();
  153. pEnum = NULL;
  154. }
  155. return bRet;
  156. }
复制代码
  1. /*******************************************************************************
  2. * @file
  3. * @author def< qq group: 324164944 >
  4. * @blog http://www.cnblogs.com/itdef/
  5. * @brief
  6. /*******************************************************************************/
  7. #include <iostream>
  8. #include "CMyWMIClass.h"
  9. using namespace std;
  10. int _tmain(int argc, _TCHAR* argv[])
  11. {
  12. while(1)
  13. {
  14. Sleep(3000);
  15. DEF::CWMI a;
  16. a.Initialize();
  17. cout << endl << "Caption ";
  18. a.QueryInfo("SELECT * FROM Win32_OperatingSystem","Caption");
  19. cout << "Manufacturer ";
  20. a.QueryInfo("SELECT * FROM Win32_OperatingSystem","Manufacturer");
  21. cout << "CSName ";
  22. a.QueryInfo("SELECT * FROM Win32_OperatingSystem","CSName");
  23. cout << "PNPDeviceID ";
  24. a.QueryInfo("SELECT * FROM Win32_DiskDrive", "PNPDeviceID");
  25. cout << "Disk SerialNumber ";
  26. a.QueryInfo("SELECT * FROM Win32_PhysicalMedia", "SerialNumber");
  27. cout << "TotalVisibleMemorySize ";
  28. a.QueryInfo("SELECT * FROM Win32_OperatingSystem","TotalVisibleMemorySize");
  29. cout << "FreePhysicalMemory ";
  30. a.QueryInfo("SELECT * FROM Win32_OperatingSystem","FreePhysicalMemory");
  31. cout << "SerialNumber ";
  32. a.QueryInfo("SELECT * FROM Win32_DiskDrive","SerialNumber");
  33. // cout << "Process Caption ";
  34. // a.QueryInfo("SELECT * FROM Win32_Process","Caption");
  35. //
  36. // cout << "Process Handle ";
  37. // a.QueryInfo("SELECT * FROM Win32_Process","Handle");
  38. //
  39. //
  40. // cout << "Process ExecutablePath ";
  41. // a.QueryInfo("SELECT * FROM Win32_Process","ExecutablePath");
  42. }
  43. return 0;
  44. }
复制代码


回复

使用道具 举报