【C++】WMI获取系统硬件信息(CPU/DISK/NetWork etc)

官网找到一个例子,根据例子修改下可以获取很多信息

原文链接:http://blog.youkuaiyun.com/u010352603/article/details/51400769

[cpp]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. #define _WIN32_DCOM  
  2. #include <iostream>  
  3. using namespace std;  
  4. #include <comdef.h>  
  5. #include <Wbemidl.h>  
  6.   
  7. #pragma comment(lib, "wbemuuid.lib")  
  8.   
  9. int main(int argc, char **argv)  
  10. {  
  11.     HRESULT hres;  
  12.   
  13.     // Step 1: --------------------------------------------------  
  14.     // Initialize COM. ------------------------------------------  
  15.   
  16.     hres =  CoInitializeEx(0, COINIT_MULTITHREADED);   
  17.     if (FAILED(hres))  
  18.     {  
  19.         cout << "Failed to initialize COM library. Error code = 0x"   
  20.             << hex << hres << endl;  
  21.         return 1;                  // Program has failed.  
  22.     }  
  23.   
  24.     // Step 2: --------------------------------------------------  
  25.     // Set general COM security levels --------------------------  
  26.   
  27.     hres =  CoInitializeSecurity(  
  28.         NULL,   
  29.         -1,                          // COM authentication  
  30.         NULL,                        // Authentication services  
  31.         NULL,                        // Reserved  
  32.         RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication   
  33.         RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation    
  34.         NULL,                        // Authentication info  
  35.         EOAC_NONE,                   // Additional capabilities   
  36.         NULL                         // Reserved  
  37.         );  
  38.   
  39.                         
  40.     if (FAILED(hres))  
  41.     {  
  42.         cout << "Failed to initialize security. Error code = 0x"   
  43.             << hex << hres << endl;  
  44.         CoUninitialize();  
  45.         return 1;                    // Program has failed.  
  46.     }  
  47.       
  48.     // Step 3: ---------------------------------------------------  
  49.     // Obtain the initial locator to WMI -------------------------  
  50.   
  51.     IWbemLocator *pLoc = NULL;  
  52.   
  53.     hres = CoCreateInstance(  
  54.         CLSID_WbemLocator,               
  55.         0,   
  56.         CLSCTX_INPROC_SERVER,   
  57.         IID_IWbemLocator, (LPVOID *) &pLoc);  
  58.    
  59.     if (FAILED(hres))  
  60.     {  
  61.         cout << "Failed to create IWbemLocator object."  
  62.             << " Err code = 0x"  
  63.             << hex << hres << endl;  
  64.         CoUninitialize();  
  65.         return 1;                 // Program has failed.  
  66.     }  
  67.   
  68.     // Step 4: -----------------------------------------------------  
  69.     // Connect to WMI through the IWbemLocator::ConnectServer method  
  70.   
  71.     IWbemServices *pSvc = NULL;  
  72.    
  73.     // Connect to the root\cimv2 namespace with  
  74.     // the current user and obtain pointer pSvc  
  75.     // to make IWbemServices calls.  
  76.     hres = pLoc->ConnectServer(  
  77.          _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace  
  78.          NULL,                    // User name. NULL = current user  
  79.          NULL,                    // User password. NULL = current  
  80.          0,                       // Locale. NULL indicates current  
  81.          NULL,                    // Security flags.  
  82.          0,                       // Authority (for example, Kerberos)  
  83.          0,                       // Context object   
  84.          &pSvc                    // pointer to IWbemServices proxy  
  85.          );  
  86.       
  87.     if (FAILED(hres))  
  88.     {  
  89.         cout << "Could not connect. Error code = 0x"   
  90.              << hex << hres << endl;  
  91.         pLoc->Release();       
  92.         CoUninitialize();  
  93.         return 1;                // Program has failed.  
  94.     }  
  95.   
  96.     cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;  
  97.   
  98.   
  99.     // Step 5: --------------------------------------------------  
  100.     // Set security levels on the proxy -------------------------  
  101.   
  102.     hres = CoSetProxyBlanket(  
  103.        pSvc,                        // Indicates the proxy to set  
  104.        RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx  
  105.        RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx  
  106.        NULL,                        // Server principal name   
  107.        RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx   
  108.        RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx  
  109.        NULL,                        // client identity  
  110.        EOAC_NONE                    // proxy capabilities   
  111.     );  
  112.   
  113.     if (FAILED(hres))  
  114.     {  
  115.         cout << "Could not set proxy blanket. Error code = 0x"   
  116.             << hex << hres << endl;  
  117.         pSvc->Release();  
  118.         pLoc->Release();       
  119.         CoUninitialize();  
  120.         return 1;               // Program has failed.  
  121.     }  
  122.   
  123.     // Step 6: --------------------------------------------------  
  124.     // Use the IWbemServices pointer to make requests of WMI ----  
  125.   
  126.     // For example, get the name of the operating system  
  127.     IEnumWbemClassObject* pEnumerator = NULL;  
  128.     hres = pSvc->ExecQuery(  
  129.         bstr_t("WQL"),   
  130.         bstr_t("SELECT * FROM Win32_OperatingSystem"),  
  131.         WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,   
  132.         NULL,  
  133.         &pEnumerator);  
  134.       
  135.     if (FAILED(hres))  
  136.     {  
  137.         cout << "Query for operating system name failed."  
  138.             << " Error code = 0x"   
  139.             << hex << hres << endl;  
  140.         pSvc->Release();  
  141.         pLoc->Release();  
  142.         CoUninitialize();  
  143.         return 1;               // Program has failed.  
  144.     }  
  145.   
  146.     // Step 7: -------------------------------------------------  
  147.     // Get the data from the query in step 6 -------------------  
  148.    
  149.     IWbemClassObject *pclsObj = NULL;  
  150.     ULONG uReturn = 0;  
  151.      
  152.     while (pEnumerator)  
  153.     {  
  154.         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,   
  155.             &pclsObj, &uReturn);  
  156.   
  157.         if(0 == uReturn)  
  158.         {  
  159.             break;  
  160.         }  
  161.   
  162.         VARIANT vtProp;  
  163.   
  164.         // Get the value of the Name property  
  165.         hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);  
  166.         wcout << " OS Name : " << vtProp.bstrVal << endl;  
  167.         VariantClear(&vtProp);  
  168.   
  169.         pclsObj->Release();  
  170.     }  
  171.   
  172.     // Cleanup  
  173.     // ========  
  174.       
  175.     pSvc->Release();  
  176.     pLoc->Release();  
  177.     pEnumerator->Release();  
  178.     CoUninitialize();  
  179.   
  180.     return 0;   // Program successfully completed.  
  181.    
  182. }  

