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

VC++ 获取CPU信息的两种方法

文章目录

  • 方法一:使用 Windows API `GetSystemInfo` 和 `GetNativeSystemInfo` (基本信息)
    • 编译和运行
    • 代码解释
  • 方法二:使用 `__cpuid`(CPU序列号、特性等)
    • 代码解释:

开发过程中需要使用 VC++获取电脑CPU信息,先总结两种方法如下:

方法一:使用 Windows API GetSystemInfoGetNativeSystemInfo (基本信息)

这是获取 CPU 基本信息(如核心数、架构)的最简单方法。代码如下:

#include <windows.h>
#include <iostream>
#include <string>int main() {SYSTEM_INFO sysInfo;GetSystemInfo(&sysInfo); // For applications running under WOW64 (32-bit on 64-bit OS),// this returns information about the emulated 32-bit environment.std::cout << "Using GetSystemInfo():" << std::endl;std::cout << "  Number of processors (cores/threads reported by OS): " << sysInfo.dwNumberOfProcessors << std::endl;std::cout << "  Processor type (deprecated, use wProcessorArchitecture): " << sysInfo.dwProcessorType << std::endl; // Deprecatedstd::cout << "  Processor architecture: ";switch (sysInfo.wProcessorArchitecture) {case PROCESSOR_ARCHITECTURE_AMD64:   std::cout << "x64 (AMD or Intel)" << std::endl; break;case PROCESSOR_ARCHITECTURE_ARM:     std::cout << "ARM" << std::endl; break;case PROCESSOR_ARCHITECTURE_ARM64:   std::cout << "ARM64" << std::endl; break;case PROCESSOR_ARCHITECTURE_IA64:    std::cout << "Intel Itanium-based" << std::endl; break; // Rarecase PROCESSOR_ARCHITECTURE_INTEL:   std::cout << "x86" << std::endl; break;default:                             std::cout << "Unknown architecture (" << sysInfo.wProcessorArchitecture << ")" << std::endl; break;}std::cout << "  Processor level: " << sysInfo.wProcessorLevel << std::endl;std::cout << "  Processor revision: " << sysInfo.wProcessorRevision << std::endl;std::cout << "  Page size: " << sysInfo.dwPageSize << " bytes" << std::endl;std::cout << "  Minimum application address: " << sysInfo.lpMinimumApplicationAddress << std::endl;std::cout << "  Maximum application address: " << sysInfo.lpMaximumApplicationAddress << std::endl;std::cout << "  Active processor mask: 0x" << std::hex << sysInfo.dwActiveProcessorMask << std::dec << std::endl;std::cout << std::endl;// For 32-bit applications running on 64-bit Windows (WOW64),// GetNativeSystemInfo provides information about the host system's processor.// For 64-bit applications, it's the same as GetSystemInfo.// It's generally better to use GetNativeSystemInfo if you want the true hardware info.SYSTEM_INFO nativeSysInfo;GetNativeSystemInfo(&nativeSysInfo);std::cout << "Using GetNativeSystemInfo():" << std::endl;std::cout << "  Number of processors (cores/threads reported by OS): " << nativeSysInfo.dwNumberOfProcessors << std::endl;std::cout << "  Processor architecture: ";switch (nativeSysInfo.wProcessorArchitecture) {case PROCESSOR_ARCHITECTURE_AMD64:   std::cout << "x64 (AMD or Intel)" << std::endl; break;case PROCESSOR_ARCHITECTURE_ARM:     std::cout << "ARM" << std::endl; break;case PROCESSOR_ARCHITECTURE_ARM64:   std::cout << "ARM64" << std::endl; break;case PROCESSOR_ARCHITECTURE_IA64:    std::cout << "Intel Itanium-based" << std::endl; break;case PROCESSOR_ARCHITECTURE_INTEL:   std::cout << "x86" << std::endl; break;default:                             std::cout << "Unknown architecture (" << nativeSysInfo.wProcessorArchitecture << ")" << std::endl; break;}std::cout << "  Processor level: " << nativeSysInfo.wProcessorLevel << std::endl;std::cout << "  Processor revision: " << nativeSysInfo.wProcessorRevision << std::endl;// To get logical processor information (NUMA nodes, core relationships, cache)// you would use GetLogicalProcessorInformation or GetLogicalProcessorInformationEx// which are more complex.return 0;
}

编译和运行

  1. 创建一个新的 C++ 空项目或 Win32 控制台应用程序项目。
  2. 将以上代码粘贴到主 .cpp 文件中。
  3. 编译并运行。

代码解释

  • GetSystemInfo: 获取当前进程运行环境的系统信息。如果一个 32 位程序运行在 64 位 Windows (WOW64) 上,它会返回模拟的 32 位环境信息。
  • GetNativeSystemInfo: 获取物理硬件的系统信息。在 64 位系统上,即使是 32 位程序调用它,也会得到 64 位系统的信息。通常推荐使用这个函数来获取真实的硬件信息。
  • dwNumberOfProcessors: 返回操作系统报告的逻辑处理器数量(可能是物理核心数,也可能是启用了超线程后的线程数)。
  • wProcessorArchitecture: 返回 CPU 的架构(x86, x64, ARM 等)。
  • wProcessorLevelwProcessorRevision: 提供关于处理器型号和修订版本的一些信息,但具体含义依赖于处理器架构。

