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;
}
