Chinaunix首页 | 论坛 | 博客
  • 博客访问: 255584
  • 博文数量: 71
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 920
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-16 13:07
个人简介

我喜欢蓝天,也喜欢雨天。

文章分类

全部博文(71)

文章存档

2014年(14)

2013年(57)

分类: C/C++

2013-10-11 15:57:39

利用WMI获取系统信息,
一般步骤:
1. 通过调用CoInitialzeEx来初始化COM参数.

2. 通过调用CoInitializeSecurity来初始化COM过程安全.

3. 通过调用CoCreateInstance来实例化。

4. 通过调用IWbemLocator::ConnectServer来获取一个本机root\cimv2命名空间的IWbemServices的指针。(注意在字符串中要写“root\\cimv2”,两个\\)

5. 设置IWbemServices代理安全,WMI service可以通过调用CoSetProxyBlanket来模拟客户端。

6. 用IWbmeServices指针来查询WMI. 本例子通过调用IWbemServies::ExecQuery 来查询操作系统名称.
   SELEC * FROM Win32_OperatingSystem 
7. 获取和现实WQL查询的数据. 

WMI 可以访问的信息类型有:
   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_SerialPortConfiguration
   Win32_SMBIOSMemory
   Win32_SoundDevice
   Win32_SystemEnclosure
   Win32_SystemMemoryResource
   Win32_SystemSlot
   Win32_TapeDrive
   Win32_TemperatureProbe
   Win32_UninterruptiblePowerSupply
   Win32_USBController
   Win32_VideoConfiguration
   Win32_VideoController
   Win32_VoltageProbe

下面是自己写的得到product name的示例程序,dos下的命令语句是wmic.exe baseboard get product

.h文件

点击(此处)折叠或打开

  1. /*
  2. 通过WMI,得到product name
  3. */
  4. #pragma once
  5. #include "stdafx.h"
  6. #include <comdef.h>
  7. #include <Wbemidl.h>
  8. #include <Wbemcli.h>
  9. #include <conio.h>

  10. class GetProductName
  11. {
  12. public:
  13.     GetProductName();
  14.     ~GetProductName();
  15.     //正确返回0,错误返回非0
  16.     //nName:out 得到的product name
  17.     int Get(CString & nName);

  18. private:
  19.     HRESULT hres;
  20.     IEnumWbemClassObject* pEnumerator;
  21.     IWbemLocator *pLoc;
  22.     IWbemServices *pSvc;

  23.     // Step 1:
  24.     // Initialize COM.
  25.     BOOL Setp1(void);

  26.     // Step 2: --------------------------------------------------
  27.     // Set general COM security levels --------------------------
  28.     // Note: If you are using Windows 2000, you need to specify -
  29.     // the default authentication credentials for a user by using
  30.     // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
  31.     // parameter of CoInitializeSecurity ------------------------
  32.     BOOL Setp2(void);

  33.     // Step 3: ---------------------------------------------------
  34.     // Obtain the initial locator to WMI -------------------------
  35.     BOOL Setp3(void);

  36.     // Step 4: -----------------------------------------------------
  37.     // Connect to WMI through the IWbemLocator::ConnectServer method
  38.     // Connect to the root/cimv2 namespace with
  39.     // the current user and obtain pointer pSvc
  40.     // to make IWbemServices calls.
  41.     BOOL Setp4(void);

  42.     // Step 5: --------------------------------------------------
  43.     // Set security levels on the proxy -------------------------
  44.     BOOL Setp5(void);

  45.     // Step 6: --------------------------------------------------
  46.     // Use the IWbemServices pointer to make requests of WMI ----
  47.     // get the product name of the baseboard
  48.     BOOL Setp6(void);

  49.     // Step 7: -------------------------------------------------
  50.     // Get the data from the query in step 6 -------------------
  51.     BOOL Setp7(CString & nName);
  52. };

.cpp

