hardwaremanage.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "hardwaremanage.h"
  2. #include <QtNetwork>
  3. #include <QNetworkAccessManager>
  4. HardWareManage::HardWareManage()
  5. {
  6. }
  7. QString HardWareManage::getMachineCode()
  8. {
  9. QString mcode = exeScript();
  10. QString value = "";
  11. if(!mcode.contains("CpuID"))
  12. {
  13. mcode = getCpuID() + " " + getTimeStamp();
  14. }
  15. if(!mcode.isEmpty())
  16. {
  17. QByteArray ba;
  18. ba = QCryptographicHash::hash ( mcode.toAscii(), QCryptographicHash::Md5 );
  19. value.append(ba.toHex());
  20. }
  21. return value;
  22. }
  23. QString HardWareManage::exeScript()
  24. {
  25. QString fileName = ":/mcode.vbs";
  26. QString scriptPath = QCoreApplication::applicationDirPath()+"/../Temp/mcode.vbs";
  27. QString mcodePath = QCoreApplication::applicationDirPath()+"/../Temp/mcode.txt";
  28. QString batPath = QCoreApplication::applicationDirPath()+"/../Temp/mcode.bat";
  29. //读取存放在资源文件的脚本文件
  30. QFile fileIn(fileName);
  31. fileIn.open(QIODevice::ReadOnly);
  32. QTextStream in(&fileIn);
  33. QString script = in.readAll();
  34. fileIn.close();
  35. //写脚本文件
  36. QFile fileOut(scriptPath);
  37. fileOut.open(QIODevice::WriteOnly);
  38. QTextStream out(&fileOut);
  39. out<<script;
  40. fileOut.close();
  41. //写批处理文件
  42. QFile fileOut2(batPath);
  43. fileOut2.open(QIODevice::WriteOnly);
  44. QTextStream out2(&fileOut2);
  45. out2<<"cscript.exe \"" + scriptPath + "\" //nologo >> " + "\"" + mcodePath + "\"";
  46. fileOut2.close();
  47. //执行脚本文件
  48. QProcess process;
  49. process.start(batPath);
  50. process.waitForFinished();
  51. //读取结果
  52. QFile fileIn2(mcodePath);
  53. fileIn2.open(QIODevice::ReadOnly);
  54. QTextStream in2(&fileIn2);
  55. QString mcode = in2.readAll();
  56. fileIn2.close();
  57. //删除过程文件
  58. QFile::remove(scriptPath);
  59. QFile::remove(mcodePath);
  60. QFile::remove(batPath);
  61. return mcode;
  62. }
  63. QString HardWareManage::getCpuID()
  64. {
  65. unsigned long s1,s2,s3,s4;
  66. char string[128];
  67. char szCpuId[1024];
  68. char p1[128], p2[128];
  69. unsigned int eax = 0;
  70. unsigned int ebx,ecx,edx;
  71. QString strCPUID;
  72. #if 1
  73. asm volatile
  74. (
  75. "cpuid"
  76. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  77. : "0"(0)
  78. );
  79. snprintf(szCpuId, 5, "%s", (char *)&ebx);
  80. snprintf(szCpuId+4, 5, "%s", (char *)&edx);
  81. snprintf(szCpuId+8, 5, "%s", (char *)&ecx);
  82. #endif
  83. asm volatile
  84. (
  85. "movl $0x01 , %%eax ; \n\t"
  86. "xorl %%edx , %%edx ;\n\t"
  87. "cpuid ;\n\t"
  88. "movl %%edx ,%0 ;\n\t"
  89. "movl %%eax ,%1 ; \n\t"
  90. :"=m"(s1),"=m"(s2)
  91. );
  92. char cpuID[20];
  93. memset(cpuID,0,20);
  94. sprintf((char *)p1, "%08X%08X", s1, s2);
  95. snprintf(szCpuId+12, 20, "%s", (char *)p1);
  96. memcpy(cpuID,p1,17);
  97. strCPUID = strCPUID.fromAscii(cpuID);
  98. asm volatile
  99. (
  100. "movl $0x03,%%eax ;\n\t"
  101. "xorl %%ecx,%%ecx ;\n\t"
  102. "xorl %%edx,%%edx ;\n\t"
  103. "cpuid ;\n\t"
  104. "movl %%edx,%0 ;\n\t"
  105. "movl %%ecx,%1 ;\n\t"
  106. :"=m"(s3),"=m"(s4)
  107. );
  108. sprintf((char *)p2, "%08X-%08X\n", s3, s4);
  109. snprintf(szCpuId+31, 19, "%s", (char *)p2);
  110. return strCPUID;
  111. }
  112. QString HardWareManage::getMAC()
  113. {
  114. QList<QNetworkInterface> networkInterface = QNetworkInterface::allInterfaces();
  115. QString mac = "";
  116. if(networkInterface.size()>0)
  117. {
  118. mac = networkInterface.at(0).hardwareAddress();
  119. }
  120. return mac;
  121. }
  122. QString HardWareManage::getTimeStamp()
  123. {
  124. QDateTime datetime1 = QDateTime::currentDateTime();//获取系统现在的时间
  125. QTime time2 = QTime::currentTime();
  126. QString str = datetime1.toString("yyyy-MM-dd hh:mm:ss zzz "); //设置显示格式
  127. qsrand(time2.msec()+time2.second()*10000);
  128. for(int i=0;i<6;i++)
  129. {
  130. int v = qrand()%9;
  131. str += QString::number(v);
  132. }
  133. return str;
  134. }