#include "SysInfoExt.h" #include #ifdef Q_OS_LINUX # include # include #endif namespace SysInfoExt { #ifdef Q_OS_LINUX struct CPUInfo { QString cpuID; QString devID; }; QByteArray GetMD5Hash(QByteArray buff) { QCryptographicHash hash(QCryptographicHash::Md5); hash.addData(buff); return hash.result().toHex(); } QString GetCpuId() { QString cpuid = ""; unsigned int dwBuf[4]; int InfoType = 1; __cpuid(InfoType, dwBuf[0], dwBuf[1], dwBuf[2], dwBuf[3]); __cpuid_count(InfoType, 0, dwBuf[0], dwBuf[1], dwBuf[2], dwBuf[3]); cpuid = QString::number(dwBuf[3], 16).toUpper(); cpuid = cpuid + QString::number(dwBuf[0], 16).toUpper(); return cpuid; } QString GetMeCode() { QString strMe = GetCpuId(); QByteArray ba = strMe.toLatin1(); QString strTemp = ba.toBase64(); return strTemp; } CPUInfo getCPUInfo() { CPUInfo cpuinfo; QByteArray hwid = GetMeCode().toUtf8().toHex(); QByteArray algo = {}; uint8_t hwid_cnt = hwid.length() / 2; for (int i = 0; i < hwid_cnt; i++) { QByteArray sum0 = GetMD5Hash(hwid.mid(i * 2, 2)).mid(hwid_cnt, 2); QByteArray csum = GetMD5Hash(sum0).mid(i, 2).toUpper(); algo.append(csum); } const QString pcuid = { QString("%0").arg(GetMD5Hash(algo).data()) }; cpuinfo.cpuID.append(QString("%0").arg(hwid.data()).toUpper()); cpuinfo.devID.append(QString("%0").arg(pcuid).toUpper()); return cpuinfo; } #endif QString fixInfo(const QString &str) { QString temp = str; // \r \n 去除 temp.remove("\r"); temp.remove("\n"); // 去除多余的空格 QStringList list; for (auto s : temp.split(" ")) { if (!s.trimmed().isEmpty()) { list.append(s); } } return list.join(" "); } /** * @brief 通用信息获取 * @param cmd * @return */ QString getWMIC(const QString &cmd) { QProcess p; p.start(cmd); p.waitForFinished(); QString res = QString::fromLocal8Bit(p.readAllStandardOutput()); return fixInfo(res); } /** * 功能: 获取CPU的名称 */ QString getCpuName() { #ifdef Q_OS_WIN return getWMIC("wmic cpu get name"); #endif #ifdef Q_OS_LINUX return getWMIC("grep -m 1 'model name' /proc/cpuinfo"); #endif return ""; } /** * 功能: 获取CPU的ID */ QString getCpuId() { #ifdef Q_OS_WIN return getWMIC("wmic cpu get processorid"); #endif return ""; } /** * 功能: 查询主板序号 */ QString getBaseboardNum() { #ifdef Q_OS_WIN return getWMIC("wmic baseboard get serialnumber"); #endif return ""; } /** * 功能: 查询BIOS序号 */ QString getBiosNum() { #ifdef Q_OS_WIN return getWMIC("wmic bios get serialnumber"); #endif return ""; } /** * 功能: 查询硬盘序号 */ QString getDiskNum() { #ifdef Q_OS_WIN return getWMIC("wmic diskdrive where index=0 get serialnumber"); #endif #ifdef Q_OS_LINUX return getWMIC("lsblk --nodeps -no serial /dev/sda | head -n 1"); #endif return ""; } } // namespace SysInfoExt