当前位置: 首页 > 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;
}


http://www.mrgr.cn/news/30429.html

相关文章:

  • 总结拓展十:SAP开发计划(下)
  • ①原装进口芯片一主多从RS485通讯转换器从站转地址波特率转校验位转寄存器转停止位modbus协议转换中继器
  • 进入C++
  • Python的基础知识,帮助初学者快速上手
  • Java键盘输入语句
  • 大模型-模型架构-长上下文模型
  • 3.使用 VSCode 过程中的英语积累 - Selection 菜单(每一次重点积累 5 个单词)
  • 面试官:什么是CAS?存在什么问题?
  • 【海康威视面经】
  • gcc升级(含命令行升级、手动升级两种方式)
  • 数据结构之二叉树遍历
  • 字节推音乐生成神器 Seed-Music 支持多样化输入和精确控制
  • C++初阶学习第六弹------标准库中的string类
  • 【新手上路】衡石分析平台使用手册-认证方式
  • 关于Java数据结构中集合的一个小知识
  • python 函数简记
  • 【iOS】KVC
  • 95分App引领年轻人省钱赚钱新风尚,闲置也能变宝藏
  • 内存管理篇-27寄存器映射:ioremap
  • 打工人、设计师必备的AI抠图工具