一、前言

在当今计算机应用广泛的领域中,了解系统的内存、CPU和GPU使用情况是非常重要的。对于开发人员和系统管理员来说,准确获取这些信息可以帮助他们优化软件性能、诊断问题并做出相应的调整。在Windows平台上实现这一目标会涉及到调用Windows系统API,使用合适的工具和库来获取所需的信息。

本文将介绍如何使用Qt和Windows API来读取系统的内存、CPU和GPU使用详细信息。将提供一个完整的示例代码,展示了如何使用这些技术来获取系统的关键性能指标。通过阅读本文,将学习如何使用Qt框架和Windows API来实现这些功能,以及如何根据需求进行扩展和定制。

二、获取系统的配置信息

Windows下Qt读取系统的内存、CPU、GPU等使用信息_Windows

Windows下Qt读取系统的内存、CPU、GPU等使用信息_Windows_02

#include <QApplication>

#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QLabel>
#include <QSysInfo>
#include <QProcess>
#include <QDebug>

#include <QDebug>
#include <Windows.h>


#pragma execution_character_set("utf-8")

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    QMainWindow window;
     window.resize(400, 300);

     QLabel *label = new QLabel(&window);
     label->setAlignment(Qt::AlignCenter);
     label->setWordWrap(true);
     window.setCentralWidget(label);

     // 获取系统内存信息
     QString memoryInfo = "Memory Information:\n";

     MEMORYSTATUSEX memoryStatus;
         memoryStatus.dwLength = sizeof(memoryStatus);
         if (GlobalMemoryStatusEx(&memoryStatus)) {
             memoryInfo+=QString("Total Physical Memory: %1 %2\n").arg(memoryStatus.ullTotalPhys / (1024 * 1024)).arg("MB");
             memoryInfo+=QString("Available Physical Memory: %1 %2\n").arg(memoryStatus.ullAvailPhys / (1024 * 1024)).arg("MB");
             memoryInfo+=QString("Total Virtual Memory: %1 %2\n").arg(memoryStatus.ullTotalVirtual / (1024 * 1024)).arg("MB");
             memoryInfo+=QString("Available Virtual Memory: %1 %2\n").arg(memoryStatus.ullAvailVirtual / (1024 * 1024)).arg("MB");
         } else {
             memoryInfo+=QString("无法获取内存使用情况信息。\n");
         }

     // 获取CPU信息
     QString cpuInfo = "CPU Information:\n";
     QProcess cpuProcess;
     cpuProcess.start("wmic cpu get Name");
     cpuProcess.waitForFinished();
     QString cpuResult = cpuProcess.readAllStandardOutput();
     QString cpuName = cpuResult.split("\n").at(1).trimmed();
     cpuInfo += "Model: " + cpuName + "\n";

     // 获取GPU信息
     QString gpuInfo = "GPU Information:\n";
     QProcess gpuProcess;
     gpuProcess.start("wmic path win32_VideoController get Name");
     gpuProcess.waitForFinished();
     QString gpuResult = gpuProcess.readAllStandardOutput();
     QStringList gpuList = gpuResult.split("\n", QString::SkipEmptyParts);
     for (int i = 1; i < gpuList.size(); i++) {
         QString gpuName = gpuList.at(i).trimmed();
         gpuInfo += "GPU " + QString::number(i) + ": " + gpuName + "\n";
     }

     // 在标签中显示系统信息
     QString systemInfo = memoryInfo + "\n" + cpuInfo + "\n" + gpuInfo;
     label->setText(systemInfo);

     window.show();


    //Widget w;
    //w.show();
    return a.exec();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.

三、wmic

wmic是Windows Management Instrumentation Command-line(WMI命令行)实用工具的缩写。它提供了一个命令行界面,可以通过WMI接口与操作系统进行交互和管理。以下是对wmic的详细介绍:

【1】基本概念:Windows Management Instrumentation(WMI)是微软提供的一种标准化的系统管理技术,允许开发人员和管理员使用编程方式来监视和控制Windows操作系统上的资源。WMI提供了一个信息框架,以获取有关计算机硬件、软件和操作系统配置的详细信息。

【2】功能:wmic允许用户通过命令行执行各种系统管理任务,包括查询、修改和监视操作系统中的各种设置和资源,如进程、服务、磁盘驱动器、网络适配器等。它还可以与远程计算机通信,并将结果输出为文本、XML或HTML格式。通过wmic,你可以轻松地获取系统信息、执行管理任务和编写自动化脚本。

【3】语法和用法:wmic的基本语法是wmic <命令> [参数]

常用的命令包括:

  • wmic os:获取操作系统的详细信息。
  • wmic cpu:获取CPU的信息。
  • wmic process:获取正在运行的进程列表。
  • wmic service:获取系统服务的信息。
  • wmic logicaldisk:获取逻辑磁盘驱动器的信息。
  • wmic nicconfig:获取网络适配器配置的信息。

示例用法:以下是使用wmic命令获取操作系统信息和CPU信息的示例:

  • wmic os get Caption, Version, OSArchitecture:获取操作系统的名称、版本和体系结构。
  • wmic cpu get Name, MaxClockSpeed, Manufacturer:获取CPU的名称、最大时钟速度和制造商。

对于更复杂的查询和操作,可以使用WQL(WMI查询语言)来结合wmic命令。WQL类似于SQL,可以用于过滤和排序数据,并执行高级的系统管理任务。