Get system information via WMI
Summary: WMI: Windows Management Instrumentation (Windows Administration Tool) can get almost all system information such as motherboard, BIOS, disk, graphics card, network through WMI. Use this tool to manage almost all information in the local or client system. Many network management tools are developed based on WMI. With this tool in Windows NT / 2000 / XP / 2003, you can choose to install this tool in Windows 98. 1 Initializing the COM interface: Access WMI, you must initialize the COM interface, call Coinitialize (NULL) at the beginning of the program; initialize, call Couninitialize (); release resources. These two functions are defined in #include
2 Get access WMI privilege: Coinitializesecurity (null, -1, null, null, rpc_c_authn_level_pkt, rpc_c_imp_level_impersonate, null, eoac_none, 0); if this function returns S_OK to obtain permissions, otherwise it will fail.
3 Access WMI through IWBemlocator and IWBemServices, get system information: Parameters of this function: LPList Return information, WSClass is the system information class to find, these COM interfaces are defined in #include
void GetWmiInfo (CString lpList, WideString wsClass) {IWbemLocator * pWbemLocator = NULL; if (CoCreateInstance (CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown, (void **) & pWbemLocator) == S_OK) {IWbemServices * pWbemServices = NULL; WideString wsNamespace = (L "root // cimv2"); if (pWbemLocator-> ConnectServer (wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, & pWbemServices) == S_OK) {IEnumWbemClassObject * pEnumClassObject = NULL; WideString wsWQL = L " WQL ", wsQuery = WideString (L" Select * from ") wsClass; if (pWbemServices-> ExecQuery (wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY, NULL, & pEnumClassObject) == S_OK) {IWbemClassObject * pClassObject = NULL; ULONG uCount = 1, URETURNED; if (penumclassobject-> reset () == s_ok) {INT IENUMIDX = 0; While (penumclassobject-> next (wbem_infinite, ucount, & pclassobject, & ureturned == s_ok) { LPLIST = "---------------- [" INTOSTR (IEnumIDX) "] ----------------");
SAFEARRAY * pvNames = NULL; if (pClassObject-> GetNames (NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, & pvNames) == S_OK) {long vbl, vbu; SafeArrayGetLBound (pvNames, 1, & vbl); SafeArrayGetUBound (pvNames, 1, & vbu) ; for (long idx = vbl; idx <= vbu; idx ) {long aidx = idx; wchar_t * wsName = 0; VARIANT vValue; VariantInit (& vValue); SafeArrayGetElement (pvNames, & aidx, & wsName); BSTR bs = SysAllocString (wsName ); HResult hres = pclassobject-> get (BS, 0, & VVALUE, NULL, 0); SYSFREESTRING (BS);
IF (hres == s_ok) {ANSISTRING S; Variant V = * (Variant *) & vvalue; if (v.isaray ()) {for (int i = v.ArraylowBound (); i <= v.ArrayHighBound (); i ) {variant a = v.Gtelement (i); if (! sisempty ()) s = ","; s = Vartostr (A);}} else {s = vartle (v);} lplist = CSTRING (WSNAME) "=" s;}
VariantClear (& vValue); SysFreeString (wsName);}} if (pvNames) SafeArrayDestroy (pvNames); iEnumIdx ;}} if (pClassObject) pClassObject-> Release ();} if (pEnumClassObject) pEnumClassObject-> Release ();} if (PWBemServices) PWBemServices-> release ();} if (pwbemlocator) PWBemlocator-> Release ();} // ------------------------- --------------------------------------------------
WMI type of information can be accessed are: Win32_1394Controller Win32_BaseBoard Win32_Battery Win32_BIOS Win32_Bus Win32_CacheMemory Win32_CDROMDrive Win32_CurrentProbe Win32_DesktopMonitor Win32_DeviceMemoryAddress Win32_DiskDrive Win32_DisplayConfiguration Win32_DisplayControllerConfiguration Win32_DMAChannel Win32_Fan Win32_FloppyController Win32_FloppyDrive Win32_HeatPipe Win32_IDEController Win32_InfraredDevice Win32_IRQResource Win32_Keyboard Win32_MemoryArray Win32_MemoryDevice Win32_MotherboardDevice Win32_NetworkAdapter Win32_NetworkAdapterConfiguration Win32_OnBoardDevice Win32_ParallelPort Win32_PCMCIAController Win32_PhysicalMemory Win32_PhysicalMemoryArray Win32_PnPEntity Win32_PointingDevice Win32_PortableBattery Win32_PortConnector Win32_PortResource Win32_POTSModem Win32_PowerManagementEvent Win32_Printer Win32_PrinterConfiguration Win32_PrintJob Win32_processor Win32_refrigeration win32_serialport win32_s erialPortConfiguration Win32_SMBIOSMemory Win32_SoundDevice Win32_SystemEnclosure Win32_SystemMemoryResource Win32_SystemSlot Win32_TapeDrive Win32_TemperatureProbe Win32_UninterruptiblePowerSupply Win32_USBController Win32_VideoConfiguration Win32_VideoController Win32_VoltageProbe above type (a string value) through the front GetWmiInfo write function can obtain the corresponding information, e.g. GetWmiInfo (strInfo, "WIN32_bios");