- //只需初始化一次,初始化多次时会报错
- bool InitWmi(void)
- {
- HRESULT hres;
- hres = CoInitializeEx(0, COINIT_MULTITHREADED);
- if (FAILED(hres))
- {
- cout << "Failed to initialize COM library. Error code = 0x"<< hex << hres << endl;
- return FALSE;
- }
- hres = CoInitializeSecurity(
- NULL,
- -1, // COM authentication
- NULL, // Authentication services
- NULL, // Reserved
- RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
- RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
- NULL, // Authentication info
- EOAC_NONE, // Additional capabilities
- NULL // Reserved
- );
- if (FAILED(hres))
- {
- cout << "Failed to initialize security. Error code = 0x"<< hex << hres << endl;
- CoUninitialize();
- return FALSE;
- }
- return TRUE;
- }
- 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; }