文章目錄
- 方法一:使用 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 中。
運行效果如下圖: