当前位置: 首页 > news >正文

Win32 Wmi获取设备信息

CDeviceHelper.h

#pragma once
#include <windows.h>
#include <string>
#include "CWmiQueryHelper.h"
#include <stdint.h>#ifdef _UNICODE
using _tstring = std::wstring;
#else
using _tstring = std::string;
#endif// 设备主板信息
using DEVICE_BASEBOARD_INFO = struct
{_tstring BaseBoardManufacturer;     //主板制造商_tstring BaseBoardProduct;          //主板产品_tstring BaseBoardVersion;          //主板版本
};// 设备BIOS信息
using DEVICE_BIOS_INFO = struct
{_tstring Manufacturer;              //制造商_tstring BIOSVersion;               //版本_tstring SerialNumber;              //序列号struct {uint16_t nYear;uint16_t nMonth;uint16_t nDay;uint16_t nHour;uint16_t nMinute;uint16_t nSecond;}BIOSReleaseDate;                   //发布日期int BiosMajorRelease;               //主版本号int BiosMinorRelease;               //子版本号int ECFirmwareMajorRelease;         //EC主版本号int ECFirmwareMinorRelease;         //EC子版本号
};// 设备系统信息
using DEVICE_SYSTEM_INFO = struct
{_tstring    SystemFamily;           // 家族_tstring    SystemManufacturer;     // 制造商_tstring    SystemProductName;      // 产品名_tstring    SystemSKU;              // SKU_tstring    SystemVersion;          // 系统版本
};// CPU信息
using DEVICE_CPU_INFO = struct
{_tstring Name;                      // CPU名int NumberOfCores;                  // CPU物理核心数int NumberOfLogicalProcessors;      // CPU逻辑核心数
};// 设备信息
using DEVICE_INFO = struct
{DEVICE_BASEBOARD_INFO baseBoard;    // 主板DEVICE_BIOS_INFO biosBoard;         // BIOSDEVICE_SYSTEM_INFO systemInfo;      // 系统DEVICE_CPU_INFO cpuInfo;            // CPU
};class CDeviceHelper
{
public:CDeviceHelper();virtual ~CDeviceHelper();// 初始化bool Initialize();// 反初始化void Uninitialize();DEVICE_INFO GetDeviceInfo();DEVICE_BASEBOARD_INFO GetBaseBoardInfo();DEVICE_BIOS_INFO GetBiosInfo();DEVICE_CPU_INFO GetCpuInfo();DEVICE_SYSTEM_INFO GetSystemInfo();private:CWmiQueryHelper m_Query;
};

CDeviceHelper.cpp

