Windows客户端开发--使用WMI获取显卡详细信息(win32控制台程序)

昨天接受了WMI的基础知识,因为自己也是刚刚接触,也是应用型的,没有研究的更深更透。

今天,就写一个真是的例子吧。

比如说,我们要获得计算机显示适配器的详细信息,包括显卡的型号,显卡驱动的版本号,以及显卡当前的状态。

Win32_VideoController

class Win32_VideoController : CIM_PCVideoController {
uint16 AcceleratorCapabilities[];
string AdapterCompatibility;
string AdapterDACType;
uint32 AdapterRAM;
uint16 Availability;
string CapabilityDescriptions[];
string Caption;
uint32 ColorTableEntries;
uint32 ConfigManagerErrorCode;
boolean ConfigManagerUserConfig;
string CreationClassName;
uint32 CurrentBitsPerPixel;
uint32 CurrentHorizontalResolution;
uint64 CurrentNumberOfColors;
uint32 CurrentNumberOfColumns;
uint32 CurrentNumberOfRows;
uint32 CurrentRefreshRate;
uint16 CurrentScanMode;
uint32 CurrentVerticalResolution;
string Description;
string DeviceID;
uint32 DeviceSpecificPens;
uint32 DitherType;
datetime DriverDate;
string DriverVersion;
boolean ErrorCleared;
string ErrorDescription;
uint32 ICMIntent;
uint32 ICMMethod;
string InfFilename;
string InfSection;
datetime InstallDate;
string InstalledDisplayDrivers;
uint32 LastErrorCode;
uint32 MaxMemorySupported;
uint32 MaxNumberControlled;
uint32 MaxRefreshRate;
uint32 MinRefreshRate;
boolean Monochrome;
string Name;
uint16 NumberOfColorPlanes;
uint32 NumberOfVideoPages;
string PNPDeviceID;
uint16 PowerManagementCapabilities[];
boolean PowerManagementSupported;
uint16 ProtocolSupported;
uint32 ReservedSystemPaletteEntries;
uint32 SpecificationVersion;
string Status;
uint16 StatusInfo;
string SystemCreationClassName;
string SystemName;
uint32 SystemPaletteEntries;
datetime TimeOfLastReset;
uint16 VideoArchitecture;
uint16 VideoMemoryType;
uint16 VideoMode;
string VideoModeDescription;
string VideoProcessor;
};

英文看着费劲是吗:

