小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

【C++】WMI獲取系統(tǒng)硬件信息(CPU/DISK/NetWork etc)

 zengbj 2018-04-11
                 原創(chuàng) 2016年05月14日 01:50:22

官網(wǎng)找到一個(gè)例子,根據(jù)例子修改下可以獲取很多信息

  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                   // 物理內(nèi)存

Win32_Keyboard                         // 鍵盤(pán)

Win32_PointingDevice                   // 點(diǎn)輸入設(shè)備,如鼠標(biāo)

Win32_DiskDrive                        // 硬盤(pán)驅(qū)動(dòng)器

Win32_CDROMDrive                       // 光盤(pán)驅(qū)動(dòng)器

Win32_BaseBoard                        // 主板

Win32_BIOS                             // BIOS 芯片

Win32_ParallelPort                     // 并口

Win32_SerialPort                       // 串口

Win32_SoundDevice                      // 多媒體設(shè)置

Win32_USBController                    // USB 控制器

Win32_NetworkAdapter                   // 網(wǎng)絡(luò)適配器

Win32_NetworkAdapterConfiguration      // 網(wǎng)絡(luò)適配器設(shè)置

Win32_Printer                          // 打印機(jī)

Win32_PrinterConfiguration             // 打印機(jī)設(shè)置

Win32_PrintJob                         // 打印機(jī)任務(wù)

Win32_TCPIPPrinterPort                 // 打印機(jī)端口

Win32_POTSModem                        // MODEM

Win32_POTSModemToSerialPort            // MODEM 端口

Win32_DesktopMonitor                   // 顯示器

Win32_VideoController                  // 顯卡細(xì)節(jié)。

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

Win32_TimeZone                         // 時(shí)區(qū)

Win32_SystemDriver                     // 驅(qū)動(dòng)程序

Win32_DiskPartition                    // 磁盤(pán)分區(qū)

Win32_LogicalDisk                      // 邏輯磁盤(pán)

Win32_LogicalMemoryConfiguration       // 邏輯內(nèi)存配置

Win32_PageFile                         // 系統(tǒng)頁(yè)文件信息

Win32_PageFileSetting                  // 頁(yè)文件設(shè)置

Win32_BootConfiguration                // 系統(tǒng)啟動(dòng)配置

Win32_OperatingSystem                  // 操作系統(tǒng)信息

Win32_StartupCommand                   // 系統(tǒng)自動(dòng)啟動(dòng)程序

Win32_Service                          // 系統(tǒng)安裝的服務(wù)

Win32_Group                            // 系統(tǒng)管理組

Win32_GroupUser                        // 系統(tǒng)組賬號(hào)

Win32_UserAccount                      // 用戶賬號(hào)

Win32_Process                          // 系統(tǒng)進(jìn)程

Win32_Thread                           // 系統(tǒng)線程

Win32_Share                            // 共享

Win32_NetworkClient                    // 已安裝的網(wǎng)絡(luò)客戶端

Win32_NetworkProtocol                  // 已安裝的網(wǎng)絡(luò)協(xié)議

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多