#include "CDeviceHelper.h"
#include <intrin.h>
#include <powerbase.h>#pragma comment(lib, "PowrProf.lib")static std::string _WStrToMultiStr(UINT CodePage, const std::wstring& str)
{int cbMultiByte = ::WideCharToMultiByte(CodePage, 0, str.c_str(), -1, NULL, 0, NULL, NULL);std::string strResult(cbMultiByte, 0);size_t nConverted = ::WideCharToMultiByte(CodePage, 0, str.c_str(), (int)str.size(), &strResult[0], (int)strResult.size(), NULL, NULL);strResult.resize(nConverted);return strResult;
}static std::wstring _MultiStrToWStr(UINT CodePage, const std::string& str)
{int cchWideChar = ::MultiByteToWideChar(CodePage, 0, str.c_str(), -1, NULL, 0);std::wstring strResult(cchWideChar, 0);size_t nConverted = ::MultiByteToWideChar(CodePage, 0, str.c_str(), (int)str.size(), &strResult[0], (int)strResult.size());strResult.resize(nConverted);return strResult;
}static std::wstring TStrToWStr(const _tstring& str)
{
#ifdef _UNICODEreturn str;
#elsereturn _MultiStrToWStr(CP_ACP, str);
#endif
}static std::string TStrToAStr(const _tstring& str)
{
#ifdef _UNICODEreturn _WStrToMultiStr(CP_ACP, str);
#elsereturn str;
#endif
}static _tstring WStrToTStr(const std::wstring& str)
{
#ifdef _UNICODEreturn str;
#elsereturn _WStrToMultiStr(CP_ACP, str);
#endif
}static _tstring AStrToTStr(const std::string& str)
{
#ifdef _UNICODEreturn _MultiStrToWStr(CP_ACP, str);
#elsereturn str;
#endif
}CDeviceHelper::CDeviceHelper()
{Initialize();
}CDeviceHelper::~CDeviceHelper()
{Uninitialize();
}bool CDeviceHelper::Initialize()
{if (!m_Query.Initialize()){return false;}return true;
}void CDeviceHelper::Uninitialize()
{m_Query.Uninitialize();
}DEVICE_BASEBOARD_INFO CDeviceHelper::GetBaseBoardInfo()
{DEVICE_BASEBOARD_INFO infoResult;m_Query.QueryClass(_T("Win32_BaseBoard"),[&infoResult](uint64_t nIndex,const CWmiClassHelper& obj) ->void {infoResult.BaseBoardManufacturer = obj.GetString(_T("Manufacturer"));infoResult.BaseBoardProduct = obj.GetString(_T("Product"));infoResult.BaseBoardVersion = obj.GetString(_T("Version"));});return infoResult;
}DEVICE_BIOS_INFO CDeviceHelper::GetBiosInfo()
{DEVICE_BIOS_INFO infoResult;infoResult.ECFirmwareMajorRelease = 0;infoResult.ECFirmwareMinorRelease = 0;infoResult.BIOSReleaseDate.nYear = 0;infoResult.BIOSReleaseDate.nMonth = 0;infoResult.BIOSReleaseDate.nDay = 0;infoResult.BIOSReleaseDate.nHour = 0;infoResult.BIOSReleaseDate.nMinute = 0;infoResult.BIOSReleaseDate.nSecond = 0;infoResult.BiosMajorRelease = 0;infoResult.BiosMinorRelease = 0;m_Query.QueryClass(_T("Win32_BIOS"),[&infoResult](uint64_t nIndex,const CWmiClassHelper& obj) ->void {_tstring strReleaseDate = obj.GetString(_T("ReleaseDate"));_stscanf_s(strReleaseDate.c_str(), _T("%4hd%2hd%2hd%2hd%2hd%2hd"), &infoResult.BIOSReleaseDate.nYear, &infoResult.BIOSReleaseDate.nMonth, &infoResult.BIOSReleaseDate.nDay, &infoResult.BIOSReleaseDate.nHour,&infoResult.BIOSReleaseDate.nMinute, &infoResult.BIOSReleaseDate.nSecond);infoResult.Manufacturer = obj.GetString(_T("Manufacturer"));infoResult.SerialNumber = obj.GetString(_T("SerialNumber"));infoResult.BIOSVersion = obj.GetString(_T("SMBIOSBIOSVersion"));infoResult.BiosMajorRelease = obj.GetUInt8(_T("SystemBiosMajorVersion"));infoResult.BiosMinorRelease = obj.GetUInt8(_T("SystemBiosMinorVersion"));infoResult.ECFirmwareMajorRelease = obj.GetUInt8(_T("EmbeddedControllerMajorVersion"));infoResult.ECFirmwareMinorRelease = obj.GetUInt8(_T("EmbeddedControllerMinorVersion"));});return infoResult;
}DEVICE_SYSTEM_INFO CDeviceHelper::GetSystemInfo()
{DEVICE_SYSTEM_INFO infoResult;m_Query.QueryClass(_T("Win32_ComputerSystem"),[&infoResult](uint64_t nIndex,const CWmiClassHelper& obj) ->void {infoResult.SystemFamily = obj.GetString(_T("SystemFamily"));infoResult.SystemManufacturer = obj.GetString(_T("Manufacturer"));infoResult.SystemSKU = obj.GetString(_T("SystemSKUNumber"));infoResult.SystemVersion = obj.GetString(_T("SystemFamily"));});m_Query.QueryClass(_T("Win32_ComputerSystemProduct"),[&infoResult](uint64_t nIndex,const CWmiClassHelper& obj) ->void {infoResult.SystemProductName = obj.GetString(_T("Name"));infoResult.SystemVersion = obj.GetString(_T("Version"));});m_Query.QueryClass(_T("Win32_SystemEnclosure"),[&infoResult](uint64_t nIndex,const CWmiClassHelper& obj) ->void {infoResult.SystemProductName = obj.GetString(_T("Name"));infoResult.SystemVersion = obj.GetString(_T("Version"));});return infoResult;
}_tstring GetCpuName()
{std::string cpuName;int cpuInfo[4] = { 0 };__cpuid(cpuInfo, 0x80000000);unsigned int extMaxCpuID = cpuInfo[0];if (extMaxCpuID >= 0x80000004){char cpuBrand[0x40] = { 0 };for (unsigned int i = 0x80000002; i <= 0x80000004; ++i){__cpuid(cpuInfo, i);memcpy(cpuBrand + (i - 0x80000002) * 16, cpuInfo, sizeof(cpuInfo));}// 去除末尾空格for (int i = _countof(cpuBrand) - 1; i >=0; i--){if (!(' ' == cpuBrand[i] || '\0' == cpuBrand[i])){break;}cpuBrand[i] = '\0';}cpuName = cpuBrand;}return AStrToTStr(cpuName);
}DEVICE_CPU_INFO CDeviceHelper::GetCpuInfo()
{DEVICE_CPU_INFO infoResult;infoResult.NumberOfCores = 0;infoResult.NumberOfLogicalProcessors = 0;infoResult.Name = GetCpuName();PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX pLogicalInfos = NULL;DWORD elementSize = 0;do{::GetLogicalProcessorInformationEx(RelationProcessorCore, pLogicalInfos, &elementSize);if (ERROR_INSUFFICIENT_BUFFER != ::GetLastError()){break;}pLogicalInfos = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)::HeapAlloc(::GetProcessHeap(), 0, elementSize);if (NULL == pLogicalInfos){break;}if (!::GetLogicalProcessorInformationEx(RelationProcessorCore, pLogicalInfos, &elementSize)){break;}LPBYTE lpPos = (LPBYTE)pLogicalInfos;DWORD dwSizeCount = elementSize;while (dwSizeCount > 0){PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX pInfo = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)lpPos;infoResult.NumberOfCores += 1;infoResult.NumberOfLogicalProcessors += LTP_PC_SMT == pInfo->Processor.Flags ? 2 : 1;dwSizeCount -= pInfo->Size;lpPos += pInfo->Size;}} while (false);if (NULL != pLogicalInfos){::HeapFree(::GetProcessHeap(), 0, pLogicalInfos);}/*typedef struct _PROCESSOR_POWER_INFORMATION {ULONG Number;ULONG MaxMhz;ULONG CurrentMhz;ULONG MhzLimit;ULONG MaxIdleState;ULONG CurrentIdleState;} PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION;PROCESSOR_POWER_INFORMATION processorPowerInfo[24] = {0};LONG lResult = ::CallNtPowerInformation(ProcessorInformation, NULL, 0, &processorPowerInfo, sizeof(processorPowerInfo));*/return infoResult;
}DEVICE_INFO CDeviceHelper::GetDeviceInfo()
{DEVICE_INFO infoResult;infoResult.baseBoard = GetBaseBoardInfo();infoResult.biosBoard = GetBiosInfo();infoResult.systemInfo = GetSystemInfo();infoResult.cpuInfo = GetCpuInfo();return infoResult;
}

 main.cpp

