Windows下获取CPU、主板、硬盘等电脑相关硬件的办法很多,可以直接调用Windows API的方式实现,不过代码都很复杂,不容易理解。这里使用一种很简便的方式来查询我们需要的信息,先了解一个东西“wmic”(Windows Management Instrumentation,Windows管理工具),提供了从命令行接口和批命令脚本执行系统管理的支持。可以打开cmd在其中输入如下命令,获取相关的信息。
//获取cpu名称:wmic cpu get Name
//获取cpu核心数:wmic cpu get NumberOfCores
//获取cpu线程数:wmic cpu get NumberOfLogicalProcessors
//查询cpu***:wmic cpu get processorid
//查询主板***:wmic baseboard get serialnumber
//查询BIOS***:wmic bios get serialnumber
//查看硬盘:wmic diskdrive get serialnumber
//获取主板序唯一标识:wmic csproduct get uuid
//查询网卡连接唯一标识:Wmic Path Win32_NetworkAdapter get GUID
//查询网卡物理地址:Wmic Path Win32_NetworkAdapter get NetEnabled
//查询网卡是否启用:Wmic Path Win32_NetworkAdapter get NetEnabled
//查询网卡是否为物理适配器:Wmic Path Win32_NetworkAdapter get PhysicalAdapter
//查询网卡索引号:Wmic Path Win32_NetworkAdapter get Index
Qt获取
//程序中用到的头文件
#include <QProcess>
QString getInfo(const QString &cmd)
{
QProcess p; //启动外部程序
p.start(cmd); //一体式启动,不分离,主程序退出则启动程序退出,使用close关闭
//p.startDetached(cmd) //分离式启动,主程序退出后,外部程序继续运行。
p.waitForFinished(-1); //超时等待,设置为-1,直到执行完成。
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();
p.close();
return result;
}
//查询CPU型号
QString getCpuName()
{
return getInfo("wmic cpu get Name")
}
//查询CPU核心数
QString getCpuCore()
{
return getInfo("wmic cpu get NumberOfCores")
}
查询CPU线程数
QString getCpuProcessors()
{
return getInfo("wmic cpu get NumberOfLogicalProcessors")
}
//查询CPU***
QString getCpuProcessorid()
{
return getInfo("wmic cpu get processorid")
}
//查询主板***
QString getBaseboardSerialnumber()
{
return getInfo("wmic baseboard get serialnumber")
}
//查询BIOS***
QString getBiosSerialnumber()
{
return getInfo("wmic bios get serialnumber")
}
//查询主板唯一标识
QString getBaseboardUuid()
{
return getInfo("wmic csproduct get uuid")
}
//查询硬盘***
QString getDiskSerialnumber()
{
return getInfo("wmic diskdrive get serialnumber")
上面的办法可能在一些机器上不能实现,可能原因是因为系统预装时没有安装wmic工具,因此无法执行wmic命令,查了相关资料后,找到了另外一种办法,办法相对比较复杂,目前也只有获取CPU信息的程序。
void getcpuidex(unsigned int CPUInfo[4], unsigned int InfoType, unsigned int ECXValue)
{
#if defined(_MSC_VER)
#if defined(_WIN64)
__cpuidex((int*)(void*)CPUInfo, (int)InfoType, (int)ECXValue);
#else
if (NULL==CPUInfo) return;
_asm{
mov edi, CPUInfo;
mov eax, InfoType;
mov ecx, ECXValue;
cpuid;
mov [edi], eax;
mov [edi+4], ebx;
mov [edi+8], ecx;
mov [edi+12], edx;
}
#endif
#endif
}
void getcpuid(unsigned int CPUInfo[4], unsigned int InfoType)
{
#if defined(__GNUC__)
__cpuid(InfoType, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
#elif defined(_MSC_VER)
#if _MSC_VER >= 1400
__cpuid((int*)(void*)CPUInfo, (int)(InfoType));
#else
getcpuidex(CPUInfo, InfoType, 0);
#endif
#endif
}
QString get_cpuId()
{
QString cpu_id = "";
unsigned int dwBuf[4]={0};
unsigned long long ret = 0;
getcpuid(dwBuf, 1);
ret = dwBuf[3];
ret = ret << 32;
QString str0 = QString::number(dwBuf[3], 16).toUpper();
QString str0_1 = str0.rightJustified(8,'0');
QString str1 = QString::number(dwBuf[0], 16).toUpper();
QString str1_1 = str1.rightJustified(8,'0');
cpu_id = str0_1 + str1_1;
return cpu_id;
}