QT software development: Get CPU serial number, hard disk serial number, motherboard serial number (using wmic command)

1. Environmental introduction

QT version:  5.12.6

Environment: win10 64 bit

Compiler: MinGW 32

2. Function introduction

Obtain the CPU serial number, hard disk serial number, motherboard serial number, CPU name and other information through the wmic command.

It is more practical to do software encryption, data encryption, and software binding with one machine and one code.

WMIC extends WMI (Windows Management Instrumentation, Windows Management Tool) to provide support for executing system management from a command line interface and batch command scripts.

The wmic command is very powerful. On the win10 system, the wmic command is replaced by the "help Get-WmiObject" of powershell, but the wmic command can still be used normally.

This is the effect of powershell running:

The effect of running the wmic command in the dos window:

Introduction to commonly used wmic commands:

Get the cpu name:   wmic cpu get Name
Get the number of cpu cores: wmic cpu get NumberOfCores
Get the number of cpu threads: wmic cpu get NumberOfLogicalProcessors
Query the cpu serial number: wmic cpu get processorid
Query the motherboard serial number: wmic baseboard get serialnumber
Query the BIOS serial number: wmic bios get serialnumber
view hard disk serial number: wmic diskdrive where index=0 get serialnumber
view cpu details: wmic cpu list brief
view physical memory: wmic memphysical list brief
view logical memory: wmic memlogical list brief
view cache memory: wmic memcache list brief
view Virtual memory: wmic pagefile list brief
Check the network card: wmic nic list brief
Shut down the local computer:wmic process call create shutdown.exe to
obtain the process name and executable path: wmic process get name, executablepath

Delete the specified process (according to the process name):
wmic process where name="qq.exe" call terminate
or use
wmic process where name="qq.exe" delete

Delete the specified process (according to the process PID): wmic process where pid="123" delete
Create a new process: wmic process call create "C:\Program Files\Tencent\QQ\QQ.exe"

3. Software operation effect

Fourth, the core code

4.1 widget.h

#ifndef WIDGET_H#define WIDGET_H #include <QWidget>#include <QDebug>#include <QProcess>#include <windows.h> QT_BEGIN_NAMESPACEnamespace Ui { class Widget; }QT_END_NAMESPACE class Widget : public QWidget{    Q_OBJECT public:    Widget(QWidget *parent = nullptr);    ~Widget();     QString getWMIC(const QString &cmd);    QString getCpuName();    QString getCpuId();    QString getBaseboardNum();    QString getBiosNum();    QString getDiskNum();private slots:    void on_pushButton_get_clicked(); private:    Ui::Widget *ui;};#endif // WIDGET_H

4.2 widget.cpp

#include "widget.h"#include "ui_widget.h" /*工程: video_file_encryption日期: 2021-06-08作者: DS小龙哥环境: win10 QT5.12.6 MinGW32功能: 获取CPU名称*/QString Widget::getCpuName(){    QString cmd="wmic cpu get name";    QProcess p;    p.start(cmd);    p.waitForFinished();    QString result = QString::fromLocal8Bit(p.readAllStandardOutput());    QStringList list = cmd.split(" ");    result = result.remove(list.last(), Qt::CaseInsensitive);    result = result.replace("\r", "");    result = result.replace("\n", "");    result = result.simplified();    return result;} /*工程: video_file_encryption日期: 2021-06-08作者: DS小龙哥环境: win10 QT5.12.6 MinGW32功能: 获取CPU的ID*/QString Widget::getCpuId(){    QString cmd="wmic cpu get processorid";    QProcess p;    p.start(cmd);    p.waitForFinished();    QString result = QString::fromLocal8Bit(p.readAllStandardOutput());    QStringList list = cmd.split(" ");    result = result.remove(list.last(), Qt::CaseInsensitive);    result = result.replace("\r", "");    result = result.replace("\n", "");    result = result.simplified();    return result;} /*工程: video_file_encryption日期: 2021-06-08作者: DS小龙哥环境: win10 QT5.12.6 MinGW32功能: 查询主板序号*/QString Widget::getBaseboardNum(){    QString cmd="wmic baseboard get serialnumber";    QProcess p;    p.start(cmd);    p.waitForFinished();    QString result = QString::fromLocal8Bit(p.readAllStandardOutput());    QStringList list = cmd.split(" ");    result = result.remove(list.last(), Qt::CaseInsensitive);    result = result.replace("\r", "");    result = result.replace("\n", "");    result = result.simplified();    return result;} /*工程: video_file_encryption日期: 2021-06-08作者: DS小龙哥环境: win10 QT5.12.6 MinGW32功能: 查询BIOS序号*/QString Widget::getBiosNum(){    QString cmd="wmic bios get serialnumber";    QProcess p;    p.start(cmd);    p.waitForFinished();    QString result = QString::fromLocal8Bit(p.readAllStandardOutput());    QStringList list = cmd.split(" ");    result = result.remove(list.last(), Qt::CaseInsensitive);    result = result.replace("\r", "");    result = result.replace("\n", "");    result = result.simplified();    return result;} /*工程: video_file_encryption日期: 2021-06-08作者: DS小龙哥环境: win10 QT5.12.6 MinGW32功能: 查询硬盘序号*/QString Widget::getDiskNum(){    QString cmd="wmic diskdrive where index=0 get serialnumber";    QProcess p;    p.start(cmd);    p.waitForFinished();    QString result = QString::fromLocal8Bit(p.readAllStandardOutput());    QStringList list = cmd.split(" ");    result = result.remove(list.last(), Qt::CaseInsensitive);    result = result.replace("\r", "");    result = result.replace("\n", "");    result = result.simplified();    return result;} Widget::Widget(QWidget *parent)    : QWidget(parent)    , ui(new Ui::Widget){    ui->setupUi(this);    this->setWindowTitle("获取CPU信息");}  Widget::~Widget(){    delete ui;} /*工程: video_file_encryption日期: 2021-06-08作者: DS小龙哥环境: win10 QT5.12.6 MinGW32功能: 获取数据*/void Widget::on_pushButton_get_clicked(){    ui->lineEdit_cpu->setText(getCpuId());    ui->lineEdit_bord->setText(getBaseboardNum());    ui->lineEdit_cpu_name->setText(getCpuName());    ui->lineEdit_bios->setText(getBiosNum());    ui->lineEdit_disk->setText(getDiskNum());}

4.3 ui interface