#include <iostream>
#include "Win32Utils/CDeviceHelper.h"int main()
{CDeviceHelper obj;clock_t tmBegin = ::clock();DEVICE_INFO info = obj.GetDeviceInfo();clock_t tmEnd = ::clock();printf("cost time: %d\r\n", tmEnd - tmBegin);return 0;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • VMware Workstation Player虚拟机Ubuntu启用Windows共享目录
  • 代码随想录八股训练营第四十天| C++
  • Leetcode Hot 100刷题记录 -Day14(矩阵置0)
  • Nacos未授权访问
  • 大工程师插件下载 官方地址
  • 【数据结构】十大经典排序算法总结与分析
  • Vue3.0组合式API:computed计算属性、watch监听器、watchEffect高级监听器
  • C++ 常用设计模式
  • 有源滤波器UAF42
  • Golang协程泄漏定位和排查
  • 项目小总结
  • 在CentOS上搭建NFS服务器
  • rtmp推流
  • yolov8多任务模型-目标检测+车道线检测+可行驶区域检测-yolo多检测头代码+教程
  • 硬件工程师笔试面试——变压器
  • 【5+】跨webview多页面 触发事件(二)
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • canvas 五子棋游戏
  • Date型的使用
  • ES6 学习笔记(一)let,const和解构赋值
  • java8-模拟hadoop
  • javascript 总结(常用工具类的封装)
  • JavaScript中的对象个人分享
  • java第三方包学习之lombok
  • Just for fun——迅速写完快速排序
  • SpriteKit 技巧之添加背景图片
  • Sublime Text 2/3 绑定Eclipse快捷键
  • 可能是历史上最全的CC0版权可以免费商用的图片网站
  • 你真的知道 == 和 equals 的区别吗?
  • 前端面试之CSS3新特性
  • 区块链共识机制优缺点对比都是什么
  • 如何选择开源的机器学习框架?
  • 山寨一个 Promise
  • 探索 JS 中的模块化
  • 一道闭包题引发的思考
  • LIGO、Virgo第三轮探测告捷,同时探测到一对黑洞合并产生的引力波事件 ...
  • 通过调用文摘列表API获取文摘
  • ​Java基础复习笔记 第16章:网络编程
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • ​MySQL主从复制一致性检测
  • (04)odoo视图操作
  • (42)STM32——LCD显示屏实验笔记
  • (办公)springboot配置aop处理请求.
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (附源码)springboot青少年公共卫生教育平台 毕业设计 643214
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (四)stm32之通信协议
  • (一)基于IDEA的JAVA基础12
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • (转)Google的Objective-C编码规范
  • (转)winform之ListView
  • (转载)跟我一起学习VIM - The Life Changing Editor
  • **PHP二维数组遍历时同时赋值
  • .NET Core 中的路径问题
  • .NET Core/Framework 创建委托以大幅度提高反射调用的性能