AcceleratorCapabilities  --图形和视频控制器的三维阵列的能力
AdapterCompatibility  --用于此控制器与系统比较兼容性一般芯片组
AdapterDACType  --姓名或数字 - 模拟转换器(DAC)芯片的标识符
AdapterRAM  --视频适配器的内存大小
Availability  --可用性和设备的状态
CapabilityDescriptions  --自由形式字符串提供更详细的解释中的任何加速器能力阵列所指示的视频加速器功能
Caption  --对象的简短描述
ColorTableEntries  --尺寸系统的色表
ConfigManagerErrorCode  --Win32的配置管理器错误代码
ConfigManagerUserConfig  --如果为TRUE,该装置是使用用户定义的配置
CreationClassName  --第一个具体类的名称出现在创建实例所使用的继承链
CurrentBitsPerPixel  --使用的比特数以显示每个像素
CurrentHorizontalResolution  --水平像素的当前数量
CurrentNumberOfColors  --在当前的分辨率支持的色彩数目
CurrentNumberOfColumns  --此视频控制器列(如果在字符模式下)编号
CurrentNumberOfRows  --此视频控制器行(如果在字符模式下)编号
CurrentRefreshRate  --频率在该视频控制器刷新监视器的图像
CurrentScanMode  --当前扫描模式
CurrentVerticalResolution  --当前垂直像素数量
Description  --描述
DeviceID  --该视频控制器标识符(唯一的计算机系统)
DeviceSpecificPens  --目前许多设备专用笔。值0xFFFF表示设备不支持笔。
DitherType  --抖动型视频控制器
DriverDate  --当前已安装的视频驱动程序的最后修改日期和时间
DriverVersion  --视频驱动程序的版本号
ErrorCleared  --如果为真,报上一个错误代码属性中的错误现已清除
ErrorDescription  --可能采取的纠正措施字符串提供有关记录在一个错误代码属性错误的详细信息
ICMIntent  --应使用默认三种可能的配色方法或意图中的一个特定值
ICMMethod  --处理ICM方法。对于非ICM的应用程序,这个属性决定了ICM是否已启用对于ICM的应用程序,系统将检查此属性来确定如何处理ICM支持
InfFilename  --视频适配器的路径.inf文件
InfSection  --Windows的视频信息所在的.inf文件
InstallDate  --安装的日期
InstalledDisplayDrivers  --已安装的显示设备驱动程序的名称
LastErrorCode  --报告的逻辑设备上一个错误代码 
MaxMemorySupported  --以字节为单位支持的内存最高限额
MaxNumberControlled  --可支持通过该控制器可直接寻址的实体的最大数量
MaxRefreshRate  --在赫兹视频控制器的最大刷新率
MinRefreshRate   --在赫兹视频控制器的最小刷新率
Monochrome  --如果是TRUE,灰阶用于显示图像。
Name  --标签由该对象是已知的。当子类,该属性可以被覆盖是一个关键属性。
NumberOfColorPlanes  --当前一些颜色平面。如果该值不适用于当前视频的配置,输入0(零)
NumberOfVideoPages  --当前的分辨率和可用内存支持视频页数
PNPDeviceID  --即插即用逻辑设备的播放装置识别符
PowerManagementCapabilities  --逻辑设备的特定功率相关的能力阵列
PowerManagementSupported  --如果为TRUE,该装置可以是电源管理(可以投入挂起模式,等等)
ProtocolSupported  --由控制器使用协议访问“控制”的设备
ReservedSystemPaletteEntries  --系统调色板保留的条目数
SpecificationVersion  --初始化数据规范的版本号(在其上的结构的基础)
Status  --对象的当前状态
StatusInfo  --对象的当前状态详细信息
SystemCreationClassName  --该作用域计算机的创建类别名称属性的值
SystemName  --系统的名称
SystemPaletteEntries  --当前一些系统调色板颜色索引条目
TimeOfLastReset  --该控制器是最后一次复位日期和时间,这可能意味着该控制器被断电或重新初始化
VideoArchitecture  --视频体系结构的类型
VideoMemoryType  --显存类型
VideoMode  --当前视频模式
VideoModeDescription  --当前的分辨率,颜色和视频控制器的扫描模式设置
VideoProcessor  --无格式的字符串描述视频处理器

获取计算机显卡详细信息,全部代码:

#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
#include <string>

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

