#include "hardwaremanage.h" #include #include HardWareManage::HardWareManage() { } QString HardWareManage::getMachineCode() { QString mcode = exeScript(); QString value = ""; if(!mcode.contains("CpuID")) { mcode = getCpuID() + " " + getTimeStamp(); } if(!mcode.isEmpty()) { QByteArray ba; ba = QCryptographicHash::hash ( mcode.toAscii(), QCryptographicHash::Md5 ); value.append(ba.toHex()); } return value; } QString HardWareManage::exeScript() { QString fileName = ":/mcode.vbs"; QString scriptPath = QCoreApplication::applicationDirPath()+"/../Temp/mcode.vbs"; QString mcodePath = QCoreApplication::applicationDirPath()+"/../Temp/mcode.txt"; QString batPath = QCoreApplication::applicationDirPath()+"/../Temp/mcode.bat"; //读取存放在资源文件的脚本文件 QFile fileIn(fileName); fileIn.open(QIODevice::ReadOnly); QTextStream in(&fileIn); QString script = in.readAll(); fileIn.close(); //写脚本文件 QFile fileOut(scriptPath); fileOut.open(QIODevice::WriteOnly); QTextStream out(&fileOut); out<> " + "\"" + mcodePath + "\""; fileOut2.close(); //执行脚本文件 QProcess process; process.start(batPath); process.waitForFinished(); //读取结果 QFile fileIn2(mcodePath); fileIn2.open(QIODevice::ReadOnly); QTextStream in2(&fileIn2); QString mcode = in2.readAll(); fileIn2.close(); //删除过程文件 QFile::remove(scriptPath); QFile::remove(mcodePath); QFile::remove(batPath); return mcode; } QString HardWareManage::getCpuID() { unsigned long s1,s2,s3,s4; char string[128]; char szCpuId[1024]; char p1[128], p2[128]; unsigned int eax = 0; unsigned int ebx,ecx,edx; QString strCPUID; #if 1 asm volatile ( "cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "0"(0) ); snprintf(szCpuId, 5, "%s", (char *)&ebx); snprintf(szCpuId+4, 5, "%s", (char *)&edx); snprintf(szCpuId+8, 5, "%s", (char *)&ecx); #endif asm volatile ( "movl $0x01 , %%eax ; \n\t" "xorl %%edx , %%edx ;\n\t" "cpuid ;\n\t" "movl %%edx ,%0 ;\n\t" "movl %%eax ,%1 ; \n\t" :"=m"(s1),"=m"(s2) ); char cpuID[20]; memset(cpuID,0,20); sprintf((char *)p1, "%08X%08X", s1, s2); snprintf(szCpuId+12, 20, "%s", (char *)p1); memcpy(cpuID,p1,17); strCPUID = strCPUID.fromAscii(cpuID); asm volatile ( "movl $0x03,%%eax ;\n\t" "xorl %%ecx,%%ecx ;\n\t" "xorl %%edx,%%edx ;\n\t" "cpuid ;\n\t" "movl %%edx,%0 ;\n\t" "movl %%ecx,%1 ;\n\t" :"=m"(s3),"=m"(s4) ); sprintf((char *)p2, "%08X-%08X\n", s3, s4); snprintf(szCpuId+31, 19, "%s", (char *)p2); return strCPUID; } QString HardWareManage::getMAC() { QList networkInterface = QNetworkInterface::allInterfaces(); QString mac = ""; if(networkInterface.size()>0) { mac = networkInterface.at(0).hardwareAddress(); } return mac; } QString HardWareManage::getTimeStamp() { QDateTime datetime1 = QDateTime::currentDateTime();//获取系统现在的时间 QTime time2 = QTime::currentTime(); QString str = datetime1.toString("yyyy-MM-dd hh:mm:ss zzz "); //设置显示格式 qsrand(time2.msec()+time2.second()*10000); for(int i=0;i<6;i++) { int v = qrand()%9; str += QString::number(v); } return str; }