本文共 7738 字,大约阅读时间需要 25 分钟。
导读:WMI使用技巧集
很多的朋友对WMI可能见过但理解不深,我也是十分想了解关于WMI的知识,可一直找不对太合适的资料,在网上的一些资料不是有很多错误,就是讲解不清,我有空的时候将关于WMI的知识集中一下,放在这里便于大家学习。本贴会不断增加。 1、 什么是WMI WMI是英文Windows Management Instrumentation的简写,它的功能主要是:访问本地主机的一些信息和服务,可以管理远程计算机(当然你必须要拥有足够的权限),比如:重启,关机,关闭进程,创建进程等。 2、 如何用WMI获得本地磁盘的信息? 首先要在VS.NET中创建一个项目,然后在添加引用中引用一个.net的装配件:System.Management.dll,这样你的项目才能使用WMI。代码如下: using System; using System.Management; class Sample_ManagementObject { public static int Main(string[] args) { SelectQuery query=new SelectQuery("Select * From Win32_LogicalDisk"); ManagementObjectSearcher searcher=new ManagementObjectSearcher(query); foreach(ManagementBaseObject disk in searcher.Get()) { Console.WriteLine("/r/n"+disk["Name"] +" "+disk["DriveType"] + " " + disk["VolumeName"]); } Console.ReadLine(); return 0; } } disk["DriveType"] 的返回值意义如下: 1 No type 2 Floppy disk 3 Hard disk 4 Removable drive or network drive 5 CD-ROM 6 RAM disk 3、如何用WMI获得指定磁盘的容量? using System; using System.Management; // This example demonstrates reading a property of a ManagementObject. class Sample_ManagementObject { public static int Main(string[] args) { ManagementObject disk = new ManagementObject( "win32_logicaldisk.deviceid=/"c:/""); disk.Get(); Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes"); Console.ReadLine(); return 0; } }C#等对WMI支持比较好,使用也方便,
vc6中使用比较的方法资料很少问一下,win98能支持WMI吗?我看有的人说支持,也有人说不支持
WMI基本是独立于系统的,在某些系统中默认包含WMI,其他系统需要安装WMI支持。
VC的例子
http://www.codeguru.com/Cpp/W-P/system/misc/article.php/c5675/ 另外98的wmi下载 http://www.lingbo.cn/download.asp wmi9x.exeManagementObjectSearcher searcher=new ManagementObjectSearcher(query);
foreach(ManagementBaseObject disk in searcher.Get()) {// Get WMI information.
//tested by onega VC20003,windows 2003 #include "stdafx.h" #define _WIN32_DCOM #include using namespace std; #include #include #include # pragma comment(lib, "wbemuuid.lib") struct InitOle { InitOle() { hres = CoInitializeEx(0, COINIT_MULTITHREADED); } ~InitOle() { ::CoUninitialize(); } HRESULT hres; } _init_InitOle_; HRESULT PrintObject(IWbemClassObject* spInstance) { if(!spInstance) return S_OK; std::cout<<__FUNCTION__<<" start "< LPSAFEARRAY psa = NULL; HRESULT hres; hres = spInstance->GetNames( NULL, WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY, NULL, &psa); long lLower, lUpper; SafeArrayGetLBound(psa , 1, &lLower); SafeArrayGetUBound(psa , 1, &lUpper); for (long i = lLower; i <= lUpper; ++i) { CComBSTR bstrPropName; if (S_OK != (hres = SafeArrayGetElement(psa, &i, &bstrPropName)) ) { if (NULL != psa) SafeArrayDestroy(psa); return hres; } //if(bstrPropName!="name") // continue; std::cout<<" "<<(LPCTSTR)_bstr_t(bstrPropName); _variant_t varProperty; HRESULT hr = spInstance->Get(bstrPropName,0,&varProperty,NULL,0); std::cout<<" = "; if(varProperty.vt !=VT_EMPTY && VT_NULL != varProperty.vt) { _variant_t vDest; hr = VariantChangeType(&vDest, &varProperty,0,VT_BSTR); if(SUCCEEDED(hr)) std::cout<< (LPCTSTR)_bstr_t(varProperty); else { if(varProperty.vt == (VT_ARRAY|VT_I4)) { SAFEARRAY* psa = varProperty.parray; int *pIntArray = NULL; SafeArrayAccessData(psa,(VOID**)&pIntArray); UINT uDim = SafeArrayGetDim(psa); if(1==uDim) { long lLbound,lRbound; SafeArrayGetLBound(psa,1,&lLbound); SafeArrayGetUBound(psa,1,&lRbound); for(long i=lLbound;i<=lRbound;i++) { std::cout<< pIntArray[i]<<" "; } } SafeArrayUnaccessData(psa); } else { std::cout<< varProperty.vt << " type "; } } } else { if(varProperty.vt == VT_EMPTY) std::cout<< "VT_EMPTY "; else std::cout<< "VT_NULL"; } std::cout< } if (NULL != psa) SafeArrayDestroy(psa); std::cout<<__FUNCTION__<<" end "< return S_OK; } int get_wmi_info(CComPtr & pSvc,_bstr_t classname) { // Step 6: -------------------------------------------------- // Use the IWbemServices pointer to make requests of WMI ---- _bstr_t wmi_query_str("SELECT * FROM "); wmi_query_str +=classname; CComPtr pEnumerator;//IEnumWbemClassObject* pEnumerator = NULL; HRESULT hres = pSvc->ExecQuery( bstr_t("WQL"), wmi_query_str, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { cout << "Query for "<<(LPCSTR)classname<<" failed." << " Error code = 0x" << hex << hres << endl; return 1; // Program has failed. } // Step 7: ------------------------------------------------- // Get the data from the query in step 6 ------------------- ULONG uReturn = 0; while (pEnumerator) { CComPtr pclsObj;//IWbemClassObject *pclsObj; HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if(0 == uReturn) { break; } PrintObject(pclsObj); } return 0; } int main(int argc, char **argv) { HRESULT hres = S_OK; if (FAILED(_init_InitOle_.hres)) { cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl; return 1; // Program has failed. } // 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)) { cout << "Failed to initialize security. Error code = 0x" << hex << hres << endl; return 1; // Program has failed. } // Step 3: --------------------------------------------------- // Obtain the initial locator to WMI ------------------------- CComPtr pLoc;//IWbemLocator *pLoc = NULL; 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; return 1; // Program has failed. } // Step 4: ----------------------------------------------------- // Connect to WMI through the IWbemLocator::ConnectServer method CComPtr pSvc;//IWbemServices *pSvc = NULL; // 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"), // 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)) { cout << "Could not connect. Error code = 0x" << hex << hres << endl; return 1; // Program has failed. } cout << "Connected to ROOT//CIMV2 WMI namespace" << endl; // 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)) { cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl; return 1; // Program has failed. } get_wmi_info(pSvc,"Win32_NetworkAdapter"); return 0; // Program successfully completed. } 本文转自转载地址:http://dniob.baihongyu.com/