最近需要做下用户信息统计,发现WMI真是个好东西,同时觉得COM这东西,还真不能少>_<!!
下面是源码:
/******************************************************************** created: 2014/02/18 author: baesky purpose: Gater user infomation *********************************************************************/ #include <comdef.h> #include <Wbemidl.h> # pragma comment(lib, "wbemuuid.lib") struct UserInfo { /** your spec*/ }; /** video card and OS */ bool GetUserInfo(UserInfo& Info) { HRESULT hres; hres = CoInitialize( NULL ); if (FAILED(hres)) { return false; } hres = CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL ); if (FAILED(hres)) { CoUninitialize(); return false; } IWbemLocator *pLoc = NULL; hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc); if (FAILED(hres)) { CoUninitialize(); return false; } IWbemServices *pSvc = NULL; hres = pLoc->ConnectServer( _bstr_t(L"ROOT\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc ); if (FAILED(hres)) { pLoc->Release(); CoUninitialize(); return false; } hres = CoSetProxyBlanket( pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE ); if (FAILED(hres)) { pSvc->Release(); pLoc->Release(); CoUninitialize(); return false; } IEnumWbemClassObject* pEnumerator = NULL; hres = pSvc->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * FROM Win32_OperatingSystem"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { pSvc->Release(); pLoc->Release(); CoUninitialize(); return false; } IWbemClassObject *pclsObj; ULONG uReturn = 0; while (pEnumerator) { HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if(0 == uReturn) { break; } VARIANT vtProp; hr = pclsObj->Get(L"Caption", 0, &vtProp, 0, 0); //os //vtProp.bstrVal is what you need. //VariantClear(&vtProp); //hr = pclsObj->Get(L"CSDVersion", 0, &vtProp, 0, 0); // example "sp3" //VariantClear(&vtProp); //hr = pclsObj->Get(L"OSArchitecture", 0, &vtProp, 0, 0); // example x64 //VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); pEnumerator=NULL; hres = pSvc->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * FROM Win32_VideoController"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { pSvc->Release(); pLoc->Release(); CoUninitialize(); return false; } while (pEnumerator) { HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if(0 == uReturn) { break; } VARIANT vtProp; hr = pclsObj->Get(L"Caption", 0, &vtProp, 0, 0); //video desc /** .,,,,,,,,,,,,,,,,,,,,,,your logic */ VariantClear(&vtProp); pclsObj->Release(); } pEnumerator->Release(); pEnumerator=NULL; hres = pSvc->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * FROM Win32_Processor"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { pSvc->Release(); pLoc->Release(); CoUninitialize(); return false; } while (pEnumerator) { HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if(0 == uReturn) { break; } VARIANT vtProp; hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0); //video desc /** .,,,,,,,,,,,,,,,,,,,,,,your logic */ VariantClear(&vtProp); pclsObj->Release(); } pSvc->Release(); pLoc->Release(); pEnumerator->Release(); CoUninitialize(); return true; }