下面列出了常用信息的类:

Win32_Processor                        // CPU 处理器

Win32_PhysicalMemory                   // 物理内存

Win32_Keyboard                         // 键盘

Win32_PointingDevice                   // 点输入设备,如鼠标

Win32_DiskDrive                        // 硬盘驱动器

Win32_CDROMDrive                       // 光盘驱动器

Win32_BaseBoard                        // 主板

Win32_BIOS                             // BIOS 芯片

Win32_ParallelPort                     // 并口

Win32_SerialPort                       // 串口

Win32_SoundDevice                      // 多媒体设置

Win32_USBController                    // USB 控制器

Win32_NetworkAdapter                   // 网络适配器

Win32_NetworkAdapterConfiguration      // 网络适配器设置

Win32_Printer                          // 打印机

Win32_PrinterConfiguration             // 打印机设置

Win32_PrintJob                         // 打印机任务

Win32_TCPIPPrinterPort                 // 打印机端口

Win32_POTSModem                        // MODEM

Win32_POTSModemToSerialPort            // MODEM 端口

Win32_DesktopMonitor                   // 显示器

Win32_VideoController                  // 显卡细节。

Win32_VideoSettings                    // 显卡支持的显示模式。

Win32_TimeZone                         // 时区

Win32_SystemDriver                     // 驱动程序

Win32_DiskPartition                    // 磁盘分区

Win32_LogicalDisk                      // 逻辑磁盘

Win32_LogicalMemoryConfiguration       // 逻辑内存配置

Win32_PageFile                         // 系统页文件信息

Win32_PageFileSetting                  // 页文件设置

Win32_BootConfiguration                // 系统启动配置

Win32_OperatingSystem                  // 操作系统信息

Win32_StartupCommand                   // 系统自动启动程序

Win32_Service                          // 系统安装的服务

Win32_Group                            // 系统管理组

Win32_GroupUser                        // 系统组帐号

Win32_UserAccount                      // 用户帐号

Win32_Process                          // 系统进程

Win32_Thread                           // 系统线程

Win32_Share                            // 共享

Win32_NetworkClient                    // 已安装的网络客户端

Win32_NetworkProtocol                  // 已安装的网络协议

MSDN官网关于WMI 的demo程序

MSDN官网关于WMI定义的所有类信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值