使用 VS2022 通过 C++ 检测 CPU 和内存的详细步骤及教程

更新时间:2025-08-17 23:18:41作者:hnzkhbsb
在 Windows 系统的日常运维和性能监控中,检测 CPU 与内存的使用情况是非常重要的。本文将通过 Visual Studio 2022 (VS2022),使用 C++ 编程,演示如何获取 Windows 系统下的 CPU 信息和内存使用情况。本文从环境搭建到代码实现,逐步拆解,让你能够完全落地操作。


使用 VS2022 通过 C++ 检测 CPU 和内存的详细步骤及教程



一、准备环境



操作系统要求Windows 10 / Windows 11 (64位)管理员权限开发工具Visual Studio 2022安装时选择 Desktop development with C++ 工作负载运行库Windows API(自带,无需额外安装)






二、新建 VS2022 项目



打开 VS2022点击 Create a new project选择 Console Application (C++),点击 Next填写项目名称,如 CpuMemMonitor点击 Create 创建项目






三、检测 CPU 信息



在 Windows 下,可以通过 GetSystemInfo() 和 __cpuid 指令获取 CPU 的厂商、型号和核心数。



示例代码:获取 CPU 信息


#include <iostream>

#include <windows.h>

#include <intrin.h>


void GetCPUInfo() {

SYSTEM_INFO sysInfo;

GetSystemInfo(&sysInfo);


// CPU 内核数

std::cout << "CPU 核心数: " << sysInfo.dwNumberOfProcessors << std::endl;


// 获取 CPU 厂商信息

int cpuInfo[4] = { -1 };

char cpuBrand[0x40];

__cpuid(cpuInfo, 0x80000002);

memcpy(cpuBrand, cpuInfo, sizeof(cpuInfo));

__cpuid(cpuInfo, 0x80000003);

memcpy(cpuBrand + 16, cpuInfo, sizeof(cpuInfo));

__cpuid(cpuInfo, 0x80000004);

memcpy(cpuBrand + 32, cpuInfo, sizeof(cpuInfo));


std::cout << "CPU 型号: " << cpuBrand << std::endl;

}





四、检测内存信息



Windows 提供了 GlobalMemoryStatusEx() API,可直接获取物理内存总量和可用内存。



示例代码:获取内存信息


#include <iostream>

#include <windows.h>


void GetMemoryInfo() {

MEMORYSTATUSEX statex;

statex.dwLength = sizeof(statex);

GlobalMemoryStatusEx(&statex);


std::cout << "物理内存总量: "

<< (statex.ullTotalPhys / (1024 * 1024 * 1024))

<< " GB" << std::endl;


std::cout << "可用物理内存: "

<< (statex.ullAvailPhys / (1024 * 1024 * 1024))

<< " GB" << std::endl;


std::cout << "内存使用率: "

<< statex.dwMemoryLoad << "%" << std::endl;

}





五、整合完整代码



下面给出一个完整的 C++ 程序,能够同时检测 CPU 信息 和 内存信息。

#include <iostream>

#include <windows.h>

#include <intrin.h>

using namespace std;


void GetCPUInfo() {

SYSTEM_INFO sysInfo;

GetSystemInfo(&sysInfo);


cout << "CPU 核心数: " << sysInfo.dwNumberOfProcessors << endl;


int cpuInfo[4] = { -1 };

char cpuBrand[0x40];

__cpuid(cpuInfo, 0x80000002);

memcpy(cpuBrand, cpuInfo, sizeof(cpuInfo));

__cpuid(cpuInfo, 0x80000003);

memcpy(cpuBrand + 16, cpuInfo, sizeof(cpuInfo));

__cpuid(cpuInfo, 0x80000004);

memcpy(cpuBrand + 32, cpuInfo, sizeof(cpuInfo));


cout << "CPU 型号: " << cpuBrand << endl;

}


void GetMemoryInfo() {

MEMORYSTATUSEX statex;

statex.dwLength = sizeof(statex);

GlobalMemoryStatusEx(&statex);


cout << "物理内存总量: "

<< (statex.ullTotalPhys / (1024 * 1024 * 1024))

<< " GB" << endl;


cout << "可用物理内存: "

<< (statex.ullAvailPhys / (1024 * 1024 * 1024))

<< " GB" << endl;


cout << "内存使用率: "

<< statex.dwMemoryLoad << "%" << endl;

}


int main() {

cout << "===== 系统硬件检测 =====" << endl;

GetCPUInfo();

cout << "------------------------" << endl;

GetMemoryInfo();

cout << "========================" << endl;

return 0;

}





六、运行效果



编译运行后,输出示例:

===== 系统硬件检测 =====

CPU 核心数: 8

CPU 型号: Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz

------------------------

物理内存总量: 16 GB

可用物理内存: 10 GB

内存使用率: 37%

========================





七、总结



通过本文,你学会了如何:


使用 VS2022 创建一个 C++ 控制台项目通过 Windows API 获取 CPU 核心数和型号使用 GlobalMemoryStatusEx 获取内存总量、可用内存和使用率编写一个完整可运行的 C++ 程序,实现 CPU 与内存监控

相关教程

copyright ©  2012-2025 系统家园网 m.hnzkhbsb.com 版权声明