WMI可以进行的操作很多,想Windows优化大师就有很多操作是通过WMI进行的。在这篇文章中主要是给出一个使用WMI来获取数据的一个范例,如果要获取其他数据,可以先使用WMI studio tools查询,然后copy代码就可以了。我要贴上去代码也是msdn中的范例了。
<textarea cols="77" rows="18" name="code" class="cpp">int GetStorageInfo(CString szClass,REQHARDWAREINFO& HardInfo) { HRESULT hres ; HTREEITEM hItem; CString szWQL = _T("SELECT FileSystem,FreeSpace,Name,Size,DriveType from ") + szClass; // Initialize COM. hres=CoInitializeEx(0,COINIT_MULTITHREADED); if(FAILED(hres)) { cout<<"Failed to initialize COM library. Error code = 0x" <<hex<<hres<<endl ; return 1 ; // Program has failed. } // Initialize hres=CoInitializeSecurity( NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL); if(FAILED(hres)) { cout<<"Failed to initialize security. Error code = 0x" <<hex<<hres<<endl ; CoUninitialize(); return 1 ; // Program has failed. } // Obtain the initial locator to Windows Management on a particular host computer. IWbemLocator*pLoc=0 ; hres=CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER, IID_IWbemLocator,(LPVOID*)&pLoc); if(FAILED(hres)) { cout<<"Failed to create IWbemLocator object. Err code = 0x" <<hex<<hres<<endl ; CoUninitialize(); return 1 ; // Program has failed. } IWbemServices*pSvc=0 ; // Connect to the root/cimv2 namespace with the current user and obtain pointer pSvc // to make IWbemServices calls. hres=pLoc->ConnectServer(_bstr_t(L"ROOT//CIMV2"), NULL,NULL,0,NULL,0,0,&pSvc); if(FAILED(hres)) { cout<<"Could not connect. Error code = 0x" <<hex<<hres<<endl ; pLoc->Release(); CoUninitialize(); return 1 ; // Program has failed. } cout<<"Connected to WMI"<<endl ; // Set the IWbemServices proxy so that impersonation of the user (client) occurs. 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)) { cout<<"Could not set proxy blanket. Error code = 0x" <<hex<<hres<<endl ; pSvc->Release(); pLoc->Release(); CoUninitialize(); return 1 ; // Program has failed. } // Use the IWbemServices pointer to make requests of WMI. // Make requests here: // For example, query for print queues that have more than 10 jobs IEnumWbemClassObject*pEnumerator=NULL ; hres=pSvc->ExecQuery( bstr_t("WQL"), bstr_t((LPTSTR)(LPCTSTR)szWQL), WBEM_FLAG_FORWARD_ONLY|WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if(FAILED(hres)) { cout<<"Query for print queues failed. Error code = 0x" <<hex<<hres<<endl ; pSvc->Release(); pLoc->Release(); CoUninitialize(); return 1 ; // Program has failed. } else { // clist.DeleteAllItems(); IWbemClassObject*pInstance=NULL ; ULONG dwCount ; while(pEnumerator->Next( WBEM_INFINITE, 1, &pInstance, &dwCount)==S_OK) { SAFEARRAY*pvNames=NULL ; VARIANT vName,vFileSystem,vFreeSpace,vSize,vDriveType; pInstance->Get(L"DriveType",0,&vDriveType,NULL,NULL); CString szDriveType = VariantToString(vDriveType); //固定磁盘检查 if (szDriveType.Compare("3")!=0) { continue; } pInstance->Get(L"FileSystem",0,&vFileSystem,NULL,NULL); CString szFileSystem = VariantToString(vFileSystem); pInstance->Get(L"FreeSpace",0,&vFreeSpace,NULL,NULL); CString szFreeSpace = VariantToString(vFreeSpace); pInstance->Get(L"Size",0,&vSize,NULL,NULL); CString szSize = VariantToString(vSize); pInstance->Get(L"Name",0,&vName,NULL,NULL); CString szName = VariantToString(vName); if(pvNames)SafeArrayDestroy(pvNames); } if(pInstance)pInstance->Release(); } // Cleanup // ======== pEnumerator->Release(); pSvc->Release(); pLoc->Release(); CoUninitialize(); return 0; } </textarea>