HRESULT GetVideoMemoryViaWMI( WCHAR* strInputDeviceID, DWORD* pdwAdapterRam ) { HRESULT hr; bool bGotMemory = false; HRESULT hrCoInitialize = S_OK; IWbemLocator* pIWbemLocator = nullptr; IWbemServices* pIWbemServices = nullptr; BSTR pNamespace = nullptr; *pdwAdapterRam = 0; hrCoInitialize = CoInitialize( 0 ); hr = CoCreateInstance( CLSID_WbemLocator, nullptr, CLSCTX_INPROC_SERVER, IID_IWbemLocator, ( LPVOID* )&pIWbemLocator ); #ifdef PRINTF_DEBUGGING if( FAILED( hr ) ) wprintf( L"WMI: CoCreateInstance failed: 0x%0.8x\n", hr ); #endif if( SUCCEEDED( hr ) && pIWbemLocator ) { // Using the locator, connect to WMI in the given namespace. pNamespace = SysAllocString( L"\\\\.\\root\\cimv2" ); hr = pIWbemLocator->ConnectServer( pNamespace, nullptr, nullptr, 0L, 0L, nullptr, nullptr, &pIWbemServices ); #ifdef PRINTF_DEBUGGING if( FAILED( hr ) ) wprintf( L"WMI: pIWbemLocator->ConnectServer failed: 0x%0.8x\n", hr ); #endif if( SUCCEEDED( hr ) && pIWbemServices != 0 ) { HINSTANCE hinstOle32 = nullptr; hinstOle32 = LoadLibraryW( L"ole32.dll" ); if( hinstOle32 ) { PfnCoSetProxyBlanket pfnCoSetProxyBlanket = nullptr; pfnCoSetProxyBlanket = ( PfnCoSetProxyBlanket )GetProcAddress( hinstOle32, "CoSetProxyBlanket" ); if( pfnCoSetProxyBlanket != 0 ) { // Switch security level to IMPERSONATE. pfnCoSetProxyBlanket( pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, 0 ); } FreeLibrary( hinstOle32 ); } IEnumWbemClassObject* pEnumVideoControllers = nullptr; BSTR pClassName = nullptr; pClassName = SysAllocString( L"Win32_VideoController" ); hr = pIWbemServices->CreateInstanceEnum( pClassName, 0, nullptr, &pEnumVideoControllers ); #ifdef PRINTF_DEBUGGING if( FAILED( hr ) ) wprintf( L"WMI: pIWbemServices->CreateInstanceEnum failed: 0x%0.8x\n", hr ); #endif if( SUCCEEDED( hr ) && pEnumVideoControllers ) { IWbemClassObject* pVideoControllers[10] = {0}; DWORD uReturned = 0; BSTR pPropName = nullptr; // Get the first one in the list pEnumVideoControllers->Reset(); hr = pEnumVideoControllers->Next( 5000, // timeout in 5 seconds 10, // return the first 10 pVideoControllers, &uReturned ); #ifdef PRINTF_DEBUGGING if( FAILED( hr ) ) wprintf( L"WMI: pEnumVideoControllers->Next failed: 0x%0.8x\n", hr ); if( uReturned == 0 ) wprintf( L"WMI: pEnumVideoControllers uReturned == 0\n" ); #endif VARIANT var; if( SUCCEEDED( hr ) ) { bool bFound = false; for( UINT iController = 0; iController < uReturned; iController++ ) { if ( !pVideoControllers[iController] ) continue; pPropName = SysAllocString( L"PNPDeviceID" ); hr = pVideoControllers[iController]->Get( pPropName, 0L, &var, nullptr, nullptr ); #ifdef PRINTF_DEBUGGING if( FAILED( hr ) ) wprintf( L"WMI: pVideoControllers[iController]->Get PNPDeviceID failed: 0x%0.8x\n", hr ); #endif if( SUCCEEDED( hr ) ) { if( wcsstr( var.bstrVal, strInputDeviceID ) != 0 ) bFound = true; } VariantClear( &var ); if( pPropName ) SysFreeString( pPropName ); if( bFound ) { pPropName = SysAllocString( L"AdapterRAM" ); hr = pVideoControllers[iController]->Get( pPropName, 0L, &var, nullptr, nullptr ); #ifdef PRINTF_DEBUGGING if( FAILED( hr ) ) wprintf( L"WMI: pVideoControllers[iController]->Get AdapterRAM failed: 0x%0.8x\n", hr ); #endif if( SUCCEEDED( hr ) ) { bGotMemory = true; *pdwAdapterRam = var.ulVal; } VariantClear( &var ); if( pPropName ) SysFreeString( pPropName ); break; } SAFE_RELEASE( pVideoControllers[iController] ); } } SAFE_RELEASE(pEnumVideoControllers); } if( pClassName ) SysFreeString( pClassName ); } if( pNamespace ) SysFreeString( pNamespace ); SAFE_RELEASE( pIWbemServices ); } SAFE_RELEASE( pIWbemLocator ); if( SUCCEEDED( hrCoInitialize ) ) CoUninitialize(); if( bGotMemory ) return S_OK; else return E_FAIL; }
tstring CCliMgr::GetWmiInfo( LPCTSTR lpszClass,LPCTSTR lpszField) { tstring SysInfo,strtmp; HRESULT ret; ret = CoInitialize(NULL); ret = CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0 ); if (ret == S_OK || ret== RPC_E_TOO_LATE ) { IWbemLocator * pIWbemLocator = NULL; IWbemServices * pWbemServices = NULL; IEnumWbemClassObject * pEnumObject = NULL; BSTR bstrNamespace = (L"root\\cimv2");// 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息 if(CoCreateInstance (CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown ,(void**)&pIWbemLocator) == S_OK) { if(pIWbemLocator->ConnectServer( bstrNamespace, // Namespace NULL, // Userid NULL, // PW NULL, // Locale 0, // flags NULL, // Authority NULL, // Context &pWbemServices ) == S_OK) { HRESULT hRes; _bstr_t strQuery = (L"Select * from Win32_OperatingSystem"); hRes = pWbemServices->ExecQuery(_bstr_t("WQL"), strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject); if(hRes == S_OK) { ULONG uCount = 1, uReturned; IWbemClassObject * pClassObject = NULL; hRes = pEnumObject->Reset(); if(hRes == S_OK) { hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned); if(hRes == S_OK) { strtmp = "操作系统的名称: "+_getWmiInfo(pClassObject,"Caption")+"\r\n"; SysInfo = SysInfo + strtmp; } if (pClassObject != NULL) pClassObject->Release(); } } strQuery = (L"Select * from Win32_DiskDrive"); hRes = pWbemServices->ExecQuery(_bstr_t("WQL"), strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject); if(hRes == S_OK) { ULONG uCount = 1, uReturned; IWbemClassObject * pClassObject = NULL; hRes = pEnumObject->Reset(); if(hRes == S_OK) { hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned); if(hRes == S_OK) { strtmp = "硬盘的Model: "+_getWmiInfo(pClassObject,"Model")+"\r\n"; SysInfo = SysInfo + strtmp; m_pLogger->WriteLog(LM_INFO, TEXT("硬盘的Model: %s"),_getWmiInfo(pClassObject,"Model").c_str()); } if (pClassObject != NULL) pClassObject->Release(); } } strQuery = (L"Select * from Win32_LogicalDisk"); hRes = pWbemServices->ExecQuery(_bstr_t("WQL"), strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject); if(hRes == S_OK) { ULONG uCount = 1, uReturned; IWbemClassObject * pClassObject = NULL; hRes = pEnumObject->Reset(); if(hRes == S_OK) { while( pEnumObject->Next( WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK ) { if (atoi(_getWmiInfo(pClassObject,"DriveType").c_str()) != 3) continue; tstring str; str =_getWmiInfo(pClassObject,"Name"); strtmp = "盘符: " +str+" "; m_pLogger->WriteLog(LM_INFO, TEXT("盘符: %s"),str.c_str()); tstring drv; drv += toupper(str[0]); if(m_Hy.IsDriverProtected((char *)drv.c_str())) { strtmp = strtmp+ "还原状态: 保护 "; m_pLogger->WriteLog(LM_INFO, TEXT("还原状态: 保护 ")); } else { strtmp = strtmp+ "还原状态: 未保护 "; m_pLogger->WriteLog(LM_INFO, TEXT("还原状态: 未保护 ")); } strtmp = strtmp+ " 剩余空间: "+_getWmiInfo(pClassObject,"FreeSpace")+"\r\n"; SysInfo = SysInfo + strtmp; m_pLogger->WriteLog(LM_INFO, TEXT("剩余空间: %s"),_getWmiInfo(pClassObject,"FreeSpace").c_str()); } if (pClassObject != NULL) pClassObject->Release(); } } strQuery = (L"Select * from Win32_processor"); hRes = pWbemServices->ExecQuery(_bstr_t("WQL"), strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject); if(hRes == S_OK) { ULONG uCount = 1, uReturned; IWbemClassObject * pClassObject = NULL; hRes = pEnumObject->Reset(); if(hRes == S_OK) { hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned); if(hRes == S_OK) { strtmp = "处理器的制造厂商: "+_getWmiInfo(pClassObject,"Manufacturer") + "\r\n"; strtmp = strtmp + "处理器的名称: "+_getWmiInfo(pClassObject,"Caption") + "\r\n"; SysInfo = SysInfo + strtmp; m_pLogger->WriteLog(LM_INFO, TEXT("处理器的制造厂商: %s"),_getWmiInfo(pClassObject,"Manufacturer").c_str()); m_pLogger->WriteLog(LM_INFO, TEXT("处理器的名称: %s"),_getWmiInfo(pClassObject,"Caption").c_str()); } if (pClassObject != NULL) pClassObject->Release(); } } strQuery = (L"Select * from Win32_LogicalMemoryConfiguration"); hRes = pWbemServices->ExecQuery(_bstr_t("WQL"), strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject); if(hRes == S_OK) { ULONG uCount = 1, uReturned; IWbemClassObject * pClassObject = NULL; hRes = pEnumObject->Reset(); if(hRes == S_OK) { hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned); if(hRes == S_OK) { strtmp = "物理内存大小: "+_getWmiInfo(pClassObject,"TotalPhysicalMemory") + "\r\n"; strtmp = strtmp + "页面文件大小: "+_getWmiInfo(pClassObject,"TotalPageFileSpace") + "\r\n"; strtmp = strtmp + "虚拟内存大小: "+_getWmiInfo(pClassObject,"TotalVirtualMemory") + "\r\n"; SysInfo = SysInfo + strtmp; m_pLogger->WriteLog(LM_INFO, TEXT("物理内存大小: %s"),_getWmiInfo(pClassObject,"TotalPhysicalMemory").c_str()); m_pLogger->WriteLog(LM_INFO, TEXT("页面文件大小: %s"),_getWmiInfo(pClassObject,"TotalPageFileSpace").c_str()); m_pLogger->WriteLog(LM_INFO, TEXT("虚拟内存大小: %s"),_getWmiInfo(pClassObject,"TotalVirtualMemory").c_str()); } if (pClassObject != NULL) pClassObject->Release(); } } strQuery = (L"Select * from Win32_VideoController"); hRes = pWbemServices->ExecQuery(_bstr_t("WQL"), strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject); if(hRes == S_OK) { ULONG uCount = 1, uReturned; IWbemClassObject * pClassObject = NULL; hRes = pEnumObject->Reset(); if(hRes == S_OK) { hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned); if(hRes == S_OK) { strtmp = "显卡名称: "+_getWmiInfo(pClassObject,"Name") + "\r\n"; SysInfo = SysInfo + strtmp; m_pLogger->WriteLog(LM_INFO, TEXT("显卡名称: %s"),_getWmiInfo(pClassObject,"Name").c_str()); } if (pClassObject != NULL) pClassObject->Release(); } } strQuery = (L"Select * from Win32_NetworkAdapter where (Netconnectionstatus = 2)"); hRes = pWbemServices->ExecQuery(_bstr_t("WQL"), strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject); if(hRes == S_OK) { ULONG uCount = 1, uReturned; IWbemClassObject * pClassObject = NULL; hRes = pEnumObject->Reset(); if(hRes == S_OK) { while( pEnumObject->Next( WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK ) { tstring str; str =_getWmiInfo(pClassObject,"MACAddress"); if (!str.empty()) { str =_getWmiInfo(pClassObject,"PNPDeviceID"); size_t pos = str.find("PCI",0); if (pos != tstring::npos) { tstring str = _getWmiInfo(pClassObject,"Caption"); size_t pos = str.find(']'); if (pos != tstring::npos) str = str.substr(pos+1); strtmp = "网卡名称: "+str + "\r\n"; SysInfo = SysInfo + strtmp; m_pLogger->WriteLog(LM_INFO, "网卡名称: %s" , str.c_str()) ; } } } if (pClassObject != NULL) pClassObject->Release(); } } } } if (pIWbemLocator) pIWbemLocator->Release(); if (pWbemServices) pWbemServices->Release(); if (pEnumObject) pEnumObject->Release(); } CoUninitialize(); if (!SysInfo.empty()) { m_SysInfo = SysInfo; } return SysInfo; }
void QueryNetworkAdapters(std::string query, std::vector<NetworkAdapter*> &vAdapters) { std::cout << "1: " << &vAdapters << std::endl; HRESULT hRes; // // Step 1: -------------------------------------------------- // // Initialize COM. ------------------------------------------ // hRes = CoInitializeEx(0, COINIT_MULTITHREADED); // if (FAILED(hRes)) // { // std::cout << "Failed to initialize COM library. Error code = 0x" << std::hex << hRes << std::endl; // } // // Step 2: -------------------------------------------------- // // Set general COM security levels -------------------------- // // Note: If you are using Windows 2000, you need to specify - // // the default authentication credentials for a user by using // // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ---- // // parameter of CoInitializeSecurity ------------------------ // 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)) //{ // std::cout << "Failed to initialize security. Error code = 0x" << std::hex << hRes << std::endl; // CoUninitialize(); //} IWbemLocator* pIWbemLocator = NULL; IWbemServices* pWbemServices = NULL; BSTR bstrNamespace = (L"root\\cimv2"); hRes = CoCreateInstance( CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown, (void **)&pIWbemLocator ); if (SUCCEEDED(hRes)) { hRes = pIWbemLocator->ConnectServer( L"root\\cimv2", // Namespace NULL, // Userid NULL, // PW NULL, // Locale 0, // flags NULL, // Authority NULL, // Context &pWbemServices ); } // Format query query = "SELECT * FROM Win32_NetworkAdapter " + query; BSTR bsQuery = ConvertStringToBSTR(query.c_str()); IEnumWbemClassObject* pEnumrator = NULL; hRes = pWbemServices->ExecQuery(L"WQL", bsQuery, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumrator); if (FAILED(hRes)) { std::cout << "Failed pWbemServices->ExecQuery. Error code = 0x" << std::hex << hRes << std::endl; CoUninitialize(); } IWbemClassObject* pClassObject[128] = {0}; ULONG uReturned; hRes = pEnumrator->Reset(); hRes = pEnumrator->Next(WBEM_INFINITE, 128, pClassObject, &uReturned); if (FAILED(hRes)) { std::cout << "Failed pEnumObject->Next. Error code = 0x" << std::hex << hRes << std::endl; CoUninitialize(); } // Fill the vector for (int i = 0; i < uReturned; i++) { _variant_t guid; pClassObject[i]->Get(L"GUID", 0, &guid, NULL, NULL); //vNetworkAdapters.push_back(new NetworkAdapter(std::string(_com_util::ConvertBSTRToString(guid.bstrVal)))); if (guid.vt == VT_BSTR) { //NetworkAdapter na(std::string(_com_util::ConvertBSTRToString(guid.bstrVal))); //vAdapters.push_back(new NetworkAdapter("{D15F65F4-27C5-4548-8705-B50B5B360737}")); char* chBuf = ConvertBSTRToString(guid.bstrVal); vAdapters.push_back(new NetworkAdapter(std::string(chBuf))); delete[] chBuf; //std::cout << _com_util::ConvertBSTRToString(guid.bstrVal) << std::endl; } pClassObject[i]->Release(); } pEnumrator->Release(); pIWbemLocator->Release(); pWbemServices->Release(); }