文章目录
- 方法一:使用 Windows API `GetSystemInfo` 和 `GetNativeSystemInfo` (基本信息)
- 编译和运行
- 代码解释
- 方法二:使用 `__cpuid`(CPU序列号、特性等)
- 代码解释:
开发过程中需要使用
VC++
获取电脑CPU信息,先总结两种方法如下:
方法一:使用 Windows API GetSystemInfo
和 GetNativeSystemInfo
(基本信息)
这是获取 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;
}
编译和运行
- 创建一个新的 C++ 空项目或 Win32 控制台应用程序项目。
- 将以上代码粘贴到主
.cpp
文件中。 - 编译并运行。
代码解释
GetSystemInfo
: 获取当前进程运行环境的系统信息。如果一个 32 位程序运行在 64 位 Windows (WOW64) 上,它会返回模拟的 32 位环境信息。GetNativeSystemInfo
: 获取物理硬件的系统信息。在 64 位系统上,即使是 32 位程序调用它,也会得到 64 位系统的信息。通常推荐使用这个函数来获取真实的硬件信息。dwNumberOfProcessors
: 返回操作系统报告的逻辑处理器数量(可能是物理核心数,也可能是启用了超线程后的线程数)。wProcessorArchitecture
: 返回 CPU 的架构(x86, x64, ARM 等)。wProcessorLevel
和wProcessorRevision
: 提供关于处理器型号和修订版本的一些信息,但具体含义依赖于处理器架构。
运行效果如下图:
方法二:使用 __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,表示程序正常结束
}
代码解释:
- 头文件包含:
•#include <stdio.h>
:用于标准输入输出(如 printf)。
•#include <windows.h>
:提供 Windows 平台相关功能。
• 根据编译器选择包含intrin.h
或cpuid.h
,以支持__cpuid
指令。 - CPUID 指令:
• CPUID 是一个 x86 指令,用于获取处理器的详细信息。
• 功能号 1 用于获取处理器的特定信息,包括处理器 ID 和特性标志。 - 跨编译器支持:
• 使用条件编译(#ifdef _MSC_VER
)区分MSVC
和其他编译器的实现方式。 - 输出:
• ProcessorId 是由 edx 和 eax 组合而成的值,表示处理器的唯一标识。
• CPU Features 显示了处理器支持的特性标志,分别存储在 eax、ebx、ecx 和 edx 中。
运行效果如下图: