c++ 使用wmi 获取系统相关信息

 

  
  
  
  
  1. //只需初始化一次,初始化多次时会报错 
  2. bool InitWmi(void) 
  3.     HRESULT hres; 
  4.     hres =  CoInitializeEx(0, COINIT_MULTITHREADED);   
  5.     if (FAILED(hres))  
  6.     {  
  7.         cout << "Failed to initialize COM library. Error code = 0x"<< hex << hres << endl;  
  8.         return FALSE;                  
  9.     } 
  10.     hres =  CoInitializeSecurity(  
  11.         NULL,   
  12.         -1,                          // COM authentication  
  13.         NULL,                        // Authentication services  
  14.         NULL,                        // Reserved  
  15.         RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication   
  16.         RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation    
  17.         NULL,                        // Authentication info  
  18.         EOAC_NONE,                   // Additional capabilities   
  19.         NULL                         // Reserved  
  20.         );  
  21.  
  22.     if (FAILED(hres))  
  23.     {  
  24.         cout << "Failed to initialize security. Error code = 0x"<< hex << hres << endl;  
  25.         CoUninitialize();  
  26.         return FALSE;                   
  27.     } 
  28.    return TRUE; 
  29.  
  30. BOOL mywmi::GetInfoWithWmi(BSTR sUsedWmiNamespacePath, // Object path of WMI namespace such as "ROOT\\CIMV2" BSTR sQueryStatement, //input you query statement,such as "SELECT * FROM Win32_Process" vector<WCHAR *>cQueryValue, //把要查询的值存放到vector中,such as Name ProcessID.. vector<vector<string> > &vRt //2维数组,保存所有查询到的项的值 ) { try { HRESULT hres; //创建一个CLSID_WebmLocator对象. IWbemLocator *pLoc = NULL; hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,IID_IWbemLocator, (LPVOID *) &pLoc); if (FAILED(hres)) { throw "Failed to create IWbemLocator object."; } //连接WMI IWbemServices *pSvc = NULL; hres = pLoc->ConnectServer( sUsedWmiNamespacePath, // Object path of WMI namespace NULL, // User name. NULL = current user NULL, // User password. NULL = current 0, // Locale. NULL indicates current NULL, // Security flags. 0, // Authority (e.g. Kerberos) 0, // Context object &pSvc // pointer to IWbemServices proxy ); if (FAILED(hres)) { pLoc->Release(); throw "Could not connect."; } // Step 5: -------------------------------------------------- // Set security levels on the proxy ------------------------- // 设置连接的安全级别 hres = CoSetProxyBlanket( pSvc, // Indicates the proxy to set RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx NULL, // Server principal name RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx NULL, // client identity EOAC_NONE // proxy capabilities ); if (FAILED(hres)) { pSvc->Release(); pLoc->Release(); throw "Could not set proxy blanket."; } //开始查询 IEnumWbemClassObject* pEnumerator = NULL; hres = pSvc->ExecQuery( bstr_t("WQL"), sQueryStatement, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { pSvc->Release(); pLoc->Release(); throw "Query for operating system name failed."; } // Step 7: ------------------------------------------------- // Get the data from the query in step 6 ------------------- //枚举查询 IWbemClassObject *pclsObj; ULONG uReturn = 0; string cTmp; while (pEnumerator) { vector<std::string>sTmpVec; HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if(0 == uReturn) { break; } VARIANT vtProp; for (UINT i = 0; i<cQueryValue.size();i++) { hr = pclsObj->Get(cQueryValue[i], 0, &vtProp, 0, 0); switch(vtProp.vt) { case 0: case 1: sTmpVec.push_back(""); break; case 3: { char buf[10]={0}; sprintf_s(buf,"%d",vtProp.intVal); sTmpVec.push_back(buf); } break; case 8: { char *buf = _com_util::ConvertBSTRToString(vtProp.bstrVal); sTmpVec.push_back(buf); delete buf; } break; default: sTmpVec.push_back(""); break; } VariantClear(&vtProp); } vRt.push_back(sTmpVec); pclsObj->Release(); } pSvc->Release(); pLoc->Release(); pEnumerator->Release(); } catch (char * str) { CoUninitialize(); cout<<str<<endl; return FALSE; } return TRUE; }

 

你可能感兴趣的:(C++)