运行效果如下图:
方法一运行效果

方法二:使用 __cpuid(CPU序列号、特性等)

引入intrin.h头文件,获取CPU序列号、特性等信息。代码如下:

#include <stdio.h>  
#include <windows.h>  #ifdef _MSC_VER  
#include <intrin.h> // 如果使用的是 MSVC 编译器,则包含此头文件以支持 __cpuid 指令  
#else  
#include <cpuid.h>  // 如果使用的是其他编译器,则包含此头文件以支持 __cpuid 指令  
#endif  int main()  
{  // 定义四个无符号整数变量,用于存储 CPUID 指令的返回值unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;  #ifdef _MSC_VER  // 如果使用的是 MSVC 编译器int cpuInfo[4];  // 定义一个数组用于存储 CPUID 指令的结果__cpuid(cpuInfo, 1);  // 调用 __cpuid 指令,功能号为 1,获取处理器信息eax = cpuInfo[0];  // 将返回值的 eax 寄存器内容存储到变量 eaxebx = cpuInfo[1];  // 将返回值的 ebx 寄存器内容存储到变量 ebxecx = cpuInfo[2];  // 将返回值的 ecx 寄存器内容存储到变量 ecxedx = cpuInfo[3];  // 将返回值的 edx 寄存器内容存储到变量 edx
#else  // 如果使用的是非 MSVC 编译器__cpuid(1, eax, ebx, ecx, edx);  // 直接调用 __cpuid 指令,功能号为 1,获取处理器信息
#endif  // 打印处理器 ID(ProcessorId),格式与命令 "wmic cpu get processorid" 的输出一致printf("ProcessorId:  %08X%08X\n", edx, eax);  // 打印 CPU 特性信息,分别显示 eax、ebx、ecx 和 edx 的值printf("CPU Features: %08X-%08X-%08X-%08X\n", eax, ebx, ecx, edx);  return 0;  // 返回 0,表示程序正常结束
}

代码解释:

  1. 头文件包含:
    #include <stdio.h>:用于标准输入输出(如 printf)。
    #include <windows.h>:提供 Windows 平台相关功能。
    • 根据编译器选择包含 intrin.hcpuid.h,以支持 __cpuid 指令。
  2. CPUID 指令:
    • CPUID 是一个 x86 指令,用于获取处理器的详细信息。
    • 功能号 1 用于获取处理器的特定信息,包括处理器 ID 和特性标志。
  3. 跨编译器支持:
    • 使用条件编译(#ifdef _MSC_VER)区分 MSVC 和其他编译器的实现方式。
  4. 输出:
    • ProcessorId 是由 edx 和 eax 组合而成的值,表示处理器的唯一标识。
    • CPU Features 显示了处理器支持的特性标志,分别存储在 eax、ebx、ecx 和 edx 中。

运行效果如下图:
方法二运行效果

http://www.xdnf.cn/news/397135.html

相关文章:

  • POSIX信号量
  • 【软件测试】基于项目驱动的功能测试报告(持续更新)
  • k8s中ingress-nginx介绍
  • Spring Boot 中的重试机制
  • 【Python】Python类型标注革命:Annotated类型深度解析与实战
  • 匈牙利算法
  • 信息系统项目管理师-软考高级(软考高项)​​​​​​​​​​​2025最新(十七)
  • java中对象的比较
  • 【文献阅读】地方政府驱动企业参与乡村振兴的机制——乡村振兴注意力视角的分析
  • 【工作记录】crmeb后端项目打开、运行
  • 【Flask开发踩坑实录】pip 安装报错:“No matching distribution found” 的根本原因及解决方案!
  • 1688 开放平台接口对接实战:商品实时数据采集 API 开发全流程
  • cmake:test project
  • OSPF的特殊区域
  • P10225 [COCI 2023/2024 #3] Milano C.le|普及
  • LeetCode 热题 100 543. 二叉树的直径
  • RS485和RS232 通信配置
  • TikTok 运营干货:内容创作与 AI 增效
  • 【高数上册笔记01】:从集合映射到区间函数
  • istio in action之应用弹性与容错机制
  • Babel 插件与预设的区别及使用
  • 每日脚本 5.11 - 进制转换和ascii字符
  • FlySecAgent:——MCP全自动AI Agent的实战利器
  • 运算放大器稳定性分析
  • MyBatis源码解读4(2.3、MyBatis运行流程)
  • 当虚拟吞噬现实——《GTA6》结合技术
  • 每日算法-250511
  • 广东省省考备考(第八天5.11)—言语:逻辑填空(每日一练)
  • AMD FPGA书籍推荐-初学者、一线工程师适用
  • 共享内存与信号量结合