点击(此处)折叠或打开

  1. #include "stdafx.h"
  2. #include "GetProductName.h"

  3. #pragma comment(lib, "wbemuuid.lib")

  4. using namespace std;

  5. GetProductName::GetProductName()
  6. {
  7.     hres = NULL;
  8.     pEnumerator = NULL;
  9.     pLoc = NULL;
  10.     pSvc = NULL;
  11. }
  12. GetProductName::~GetProductName()
  13. {
  14. }
  15. BOOL GetProductName::Setp1(void)
  16. {
  17.     // Step 1: --------------------------------------------------
  18.     // Initialize COM. ------------------------------------------

  19.     hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  20.     if (FAILED(hres))
  21.     {
  22.         return FALSE; // Program has failed.
  23.     }
  24.     return TRUE;
  25. }
  26. BOOL GetProductName::Setp2(void)
  27. {
  28.     // Step 2: --------------------------------------------------
  29.     // Set general COM security levels --------------------------
  30.     // Note: If you are using Windows 2000, you need to specify -
  31.     // the default authentication credentials for a user by using
  32.     // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
  33.     // parameter of CoInitializeSecurity ------------------------

  34.     hres = CoInitializeSecurity(
  35.         NULL,
  36.         -1, // COM authentication
  37.         NULL, // Authentication services
  38.         NULL, // Reserved
  39.         RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
  40.         RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
  41.         NULL, // Authentication info
  42.         EOAC_NONE, // Additional capabilities
  43.         NULL // Reserved
  44.         );


  45.     if (FAILED(hres))
  46.     {
  47.         CoUninitialize();
  48.         return FALSE; // Program has failed.
  49.     }
  50.     return TRUE;
  51. }
  52. BOOL GetProductName::Setp3(void)
  53. {
  54.     // Step 3: ---------------------------------------------------
  55.     // Obtain the initial locator to WMI -------------------------

  56.     hres = CoCreateInstance(
  57.         CLSID_WbemLocator,
  58.         0,
  59.         CLSCTX_INPROC_SERVER,
  60.         IID_IWbemLocator, (LPVOID *) &pLoc);

  61.     if (FAILED(hres))
  62.     {
  63.         CoUninitialize();
  64.         return FALSE; // Program has failed.
  65.     }
  66.     return TRUE;
  67. }
  68. BOOL GetProductName::Setp4(void)
  69. {
  70.     // Step 4: -----------------------------------------------------
  71.     // Connect to WMI through the IWbemLocator::ConnectServer method

  72.     // Connect to the root/cimv2 namespace with
  73.     // the current user and obtain pointer pSvc
  74.     // to make IWbemServices calls.
  75.     hres = pLoc->ConnectServer(
  76.         _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
  77.         NULL, // User name. NULL = current user
  78.         NULL, // User password. NULL = current
  79.         0, // Locale. NULL indicates current
  80.         NULL, // Security flags.
  81.         0, // Authority (e.g. Kerberos)
  82.         0, // Context object
  83.         &pSvc // pointer to IWbemServices proxy
  84.         );

  85.     if (FAILED(hres))
  86.     {
  87.         pLoc->Release();
  88.         CoUninitialize();
  89.         return FALSE; // Program has failed.
  90.     }
  91.     return TRUE;
  92. }
  93. BOOL GetProductName::Setp5(void)
  94. {
  95.     // Step 5: --------------------------------------------------
  96.     // Set security levels on the proxy -------------------------

  97.     hres = CoSetProxyBlanket(
  98.         pSvc, // Indicates the proxy to set
  99.         RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
  100.         RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
  101.         NULL, // Server principal name
  102.         RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
  103.         RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
  104.         NULL, // client identity
  105.         EOAC_NONE // proxy capabilities
  106.         );

  107.     if (FAILED(hres))
  108.     {
  109.         pSvc->Release();
  110.         pLoc->Release();
  111.         CoUninitialize();
  112.         return FALSE; // Program has failed.
  113.     }
  114.     return TRUE;
  115. }
  116. BOOL GetProductName::Setp6(void)
  117. {
  118.     // Step 6: --------------------------------------------------
  119.     // Use the IWbemServices pointer to make requests of WMI ----
  120.     // get the product name of the baseboard

  121.     hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_BaseBoard"),
  122.         WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
  123.     if (FAILED(hres))
  124.     {
  125.         pSvc->Release();
  126.         return FALSE; // Program has failed.
  127.     }
  128.     return TRUE;
  129. }
  130. BOOL GetProductName::Setp7(CString & nName)
  131. {
  132.     // Step 7: -------------------------------------------------
  133.     // Get the data from the query in step 6 -------------------
  134.     IWbemClassObject *pclsObj = NULL;

  135.     ULONG ulReturn = 0;

  136.     do
  137.     {
  138.         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &ulReturn);
  139.         if (!SUCCEEDED(hr))
  140.         {
  141.             break;
  142.             return FALSE;
  143.         }

  144.         VARIANT vtProp;
  145.         VariantInit(&vtProp);

  146.         hr = pclsObj->Get(L"product", 0, &vtProp, 0, 0);

  147.         nName = vtProp.bstrVal;

  148.         VariantClear(&vtProp);
  149.     } while(FALSE);

  150.     pEnumerator->Release();
  151.     pclsObj->Release();
  152.     return TRUE;
  153. }
  154. int GetProductName::Get(CString & nName)
  155. {
  156.     int lreturn = 0;
  157.     do
  158.     {
  159.         if (Setp1() == FALSE)
  160.         {
  161.             lreturn = 1;
  162.             break;
  163.         }
  164.         if (Setp2() == FALSE)
  165.         {
  166.             lreturn = 2;
  167.             break;
  168.         }
  169.         if (Setp3() == FALSE)
  170.         {
  171.             lreturn = 3;
  172.             break;
  173.         }
  174.         if (Setp4() == FALSE)
  175.         {
  176.             lreturn = 4;
  177.             break;
  178.         }
  179.         if (Setp5() == FALSE)
  180.         {
  181.             lreturn = 5;
  182.             break;
  183.         }
  184.         if (Setp6() == FALSE)
  185.         {
  186.             lreturn = 6;
  187.             break;
  188.         }
  189.         if (Setp7(nName) == FALSE)
  190.         {
  191.             lreturn = 7;
  192.             break;
  193.         }
  194.     } while (FALSE);

  195.     // Cleanup
  196.     pSvc->Release();
  197.     pLoc->Release();
  198.     CoUninitialize();
  199.     return lreturn;
  200. }


阅读(5631) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~