WMI获取硬件信息

  1. #define _WIN32_DCOM  
  2. #define _WIN32_WINNT   0X0500   
  3.   
  4. #include <iostream>  
  5. #include <comdef.h>  
  6. #include <conio.h>  
  7. #include "wbemidl.h"  
  8. #include "wmi.h"  
  9.   
  10. #pragma comment(lib,"WbemUuid.lib")  
  11.   
  12. using namespace std;  
  13.   
  14. static IWbemLocator *pLoc = NULL;  
  15. static IWbemServices *pSvc = NULL;  
  16. static IEnumWbemClassObject* pEnumerator = NULL;   
  17. static IWbemClassObject* pclsObj = NULL;  
  18.   
  19. // http://www.pudn.com/downloads44/sourcecode/windows/system/detail148813.html  
  20. // http://www.qingfengju.com/article.asp?id=62  
  21. // http://blog.csdn.net/shiwei0124/article/details/4985790  
  22. // http://topic.csdn.net/u/20100623/15/ad7c592d-2548-4aae-8ca2-18065baf6566.html  
  23. // http://msdn.microsoft.com/en-us/library/aa389273(VS.85).aspx  
  24. int DisplayALL( )     
  25. {     
  26.     HRESULT hres;     
  27.          
  28.     cout << "Step 1: --------------------------------------------------" << endl;     
  29.     cout << "Initialize COM. ------------------------------------------" << endl;     
  30.          
  31.     hres =  CoInitializeEx(0, COINIT_MULTITHREADED);      
  32.     if (FAILED(hres))     
  33.     {     
  34.         cout << "Failed to initialize COM library. Error code = 0x"      
  35.             << hex << hres << endl;     
  36.         return 1;                  // Program has failed.      
  37.     }     
  38.          
  39.     cout << "Step 2: --------------------------------------------------" << endl;     
  40.     cout << "Set general COM security levels --------------------------" << endl;     
  41.     cout << "Note: If you are using Windows 2000, you need to specify -" << endl;     
  42.     cout << "the default authentication credentials for a user by using" << endl;     
  43.     cout << "a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----" << endl;     
  44.     cout << "parameter of CoInitializeSecurity ------------------------" << endl;     
  45.          
  46.     hres =  CoInitializeSecurity(     
  47.         NULL,      
  48.         -1,                          // COM authentication      
  49.         NULL,                        // Authentication services      
  50.         NULL,                        // Reserved      
  51.         RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication       
  52.         RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation        
  53.         NULL,                        // Authentication info      
  54.         EOAC_NONE,                   // Additional capabilities       
  55.         NULL                         // Reserved      
  56.         );     
  57.          
  58.          
  59.     if (FAILED(hres))     
  60.     {     
  61.         cout << "Failed to initialize security. Error code = 0x"      
  62.             << hex << hres << endl;     
  63.         CoUninitialize();     
  64.         return 1;                    // Program has failed.      
  65.     }     
  66.          
  67.     cout << "Step 3: ---------------------------------------------------" << endl;     
  68.     cout << "Obtain the initial locator to WMI -------------------------" << endl;     
  69.          
  70.     IWbemLocator *pLoc = NULL;     
  71.          
  72.     hres = CoCreateInstance(     
  73.         CLSID_WbemLocator,                  
  74.         0,      
  75.         CLSCTX_INPROC_SERVER,      
  76.         IID_IWbemLocator, (LPVOID *) &pLoc);     
  77.          
  78.     if (FAILED(hres))     
  79.     {     
  80.         cout << "Failed to create IWbemLocator object."     
  81.             << " Err code = 0x"     
  82.             << hex << hres << endl;     
  83.         CoUninitialize();     
  84.         return 1;                 // Program has failed.      
  85.     }     
  86.          
  87.     cout << "Step 4: -----------------------------------------------------" << endl;     
  88.     cout << "Connect to WMI through the IWbemLocator::ConnectServer method" << endl;     
  89.          
  90.     IWbemServices *pSvc = NULL;     
  91.          
  92.     cout << "Connect to the root\\cimv2 namespace with" << endl;     
  93.     cout << "the current user and obtain pointer pSvc" << endl;     
  94.     cout << "to make IWbemServices calls." << endl;     
  95.          
  96.     hres = pLoc->ConnectServer(     
  97.         _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace      
  98.         NULL,                    // User name. NULL = current user      
  99.         NULL,                    // User password. NULL = current      
  100.         0,                       // Locale. NULL indicates current      
  101.         NULL,                    // Security flags.      
  102.         0,                       // Authority (e.g. Kerberos)      
  103.         0,                       // Context object       
  104.         &pSvc                    // pointer to IWbemServices proxy      
  105.         );     
  106.          
  107.     if (FAILED(hres))     
  108.     {     
  109.         cout << "Could not connect. Error code = 0x"      
  110.             << hex << hres << endl;     
  111.         pLoc->Release();       
  112.         CoUninitialize();     
  113.         return 1;                // Program has failed.      
  114.     }     
  115.          
  116.     cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;     
  117.          
  118.          
  119.     cout << "Step 5: --------------------------------------------------" << endl;     
  120.     cout << "Set security levels on the proxy -------------------------" << endl;     
  121.          
  122.     hres = CoSetProxyBlanket(     
  123.         pSvc,                        // Indicates the proxy to set      
  124.         RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx      
  125.         RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx      
  126.         NULL,                        // Server principal name       
  127.         RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx       
  128.         RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx      
  129.         NULL,                        // client identity      
  130.         EOAC_NONE                    // proxy capabilities       
  131.         );     
  132.          
  133.     if (FAILED(hres))     
  134.     {     
  135.         cout << "Could not set proxy blanket. Error code = 0x"      
  136.             << hex << hres << endl;     
  137.         pSvc->Release();     
  138.         pLoc->Release();       
  139.         CoUninitialize();     
  140.         return 1;               // Program has failed.      
  141.     }     
  142.          
  143.     cout << "Step 6: --------------------------------------------------" << endl;     
  144.     cout << "Use the IWbemServices pointer to make requests of WMI ----" << endl;     
  145.          
  146.     // For example, get the name of the operating system      
  147.     IEnumWbemClassObject* pEnumerator = NULL;     
  148.       
  149.     hres = pSvc->ExecQuery(     
  150.             bstr_t("WQL"),   
  151. //          bstr_t("SELECT * FROM Win32_SoundDevice"),                          // 音频设备  
  152. //          bstr_t("SELECT * FROM Win32_Product"),    
  153. //          bstr_t("SELECT * FROM Win32_Registry"),                             // 注册表  
  154. //          bstr_t("SELECT * FROM Win32_OnBoardDevice"),                        // 主板上的嵌入设备  
  155. //          bstr_t("SELECT * FROM Win32_MotherboardDevice"),                    // 母板  
  156. //          bstr_t("SELECT * FROM Win32_BaseBoard"),                            // 主板  
  157. //          bstr_t("SELECT * FROM Win32_DMAChannel"),                           // DMA通道  
  158. //          bstr_t("SELECT * FROM Win32_Bus"),                                  // 总线  
  159. //          bstr_t("SELECT * FROM Win32_BIOS"),                                 // BIOS系统  
  160. //          bstr_t("SELECT * FROM Win32_SystemBIOS"),  
  161. //          bstr_t("SELECT * FROM Win32_Processor"),                            // 处理器  
  162. //          bstr_t("SELECT * FROM Win32_SystemProcesses"),                      //   
  163. //          bstr_t("SELECT * FROM Win32_Process"),                              // 系统进程  
  164. //          bstr_t("SELECT * FROM Win32_Thread"),                               // 系统线程  
  165. //          bstr_t("SELECT * FROM Win32_AssociatedProcessorMemory"),            // CACHE  
  166. //          bstr_t("SELECT * FROM Win32_CacheMemory"),                          // 二级缓存内存  
  167. //          bstr_t("SELECT * FROM Win32_PhysicalMedia"),                        // 物理媒体信息 (硬盘)  
  168. //          bstr_t("SELECT * FROM Win32_LogicalDisk "),                         // 逻辑驱动器  
  169. //          bstr_t("SELECT * FROM Win32_DiskDrive"),                            // 磁盘驱动器  
  170. //          bstr_t("SELECT * FROM Win32_MemoryDevice"),                         // 内存设备  
  171. //          bstr_t("SELECT * FROM Win32_PhysicalMemoryArray"),                  // 物理内存数组  
  172. //          bstr_t("SELECT * FROM Win32_PhysicalMemoryLocation"),               // 物理内存位置  
  173. //          bstr_t("SELECT * FROM CIM_NumericSensor"),                          // 数字传感器  
  174. //          bstr_t("SELECT * FROM Win32_VoltageProbe"),                         // 数字传感器   
  175. //          bstr_t("SELECT * FROM Win32_TemperatureProbe"),                     // 温度传感器  
  176. //          bstr_t("SELECT * FROM Win32_CurrentProbe"),       
  177. //          bstr_t("SELECT * FROM Win32_OperatingSystem"),                      // 操作系统      
  178. //          bstr_t("SELECT * FROM Win32_UserAccount"),                          // 用户账号  
  179. //          bstr_t("SELECT * FROM Win32_SerialPort"),                           // 串行接口  
  180. //          bstr_t("SELECT * FROM Win32_ParallelPort"),                         // 并行接口  
  181. //          bstr_t("SELECT * FROM Win32_SCSIController"),                       // 小型计算机系统接口  
  182. //          bstr_t("SELECT * FROM Win32_PortResource"),                         // I/O 端口  
  183. //          bstr_t("SELECT * FROM Win32_PNPDevice"),                            // 即插即用设备  
  184. //          bstr_t("SELECT * FROM Win32_NetworkAdapter"),                       // 网络适配器  
  185. //          bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration"),  
  186. //          bstr_t("SELECT * FROM Win32_NetworkAdapterSetting"),  
  187. //          bstr_t("SELECT * FROM Win32_AssociatedBattery"),  
  188. //          bstr_t("SELECT * FROM Win32_Battery"),                              // 内部电池  
  189. //          bstr_t("SELECT * FROM Win32_PortableBattery"),  
  190. //          bstr_t("SELECT * FROM Win32_PowerManagementEvent"),  
  191. //          bstr_t("SELECT * FROM Win32_UninterruptiblePowerSupply"),  
  192. //          bstr_t("SELECT * FROM Win32_DriverForDevice"),  
  193. //          bstr_t("SELECT * FROM Win32_Printer"),                              // 打印机  
  194. //          bstr_t("SELECT * FROM Win32_TCPIPPrinterPort"),  
  195. //          bstr_t("SELECT * FROM Win32_POTSModem"),  
  196. //          bstr_t("SELECT * FROM Win32_DesktopMonitor"),                       // 显示器  
  197.             bstr_t("SELECT * FROM Win32_VideoController"),                      // 显卡  
  198. //          bstr_t("SELECT * FROM Win32_CDROMDrive"),  
  199. //          bstr_t("SELECT * FROM Win32_Keyboard"),                             // 键盘  
  200. //          bstr_t("SELECT * FROM Win32_AutochkSetting"),  
  201. //          bstr_t("SELECT * FROM Win32_PointingDevice"),                       // 点击设备:鼠标、触摸板  
  202. //          bstr_t("SELECT * FROM Win32_Fan"),                                  // 风扇  
  203. //          bstr_t("SELECT * FROM Win32_WMISetting"),  
  204. //          bstr_t("SELECT * FROM Win32_TimeZone"),       
  205. //          bstr_t("SELECT * FROM Win32_Environment"),                          // 环境路径  
  206. //          bstr_t("SELECT * FROM Win32_QuotaSetting"),  
  207. //          bstr_t("SELECT * FROM Win32_NetworkProtocol"),                      // 己安装的网络协议  
  208.             WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,      
  209.             NULL,     
  210.             &pEnumerator);     
  211.          
  212.     if (FAILED(hres))     
  213.     {     
  214.         cout << "Query for operating system name failed."     
  215.             << " Error code = 0x"      
  216.             << hex << hres << endl;     
  217.         pSvc->Release();     
  218.         pLoc->Release();     
  219.         CoUninitialize();     
  220.         return 1;               // Program has failed.      
  221.     }     
  222.          
  223.     cout << "Step 7: -------------------------------------------------" << endl;     
  224.     cout << "Get the data from the query in step 6 -------------------" << endl;     
  225.          
  226.     IWbemClassObject* pclsObj = NULL;     
  227.     ULONG uReturn = 0;     
  228.      
  229.     while (pEnumerator)     
  230.     {     
  231.         cout << "pEnumerator==>" << pEnumerator << endl;     
  232.         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);     
  233.         cout << "pclsObj==>" << pclsObj << " NO. object found" << endl;     
  234.         if ((0 == uReturn) || (0 == pclsObj))     
  235.         {     
  236.             break;     
  237.         }     
  238.      
  239.         VARIANT vtProp;           
  240.              
  241.        // Get the value of the Name property      
  242.         VariantInit(&vtProp);   
  243.         vtProp.bstrVal = 0;// NULL  
  244.        // hr = pclsObj->Get(L"MACAddress", 0, &vtProp, 0, 0);     
  245.          hr = pclsObj->GetObjectText(0, &vtProp.bstrVal);  
  246.         if (vtProp.bstrVal != 0)     
  247.         {     
  248.             wcout.imbue(locale(locale(""),"",LC_CTYPE));  
  249.             wcout << L"硬件信息: " << vtProp.bstrVal << endl;     
  250.         }     
  251.         else     
  252.         {     
  253.             cout << "硬件信息 " << "vtProp.bstrVal == NULL" << endl;     
  254.         }  
  255.           VariantClear(&vtProp);     
  256.     }     
  257.          
  258.     cout << "Cleanup" << endl;     
  259.     // ========      
  260.          
  261.     pSvc->Release();     
  262.     pLoc->Release();     
  263.     if (pEnumerator)     
  264.     {     
  265.         pEnumerator->Release();     
  266.     }     
  267.     if (pclsObj)     
  268.     {     
  269.         pclsObj->Release();     
  270.     }     
  271.     CoUninitialize();     
  272.          
  273.     cout << "end of program" << endl;     
  274.          
  275.     return 0;   // Program successfully completed.      
  276.          
  277. }     
  278.   
  279.   
  280.   
  281.   
  282. int main(int argc, char **argv)    
  283. {  
  284. //  DisplayALL(); return 0;  
  285.   
  286.     int p = Step1()?1:Step2()?1:Step3()?1:Step4()?1:Step5();  
  287.     // 下面可能错误,编译器优化,先算其中某一步为0,不再算其它  
  288. //  int p = Step1() & Step2() & Step3() & Step4() & Step5();   
  289.     if (p) goto ExitProgram;  
  290.   
  291.     // 网卡  
  292.     p = Step6("SELECT * FROM Win32_NetworkAdapter");  
  293.     if (p) goto ExitProgram;  
  294.     {  
  295.         unsigned short MACAddress[MAX_PATH] = {0};  
  296.         p = Step7(L"NetConnectionID", L"本地连接", L"MACAddress", MACAddress);  
  297.         if (MACAddress[0] != 0)     
  298.         {     
  299.             wcout.imbue(locale(locale(""),"",LC_CTYPE));  
  300.             wcout << L"网卡地址: " << MACAddress << endl;     
  301.         }    
  302.     }  
  303.   
  304.     // CPU  
  305.     p = Step6("SELECT * FROM Win32_Processor");  
  306.     if (p) goto ExitProgram;  
  307.     {  
  308.         unsigned short ProcessorId[MAX_PATH] = {0};  
  309.         p = Step7(L"DeviceID", L"CPU0", L"ProcessorId", ProcessorId);  
  310.         if (ProcessorId[0] != 0)     
  311.         {     
  312.             wcout.imbue(locale(locale(""),"",LC_CTYPE));  
  313.             wcout << L"处理器ID: " << ProcessorId << endl;     
  314.         }    
  315.     }  
  316.   
  317.     // 硬盘  
  318.     p = Step6("SELECT * FROM Win32_PhysicalMedia");  
  319.     if (p) goto ExitProgram;  
  320.     {  
  321.         unsigned short SerialNumber[MAX_PATH] = {0};  
  322.         p = Step7(L"SerialNumber", SerialNumber);  
  323.         if (SerialNumber[0] != 0)     
  324.         {     
  325.             wcout.imbue(locale(locale(""),"",LC_CTYPE));  
  326.             wcout << L"硬盘序列号: " << SerialNumber << endl;     
  327.         }   
  328.     }  
  329.   
  330.     // 显卡  
  331.     p = Step6("SELECT * FROM Win32_VideoController");  
  332.     if (p) goto ExitProgram;  
  333.     {  
  334.         unsigned short PNPDeviceID[MAX_PATH] = {0};  
  335.         p = Step7(L"PNPDeviceID", PNPDeviceID);  
  336.         if (PNPDeviceID[0] != 0)     
  337.         {     
  338.             wcout.imbue(locale(locale(""),"",LC_CTYPE));  
  339.             wcout << L"显卡序列号: " << PNPDeviceID << endl;     
  340.         }   
  341.     }  
  342.   
  343.     // 操作系统  
  344.     p = Step6("SELECT * FROM Win32_OperatingSystem");  
  345.     if (p) goto ExitProgram;  
  346.     {  
  347.         unsigned short SerialNumber[MAX_PATH] = {0};  
  348.         p = Step7(L"SerialNumber", SerialNumber);  
  349.         if (SerialNumber[0] != 0)     
  350.         {     
  351.             wcout.imbue(locale(locale(""),"",LC_CTYPE));  
  352.             wcout << L"操作系统ID: " << SerialNumber << endl;     
  353.         }   
  354.     }  
  355.   
  356.      pSvc->Release();  
  357.     pLoc->Release();       
  358.   
  359. ExitProgram:  
  360.     Step8();  
  361.     getch();  
  362.     return 0;  
  363. }  
  364.   
  365.   
  366.   
  367.   
  368.   
  369.   
  370.   
  371.   
  372.   
  373.   
  374. int Step1()  
  375. {   
  376.     HRESULT hres;     
  377.          
  378.  //   cout << "Step 1: --------------------------------------------------" << endl;     
  379.  //   cout << "Initialize COM. ------------------------------------------" << endl;     
  380.          
  381.     hres =  CoInitializeEx(0, COINIT_MULTITHREADED);      
  382.     if (FAILED(hres))     
  383.     {     
  384.         cout << "Failed to initialize COM library. Error code = 0x"      
  385.             << hex << hres << endl;     
  386.         return 1;                  // Program has failed.      
  387.     }     
  388.     return 0;  
  389. }  
  390. int Step2()  
  391. {   
  392. //  cout << "Step 2: --------------------------------------------------" << endl;     
  393. //    cout << "Set general COM security levels --------------------------" << endl;     
  394. //    cout << "Note: If you are using Windows 2000, you need to specify -" << endl;     
  395. //    cout << "the default authentication credentials for a user by using" << endl;     
  396. //    cout << "a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----" << endl;     
  397.  //   cout << "parameter of CoInitializeSecurity ------------------------" << endl;     
  398.     HRESULT hres;    
  399.   
  400.     hres =  CoInitializeSecurity(     
  401.         NULL,      
  402.         -1,                          // COM authentication      
  403.         NULL,                        // Authentication services      
  404.         NULL,                        // Reserved      
  405.         RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication       
  406.         RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation        
  407.         NULL,                        // Authentication info      
  408.         EOAC_NONE,                   // Additional capabilities       
  409.         NULL                         // Reserved      
  410.         );     
  411.          
  412.          
  413.     if (FAILED(hres))     
  414.     {     
  415.         cout << "Failed to initialize security. Error code = 0x"      
  416.             << hex << hres << endl;     
  417.         CoUninitialize();     
  418.         return 1;                    // Program has failed.      
  419.     }     
  420.     return 0;  
  421. }  
  422. int Step3()  
  423. {   
  424. //  cout << "Step 3: ---------------------------------------------------" << endl;     
  425.  //   cout << "Obtain the initial locator to WMI -------------------------" << endl;     
  426.          
  427.  //   IWbemLocator *pLoc = NULL;     
  428.     HRESULT hres;    
  429.   
  430.     hres = CoCreateInstance(     
  431.         CLSID_WbemLocator,                  
  432.         0,      
  433.         CLSCTX_INPROC_SERVER,      
  434.         IID_IWbemLocator, (LPVOID *) &pLoc);     
  435.          
  436.     if (FAILED(hres))     
  437.     {     
  438.         cout << "Failed to create IWbemLocator object."     
  439.             << " Err code = 0x"     
  440.             << hex << hres << endl;     
  441.         CoUninitialize();     
  442.         return 1;                 // Program has failed.      
  443.     }     
  444.     return 0;  
  445. }  
  446. int Step4()  
  447. {   
  448. //  cout << "Step 4: -----------------------------------------------------" << endl;     
  449.   //  cout << "Connect to WMI through the IWbemLocator::ConnectServer method" << endl;     
  450.          
  451.  //   IWbemServices *pSvc = NULL;     
  452.          
  453.  //   cout << "Connect to the root\\cimv2 namespace with" << endl;     
  454.  //   cout << "the current user and obtain pointer pSvc" << endl;     
  455.   //  cout << "to make IWbemServices calls." << endl;     
  456.     HRESULT hres;    
  457.       
  458.     hres = pLoc->ConnectServer(     
  459.         _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace      
  460.         NULL,                    // User name. NULL = current user      
  461.         NULL,                    // User password. NULL = current      
  462.         0,                       // Locale. NULL indicates current      
  463.         NULL,                    // Security flags.      
  464.         0,                       // Authority (e.g. Kerberos)      
  465.         0,                       // Context object       
  466.         &pSvc                    // pointer to IWbemServices proxy      
  467.         );     
  468.          
  469.     if (FAILED(hres))     
  470.     {     
  471.         cout << "Could not connect. Error code = 0x"      
  472.             << hex << hres << endl;     
  473.         pLoc->Release();       
  474.         CoUninitialize();     
  475.         return 1;                // Program has failed.      
  476.     }     
  477.          
  478.  //   cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;     
  479.     return 0;  
  480. }  
  481. int Step5()  
  482. {   
  483. //  cout << "Step 5: --------------------------------------------------" << endl;     
  484. //  cout << "Set security levels on the proxy -------------------------" << endl;     
  485.     HRESULT hres;    
  486.       
  487.     hres = CoSetProxyBlanket(     
  488.         pSvc,                        // Indicates the proxy to set      
  489.         RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx      
  490.         RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx      
  491.         NULL,                        // Server principal name       
  492.         RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx       
  493.         RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx      
  494.         NULL,                        // client identity      
  495.         EOAC_NONE                    // proxy capabilities       
  496.         );     
  497.          
  498.     if (FAILED(hres))     
  499.     {     
  500.         cout << "Could not set proxy blanket. Error code = 0x"      
  501.             << hex << hres << endl;     
  502.         pSvc->Release();     
  503.         pLoc->Release();       
  504.         CoUninitialize();     
  505.         return 1;               // Program has failed.      
  506.     }    
  507.     return 0;  
  508. }  
  509. int Step6(char *CmdString)  
  510. {   
  511. //  cout << "Step 6: --------------------------------------------------" << endl;     
  512. //  cout << "Use the IWbemServices pointer to make requests of WMI ----" << endl;     
  513.          
  514.     // For example, get the name of the operating system      
  515. //  IEnumWbemClassObject* pEnumerator = NULL;     
  516.     HRESULT hres;    
  517.   
  518.     hres = pSvc->ExecQuery(     
  519.             bstr_t("WQL"),  
  520.             bstr_t(CmdString),  
  521.             WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,      
  522.             NULL,     
  523.             &pEnumerator);     
  524.          
  525.     if (FAILED(hres))     
  526.     {     
  527.         cout << "Query for operating system name failed."     
  528.             << " Error code = 0x"      
  529.             << hex << hres << endl;     
  530.         pSvc->Release();     
  531.         pLoc->Release();     
  532.         CoUninitialize();     
  533.         return 1;               // Program has failed.      
  534.     }     
  535.     return 0;  
  536. }  
  537. int Step7(unsigned short *Item1, unsigned short *content1,   
  538.           unsigned short *Item2, unsigned short *content2)  
  539. {   
  540. //  cout << "Step 7: -------------------------------------------------" << endl;     
  541.  //   cout << "Get the data from the query in step 6 -------------------" << endl;     
  542.          
  543. //  IWbemClassObject* pclsObj = NULL;     
  544.     ULONG uReturn = 0;     
  545.      
  546.     while (pEnumerator)     
  547.     {     
  548. //        cout << "pEnumerator==>" << pEnumerator << endl;     
  549.         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);     
  550. //        cout << "pclsObj==>" << pclsObj << " NO. object found" << endl;     
  551.         if ((0 == uReturn) || (0 == pclsObj))     
  552.         {     
  553.             break;     
  554.         }     
  555.      
  556.         VARIANT vtProp;           
  557.              
  558.         // Get the value of the Name property      
  559.         // 名称  
  560.         VariantInit(&vtProp);   
  561.         vtProp.bstrVal = 0;// NULL  
  562.         hr = pclsObj->Get(Item1, 0, &vtProp, 0, 0);  
  563.   
  564.         if(hr != WBEM_S_NO_ERROR || vtProp.bstrVal == 0 || wcscmp(vtProp.bstrVal, content1) != 0)   
  565.         {     
  566.             VariantClear(&vtProp);  
  567.             continue;  
  568.         }  
  569.         VariantClear(&vtProp);  
  570.   
  571.         VariantInit(&vtProp);   
  572.         vtProp.bstrVal = 0;// NULL  
  573.         hr = pclsObj->Get(Item2, 0, &vtProp, 0, 0);     
  574.          // hr = pclsObj->GetObjectText(0, &vtProp.bstrVal);  
  575.         if (vtProp.bstrVal != 0)     
  576.         {     
  577. //          wcout.imbue(locale(locale(""),"",LC_CTYPE));  
  578. //          wcout << L"硬件信息: " << vtProp.bstrVal << endl;     
  579.             wcscpy(content2, vtProp.bstrVal);  
  580.         }     
  581.         else     
  582.         {     
  583.             cout << "硬件信息 " << "vtProp.bstrVal == NULL" << endl;     
  584.         }  
  585.           VariantClear(&vtProp);     
  586.     }        
  587.     return 0;  
  588. }  
  589. int Step7(unsigned short *Item, unsigned short *content)  
  590. {   
  591. //  cout << "Step 7: -------------------------------------------------" << endl;     
  592.  //   cout << "Get the data from the query in step 6 -------------------" << endl;     
  593.          
  594. //  IWbemClassObject* pclsObj = NULL;     
  595.     ULONG uReturn = 0;     
  596.      
  597.     while (pEnumerator)     
  598.     {     
  599. //      cout << "pEnumerator==>" << pEnumerator << endl;     
  600.         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);     
  601. //        cout << "pclsObj==>" << pclsObj << " NO. object found" << endl;     
  602.         if ((0 == uReturn) || (0 == pclsObj))     
  603.         {     
  604.             break;     
  605.         }     
  606.      
  607.         VARIANT vtProp;           
  608.              
  609.         // Get the value of the Name property      
  610.           // 名称  
  611.           VariantInit(&vtProp);   
  612.           vtProp.bstrVal = 0;// NULL  
  613.         hr = pclsObj->Get(Item, 0, &vtProp, 0, 0);     
  614.          // hr = pclsObj->GetObjectText(0, &vtProp.bstrVal);  
  615.         if (vtProp.bstrVal != 0)     
  616.         {     
  617. //          wcout.imbue(locale(locale(""),"",LC_CTYPE));  
  618. //          wcout << L"硬件信息: " << vtProp.bstrVal << endl;     
  619.             wcscpy(content, vtProp.bstrVal);  
  620.         }     
  621.         else     
  622.         {     
  623.             cout << "硬件信息 " << "vtProp.bstrVal == NULL" << endl;     
  624.         }  
  625.           VariantClear(&vtProp);     
  626.     }        
  627.     return 0;  
  628. }  
  629. int Step8()  
  630. {  
  631. //  cout << "Cleanup" << endl;     
  632.     // ========      
  633.     if (pEnumerator)     
  634.     {     
  635.         pEnumerator->Release();     
  636.     }     
  637.     if (pclsObj)     
  638.     {     
  639.         pclsObj->Release();     
  640.     }  
  641.      CoUninitialize();   
  642.          
  643. //  cout << "end of program" << endl;  
  644.     return 0;  
  645. }  

你可能感兴趣的:(VC,VC,wmi)