使用WMI收集客户端信息

WQL就是WMI中的查询语言,WQL的全称是WMI Query Language,简称为WQL,翻译成中文好像可以成为Windows管理规范查询语言。熟悉SQL语言的朋友会感觉它和SQL非常相似。

具体操作过程参考:http://blog.csdn.net/zhoufoxcn/archive/2008/01/14/2044246.aspx

WQL其实非常简单,它有如下特点:
1、每个WQL语句必须以SELECT开始;
2、SELECT后跟你需要查询的属性名(我刚才对应SQL将其称之为字段名了),也可以像SQL一样,以*表示返回所有属性值;
3、FROM关键字;
4、你要查询的类的名字;
5、另外,如果你想精确查询结果还可以加上WHERE条件从句。比如某个类有Enable属性,你可以在查询的时候加上WHERE ENABLE=true。

要测试WQL语句,可在winxp系统中运行“wbemtest.exe”程序,在弹出的“windows管理规范测试器”中点击“连接”,将“root/default”改为“root/cimv2”,然后点“连接”,回到windows管理规范测试器中,点击“枚举类别”,点“递归”,然后“超类别名称”处保持空白,点击确定,就能得到所有可以通过WQL语句查询的超类别名词,可以把它们看成是SQL中的“表”。任意选择其中一项,比如Win32_BaseBoard,回到windows管理规范测试器,点击“查询”,输入“Select * From Win32_BaseBoard”,双击查询结果,在“对象编辑器”的“属性”一栏中即是该超类别的所有属性,也就是SQL中表的字段。属性栏中第一项是字段名称,第二项是字段的类型,比如“CIM_STRING”对应c++中的bstrVal类型,“CIM_UINT32”对应c++中的intVal类型。下面是用程序实现的对主板,显卡,CPU,内存,操作系统信息的查询:
  1. #define _WIN32_DCOM 
  2. #include "stdafx.h" 
  3. #include <iostream> 
  4. #include <comdef.h> 
  5. #include <Wbemidl.h> 
  6. #include <Wbemcli.h>
  7. #include <conio.h> 
  8. #pragma comment(lib, "wbemuuid.lib") 
  9. using namespace std; 
  10. BOOL ManageWMI(); 
  11. int _tmain(int argc, _TCHAR* argv[]) 
  12.     if(ManageWMI()) printf("%WMI Error!"); 
  13.     _getch(); 
  14.     return 0; 
  15. }
  16. int CollectBaseBoardInfo(IWbemServices *pSvc)
  17. {
  18.     HRESULT hres;
  19.     // Step 6: -------------------------------------------------- 
  20.     // Use the IWbemServices pointer to make requests of WMI ---- 
  21.     // For example, get the name of the operating system 
  22.     IEnumWbemClassObject* pEnumerator = NULL; 
  23.     hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_BaseBoard"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); 
  24.     if (FAILED(hres)) 
  25.     { 
  26.         cout << "Query for BaseBoard Configuration failed." 
  27.             << " Error code = 0x" 
  28.             << hex << hres << endl; 
  29.         pSvc->Release(); 
  30.         return 1; // Program has failed. 
  31.     } 
  32.     // Step 7: ------------------------------------------------- 
  33.     // Get the data from the query in step 6 ------------------- 
  34.     IWbemClassObject *pclsObj; 
  35.     ULONG uReturn = 0; 
  36.     while (pEnumerator) 
  37.     { 
  38.         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 
  39.         if(0 == uReturn) 
  40.         { 
  41.             break
  42.         } 
  43.         VARIANT vtProp; 
  44.         VariantInit(&vtProp); 
  45.         //制造商
  46.         hr = pclsObj->Get(L"Manufacturer", 0, &vtProp, 0, 0); 
  47.         wcout << vtProp.bstrVal << endl;
  48.         //主板型号
  49.         hr = pclsObj->Get(L"Product", 0, &vtProp, 0, 0); 
  50.         wcout << vtProp.bstrVal << endl;
  51.         //序列号
  52.         hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0); 
  53.         wcout << vtProp.bstrVal << endl;
  54.         VariantClear(&vtProp); 
  55.     } 
  56.     pEnumerator->Release(); 
  57.     pclsObj->Release(); 
  58.     return 0;
  59. }
  60. int CollectVideoControllerInfo(IWbemServices *pSvc)
  61. {
  62.     HRESULT hres;
  63.     // Step 6: -------------------------------------------------- 
  64.     // Use the IWbemServices pointer to make requests of WMI ---- 
  65.     // For example, get the name of the operating system 
  66.     IEnumWbemClassObject* pEnumerator = NULL; 
  67.     hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_VideoController"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); 
  68.     if (FAILED(hres)) 
  69.     { 
  70.         cout << "Query for VideoController Configuration failed." 
  71.             << " Error code = 0x" 
  72.             << hex << hres << endl; 
  73.         pSvc->Release(); 
  74.         return 1; // Program has failed. 
  75.     } 
  76.     // Step 7: ------------------------------------------------- 
  77.     // Get the data from the query in step 6 ------------------- 
  78.     IWbemClassObject *pclsObj; 
  79.     ULONG uReturn = 0; 
  80.     while (pEnumerator) 
  81.     { 
  82.         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 
  83.         if(0 == uReturn) 
  84.         { 
  85.             break
  86.         } 
  87.         VARIANT vtProp; 
  88.         VariantInit(&vtProp); 
  89.         //型号
  90.         hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0); 
  91.         wcout << vtProp.bstrVal << endl;
  92.         //显存大小
  93.         hr = pclsObj->Get(L"AdapterRAM", 0, &vtProp, 0, 0); 
  94.         wcout << vtProp.intVal << endl;
  95.         VariantClear(&vtProp); 
  96.     } 
  97.     pEnumerator->Release(); 
  98.     pclsObj->Release(); 
  99.     return 0;
  100. }
  101. int CollectProcessorInfo(IWbemServices *pSvc)
  102. {
  103.     HRESULT hres;
  104.     // Step 6: -------------------------------------------------- 
  105.     // Use the IWbemServices pointer to make requests of WMI ---- 
  106.     // For example, get the name of the operating system 
  107.     IEnumWbemClassObject* pEnumerator = NULL; 
  108.     hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_Processor"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); 
  109.     if (FAILED(hres)) 
  110.     { 
  111.         cout << "Query for Processor Configuration failed." 
  112.             << " Error code = 0x" 
  113.             << hex << hres << endl; 
  114.         pSvc->Release(); 
  115.         return 1; // Program has failed. 
  116.     } 
  117.     // Step 7: ------------------------------------------------- 
  118.     // Get the data from the query in step 6 ------------------- 
  119.     IWbemClassObject *pclsObj; 
  120.     ULONG uReturn = 0; 
  121.     while (pEnumerator) 
  122.     { 
  123.         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 
  124.         if(0 == uReturn) 
  125.         { 
  126.             break
  127.         } 
  128.         VARIANT vtProp; 
  129.         VariantInit(&vtProp); 
  130.         //描述
  131.         hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0); 
  132.         wcout << vtProp.bstrVal << endl;
  133.         //时钟频率
  134.         hr = pclsObj->Get(L"ExtClock", 0, &vtProp, 0, 0); 
  135.         wcout << vtProp.intVal << endl;
  136.         //cpu型号
  137.         hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0); 
  138.         wcout << vtProp.bstrVal << endl;
  139.         VariantClear(&vtProp); 
  140.     } 
  141.     pEnumerator->Release(); 
  142.     pclsObj->Release(); 
  143.     return 0;
  144. }
  145. int CollectLogicalMemoryConfigurationInfo(IWbemServices *pSvc)
  146. {
  147.     HRESULT hres;
  148.     // Step 6: -------------------------------------------------- 
  149.     // Use the IWbemServices pointer to make requests of WMI ---- 
  150.     // For example, get the name of the operating system 
  151.     IEnumWbemClassObject* pEnumerator = NULL; 
  152.     hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_LogicalMemoryConfiguration"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); 
  153.     if (FAILED(hres)) 
  154.     { 
  155.         cout << "Query for Processor Configuration failed." 
  156.             << " Error code = 0x" 
  157.             << hex << hres << endl; 
  158.         pSvc->Release(); 
  159.         return 1; // Program has failed. 
  160.     } 
  161.     // Step 7: ------------------------------------------------- 
  162.     // Get the data from the query in step 6 ------------------- 
  163.     IWbemClassObject *pclsObj; 
  164.     ULONG uReturn = 0; 
  165.     while (pEnumerator) 
  166.     { 
  167.         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 
  168.         if(0 == uReturn) 
  169.         { 
  170.             break
  171.         } 
  172.         VARIANT vtProp; 
  173.         VariantInit(&vtProp); 
  174.         //物理内存
  175.         hr = pclsObj->Get(L"TotalPhysicalMemory", 0, &vtProp, 0, 0); 
  176.         wcout << vtProp.intVal << endl;
  177.         //虚拟内存
  178.         hr = pclsObj->Get(L"TotalVirtualMemory", 0, &vtProp, 0, 0); 
  179.         wcout << vtProp.intVal << endl;
  180.         //页面文件
  181.         hr = pclsObj->Get(L"TotalPageFileSpace", 0, &vtProp, 0, 0); 
  182.         wcout << vtProp.intVal << endl;
  183.         VariantClear(&vtProp); 
  184.     } 
  185.     pEnumerator->Release(); 
  186.     pclsObj->Release(); 
  187.     return 0;
  188. }
  189. int CollectOperatingSystemInfo(IWbemServices *pSvc)
  190. {
  191.     HRESULT hres;
  192.     // Step 6: -------------------------------------------------- 
  193.     // Use the IWbemServices pointer to make requests of WMI ---- 
  194.     // For example, get the name of the operating system 
  195.     IEnumWbemClassObject* pEnumerator = NULL; 
  196.     hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_OperatingSystem"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); 
  197.     if (FAILED(hres)) 
  198.     { 
  199.         cout << "Query for Processor Configuration failed." 
  200.             << " Error code = 0x" 
  201.             << hex << hres << endl; 
  202.         pSvc->Release(); 
  203.         return 1; // Program has failed. 
  204.     } 
  205.     // Step 7: ------------------------------------------------- 
  206.     // Get the data from the query in step 6 ------------------- 
  207.     IWbemClassObject *pclsObj; 
  208.     ULONG uReturn = 0; 
  209.     while (pEnumerator) 
  210.     { 
  211.         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 
  212.         if(0 == uReturn) 
  213.         { 
  214.             break
  215.         } 
  216.         VARIANT vtProp; 
  217.         VariantInit(&vtProp); 
  218.         //操作系统名
  219.         hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0); 
  220.         wcout << vtProp.bstrVal << endl;
  221.         //系统启动分区
  222.         hr = pclsObj->Get(L"BootDevice", 0, &vtProp, 0, 0); 
  223.         wcout << vtProp.bstrVal << endl;
  224.         //操作系统序列号
  225.         hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0); 
  226.         wcout << vtProp.intVal << endl;
  227.         //操作系统语言
  228.         hr = pclsObj->Get(L"OSLanguage", 0, &vtProp, 0, 0); 
  229.         wcout << vtProp.intVal << endl;
  230.         //制造商名
  231.         hr = pclsObj->Get(L"Manufacturer", 0, &vtProp, 0, 0); 
  232.         wcout << vtProp.bstrVal << endl;
  233.         VariantClear(&vtProp); 
  234.     } 
  235.     pEnumerator->Release(); 
  236.     pclsObj->Release(); 
  237.     return 0;
  238. }
  239. BOOL ManageWMI() 
  240.     HRESULT hres; 
  241.     // Step 1: -------------------------------------------------- 
  242.     // Initialize COM. ------------------------------------------ 
  243.     hres = CoInitializeEx(0, COINIT_MULTITHREADED); 
  244.     if (FAILED(hres)) 
  245.     { 
  246.         cout << "Failed to initialize COM library. Error code = 0x" 
  247.             << hex << hres << endl; 
  248.         return 1; // Program has failed. 
  249.     } 
  250.     // Step 2: -------------------------------------------------- 
  251.     // Set general COM security levels -------------------------- 
  252.     // Note: If you are using Windows 2000, you need to specify - 
  253.     // the default authentication credentials for a user by using 
  254.     // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ---- 
  255.     // parameter of CoInitializeSecurity ------------------------ 
  256.     hres = CoInitializeSecurity( 
  257.         NULL, 
  258.         -1, // COM authentication 
  259.         NULL, // Authentication services 
  260.         NULL, // Reserved 
  261.         RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication 
  262.         RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation 
  263.         NULL, // Authentication info 
  264.         EOAC_NONE, // Additional capabilities 
  265.         NULL // Reserved 
  266.         ); 
  267.     if (FAILED(hres)) 
  268.     { 
  269.         cout << "Failed to initialize security. Error code = 0x" 
  270.             << hex << hres << endl; 
  271.         CoUninitialize(); 
  272.         return 1; // Program has failed. 
  273.     } 
  274.     // Step 3: --------------------------------------------------- 
  275.     // Obtain the initial locator to WMI ------------------------- 
  276.     IWbemLocator *pLoc = NULL; 
  277.     hres = CoCreateInstance( 
  278.         CLSID_WbemLocator, 
  279.         0, 
  280.         CLSCTX_INPROC_SERVER, 
  281.         IID_IWbemLocator, (LPVOID *) &pLoc); 
  282.     if (FAILED(hres)) 
  283.     { 
  284.         cout << "Failed to create IWbemLocator object." 
  285.             << " Err code = 0x" 
  286.             << hex << hres << endl; 
  287.         CoUninitialize(); 
  288.         return 1; // Program has failed. 
  289.     } 
  290.     // Step 4: ----------------------------------------------------- 
  291.     // Connect to WMI through the IWbemLocator::ConnectServer method 
  292.     IWbemServices *pSvc = NULL; 
  293.     // Connect to the root/cimv2 namespace with 
  294.     // the current user and obtain pointer pSvc 
  295.     // to make IWbemServices calls. 
  296.     hres = pLoc->ConnectServer( 
  297.         _bstr_t(L"ROOT//CIMV2"), // Object path of WMI namespace 
  298.         NULL, // User name. NULL = current user 
  299.         NULL, // User password. NULL = current 
  300.         0, // Locale. NULL indicates current 
  301.         NULL, // Security flags. 
  302.         0, // Authority (e.g. Kerberos) 
  303.         0, // Context object 
  304.         &pSvc // pointer to IWbemServices proxy 
  305.         ); 
  306.     if (FAILED(hres)) 
  307.     { 
  308.         cout << "Could not connect. Error code = 0x" 
  309.             << hex << hres << endl; 
  310.         pLoc->Release(); 
  311.         CoUninitialize(); 
  312.         return 1; // Program has failed. 
  313.     } 
  314.     cout << "Connected to ROOT//CIMV2 WMI namespace" << endl; 
  315.     // Step 5: -------------------------------------------------- 
  316.     // Set security levels on the proxy ------------------------- 
  317.     hres = CoSetProxyBlanket( 
  318.         pSvc, // Indicates the proxy to set 
  319.         RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx 
  320.         RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx 
  321.         NULL, // Server principal name 
  322.         RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx 
  323.         RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx 
  324.         NULL, // client identity 
  325.         EOAC_NONE // proxy capabilities 
  326.         ); 
  327.     if (FAILED(hres)) 
  328.     { 
  329.         cout << "Could not set proxy blanket. Error code = 0x" 
  330.             << hex << hres << endl; 
  331.         pSvc->Release(); 
  332.         pLoc->Release(); 
  333.         CoUninitialize(); 
  334.         return 1; // Program has failed. 
  335.     } 
  336.     if(0 != CollectBaseBoardInfo(pSvc))
  337.     {
  338.         pLoc->Release(); 
  339.         CoUninitialize(); 
  340.         return 1;
  341.     }
  342.     if( 0 != CollectVideoControllerInfo(pSvc))
  343.     {
  344.         pLoc->Release(); 
  345.         CoUninitialize(); 
  346.         return 1;
  347.     }
  348.     if( 0 != CollectProcessorInfo(pSvc))
  349.     {
  350.         pLoc->Release(); 
  351.         CoUninitialize(); 
  352.         return 1;
  353.     }
  354.     if( 0 != CollectLogicalMemoryConfigurationInfo(pSvc))
  355.     {
  356.         pLoc->Release(); 
  357.         CoUninitialize(); 
  358.         return 1;
  359.     }
  360.     if( 0 != CollectOperatingSystemInfo(pSvc))
  361.     {
  362.         pLoc->Release(); 
  363.         CoUninitialize(); 
  364.         return 1;
  365.     }
  366.     // Cleanup 
  367.     // ======== 
  368.     pSvc->Release(); 
  369.     pLoc->Release(); 
  370.     CoUninitialize(); 
  371.     return 0; // Program successfully completed. 
  372. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值