SysInfoExt.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "SysInfoExt.h"
  2. #include <QProcess>
  3. #ifdef Q_OS_LINUX
  4. # include <QCryptographicHash>
  5. # include <cpuid.h>
  6. #endif
  7. namespace SysInfoExt {
  8. #ifdef Q_OS_LINUX
  9. struct CPUInfo
  10. {
  11. QString cpuID;
  12. QString devID;
  13. };
  14. QByteArray GetMD5Hash(QByteArray buff)
  15. {
  16. QCryptographicHash hash(QCryptographicHash::Md5);
  17. hash.addData(buff);
  18. return hash.result().toHex();
  19. }
  20. QString GetCpuId()
  21. {
  22. QString cpuid = "";
  23. unsigned int dwBuf[4];
  24. int InfoType = 1;
  25. __cpuid(InfoType, dwBuf[0], dwBuf[1], dwBuf[2], dwBuf[3]);
  26. __cpuid_count(InfoType, 0, dwBuf[0], dwBuf[1], dwBuf[2], dwBuf[3]);
  27. cpuid = QString::number(dwBuf[3], 16).toUpper();
  28. cpuid = cpuid + QString::number(dwBuf[0], 16).toUpper();
  29. return cpuid;
  30. }
  31. QString GetMeCode()
  32. {
  33. QString strMe = GetCpuId();
  34. QByteArray ba = strMe.toLatin1();
  35. QString strTemp = ba.toBase64();
  36. return strTemp;
  37. }
  38. CPUInfo getCPUInfo()
  39. {
  40. CPUInfo cpuinfo;
  41. QByteArray hwid = GetMeCode().toUtf8().toHex();
  42. QByteArray algo = {};
  43. uint8_t hwid_cnt = hwid.length() / 2;
  44. for (int i = 0; i < hwid_cnt; i++) {
  45. QByteArray sum0 = GetMD5Hash(hwid.mid(i * 2, 2)).mid(hwid_cnt, 2);
  46. QByteArray csum = GetMD5Hash(sum0).mid(i, 2).toUpper();
  47. algo.append(csum);
  48. }
  49. const QString pcuid = { QString("%0").arg(GetMD5Hash(algo).data()) };
  50. cpuinfo.cpuID.append(QString("%0").arg(hwid.data()).toUpper());
  51. cpuinfo.devID.append(QString("%0").arg(pcuid).toUpper());
  52. return cpuinfo;
  53. }
  54. #endif
  55. QString fixInfo(const QString &str)
  56. {
  57. QString temp = str;
  58. // \r \n 去除
  59. temp.remove("\r");
  60. temp.remove("\n");
  61. // 去除多余的空格
  62. QStringList list;
  63. for (auto s : temp.split(" ")) {
  64. if (!s.trimmed().isEmpty()) {
  65. list.append(s);
  66. }
  67. }
  68. return list.join(" ");
  69. }
  70. /**
  71. * @brief 通用信息获取
  72. * @param cmd
  73. * @return
  74. */
  75. QString getWMIC(const QString &cmd)
  76. {
  77. QProcess p;
  78. p.start(cmd);
  79. p.waitForFinished();
  80. QString res = QString::fromLocal8Bit(p.readAllStandardOutput());
  81. return fixInfo(res);
  82. }
  83. /**
  84. * 功能: 获取CPU的名称
  85. */
  86. QString getCpuName()
  87. {
  88. #ifdef Q_OS_WIN
  89. return getWMIC("wmic cpu get name");
  90. #endif
  91. #ifdef Q_OS_LINUX
  92. return getWMIC("grep -m 1 'model name' /proc/cpuinfo");
  93. #endif
  94. return "";
  95. }
  96. /**
  97. * 功能: 获取CPU的ID
  98. */
  99. QString getCpuId()
  100. {
  101. #ifdef Q_OS_WIN
  102. return getWMIC("wmic cpu get processorid");
  103. #endif
  104. return "";
  105. }
  106. /**
  107. * 功能: 查询主板序号
  108. */
  109. QString getBaseboardNum()
  110. {
  111. #ifdef Q_OS_WIN
  112. return getWMIC("wmic baseboard get serialnumber");
  113. #endif
  114. return "";
  115. }
  116. /**
  117. * 功能: 查询BIOS序号
  118. */
  119. QString getBiosNum()
  120. {
  121. #ifdef Q_OS_WIN
  122. return getWMIC("wmic bios get serialnumber");
  123. #endif
  124. return "";
  125. }
  126. /**
  127. * 功能: 查询硬盘序号
  128. */
  129. QString getDiskNum()
  130. {
  131. #ifdef Q_OS_WIN
  132. return getWMIC("wmic diskdrive where index=0 get serialnumber");
  133. #endif
  134. #ifdef Q_OS_LINUX
  135. return getWMIC("lsblk --nodeps -no serial /dev/sda | head -n 1");
  136. #endif
  137. return "";
  138. }
  139. } // namespace SysInfoExt