int main(int argc, char **argv)
{
  HRESULT hres;

  // Initialize COM.
  hres = CoInitializeEx(0, COINIT_APARTMENTTHREADED);
  if (FAILED(hres))
  {
    cout << "Failed to initialize COM library. "
      << "Error code = 0x"
      << hex << hres << endl;
    return 1;              // Program has failed.
  }

  // Initialize 
  hres = CoInitializeSecurity(
    NULL,
    -1,      // COM negotiates service 
    NULL,    // Authentication services
    NULL,    // Reserved
    RPC_C_AUTHN_LEVEL_DEFAULT,    // authentication
    RPC_C_IMP_LEVEL_IMPERSONATE,  // Impersonation
    NULL,             // Authentication info 
    EOAC_NONE,        // Additional capabilities
    NULL              // Reserved
    );


  if (FAILED(hres))
  {
    cout << "Failed to initialize security. "
      << "Error code = 0x"
      << hex << hres << endl;
    CoUninitialize();
    return 1;          // Program has failed.
  }

  // Obtain the initial locator to Windows Management
  // on a particular host computer.
  IWbemLocator *pLoc = 0;

  hres = CoCreateInstance(
    CLSID_WbemLocator,
    0,
    CLSCTX_INPROC_SERVER,
    IID_IWbemLocator, (LPVOID *)&pLoc);

  if (FAILED(hres))
  {
    cout << "Failed to create IWbemLocator object. "
      << "Error code = 0x"
      << hex << hres << endl;
    CoUninitialize();
    return 1;       // Program has failed.
  }

  IWbemServices *pSvc = 0;

  // Connect to the root\cimv2 namespace with the
  // current user and obtain pointer pSvc
  // to make IWbemServices calls.

  hres = pLoc->ConnectServer(

    _bstr_t(L"ROOT\\CIMV2"), // WMI namespace
    NULL,                    // User name
    NULL,                    // User password
    0,                       // Locale
    NULL,                    // Security flags 
    0,                       // Authority 
    0,                       // Context object
    &pSvc                    // IWbemServices proxy
    );

  if (FAILED(hres))
  {
    cout << "Could not connect. Error code = 0x"
      << hex << hres << endl;
    pLoc->Release();
    CoUninitialize();
    return 1;                // Program has failed.
  }

  cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;

  // Set the IWbemServices proxy so that impersonation
  // of the user (client) occurs.
  hres = CoSetProxyBlanket(

    pSvc,                         // the proxy to set
    RPC_C_AUTHN_WINNT,            // authentication service
    RPC_C_AUTHZ_NONE,             // authorization service
    NULL,                         // Server principal name
    RPC_C_AUTHN_LEVEL_CALL,       // authentication level
    RPC_C_IMP_LEVEL_IMPERSONATE,  // impersonation level
    NULL,                         // client identity 
    EOAC_NONE                     // proxy capabilities 
    );

  if (FAILED(hres))
  {
    cout << "Could not set proxy blanket. Error code = 0x"
      << hex << hres << endl;
    pSvc->Release();
    pLoc->Release();
    CoUninitialize();
    return 1;               // Program has failed.
  }


  // Use the IWbemServices pointer to make requests of WMI. 
  // Make requests here:

  // For example, query for all the running processes
  IEnumWbemClassObject* pEnumerator = NULL;
  hres = pSvc->ExecQuery(
    bstr_t("WQL"),
    bstr_t("SELECT * FROM Win32_VideoController"),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &pEnumerator);

  if (FAILED(hres))
  {
    cout << "Query for processes failed. "
      << "Error code = 0x"
      << hex << hres << endl;
    pSvc->Release();
    pLoc->Release();
    CoUninitialize();
    return 1;               // Program has failed.
  }
  else
  {
    IWbemClassObject *pclsObj;
    ULONG uReturn = 0;

    while (pEnumerator)
    {
      hres = pEnumerator->Next(WBEM_INFINITE, 1,
        &pclsObj, &uReturn);
      if (0 == uReturn)
      {
        break;
      }

      VARIANT vtProp;
      VARIANT vtProp2;

      // Get the value of the Name property
      hres = pclsObj->Get(L"Description", 0, &vtProp, 0, 0);
      WCHAR* des;
      des = vtProp.bstrVal;
      wcout << "Description: " << vtProp.bstrVal << endl;

      hres = pclsObj->Get(L"Availability", 0, &vtProp2, 0, 0);
      wcout << "Availability: " << vtProp2.uintVal << endl;

      VariantClear(&vtProp);
      VariantClear(&vtProp2);
    }

  }

  // Cleanup
  // ========

  pSvc->Release();
  pLoc->Release();
  CoUninitialize();

  return 0;   
}

无图无真相,运行结果:
Windows客户端开发--使用WMI获取显卡详细信息(win32控制台程序)_第1张图片

注意
wcout << “Description: ” << vtProp.bstrVal << endl;

wcout << “Availability: ” << vtProp2.uintVal << endl;

解析的时候类型不用弄错了bstrVal或是uintVal

你可能感兴趣的:(windows,显卡,wmi)