说明: 使用WMIC获取计算机的一些硬件信息,该方法只能在windows上使用。
可获取内容如下:
查询cpu信息:wmic cpu get DeviceID,name,NumberOfCores,NumberOfLogicalProcessors获取cpu名称:wmic cpu get Name查询cpu序列号:wmic cpu get processorid获取cpu核心数:wmic cpu get NumberOfCores获取cpu线程数:wmic cpu get NumberOfLogicalProcessors查询主板序列号:wmic baseboard get serialnumber获取主板信息:wmic csproduct查询BIOS序列号:wmic bios get serialnumber查询磁盘信息:wmic logicaldisk get name,Description,filesystem,size,freespace查看硬盘:wmic diskdrive get serialnumber查询第一个磁盘序列号:wmic diskdrive where index=0 get serialnumber查询不为空的MAC地址:wmic nic where netconnectionid!=NULL get MacAddress查询内存信息: wmic memorychip get Capacity,TAG查询系统信息: wmic os get Caption,CSDVersion,CSName,SerialNumber,Version代码实现如下:
#include <QProcess> QString getWMIC(const QString &cmd) { 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; } QString getCpuName() //获取cpu名称:wmic cpu get Name { return getWMIC("wmic cpu get name"); } QString getCpuId() //查询cpu序列号:wmic cpu get processorid { return getWMIC("wmic cpu get processorid"); } QString getCpuCoresNum() //获取cpu核心数:wmic cpu get NumberOfCores { return getWMIC("wmic cpu get NumberOfCores"); } QString getCpuLogicalProcessorsNum() //获取cpu线程数:wmic cpu get NumberOfLogicalProcessors { return getWMIC("wmic cpu get NumberOfLogicalProcessors"); } QString getDiskNum() //查看硬盘:wmic diskdrive get serialnumber { return getWMIC("wmic diskdrive where index=0 get serialnumber"); } QString getBaseBordNum() //查询主板序列号:wmic baseboard get serialnumber { return getWMIC("wmic baseboard get serialnumber"); } QString getBiosNum() //查询BIOS序列号:wmic bios get serialnumber { return getWMIC("wmic bios get serialnumber"); } //其他一样使用示例:
int main(int argc, char *argv[]) { QApplication a(argc, argv); qDebug()<<"getCpuName:"<<getCpuName(); qDebug()<<"getCpuId:"<<getCpuId(); qDebug()<<"getCpuCoresNum:"<<getCpuCoresNum(); qDebug()<<"getCpuLogicalProcessorsNum:"<<getCpuLogicalProcessorsNum(); qDebug()<<"getDiskNum:"<<getDiskNum(); qDebug()<<"getBaseBordNum:"<<getBaseBordNum(); qDebug()<<"getBiosNum:"<<getBiosNum(); }输出结果:
getCpuName: "Intel(R) Core(TM) i5-9500 CPU @ 3.00GHz" getCpuId: "BFEBFBFF000906EA" getCpuCoresNum: "6" getCpuLogicalProcessorsNum: "6" getDiskNum: "WD-WCC6Y4VJNRVN" getBaseBordNum: "" getBiosNum: "M70E1VP5"