bool getDevcieInfo(char* cmd, list<string> &resultList) {
char buffer[BUFFER_SIZE];
bool ret = false;
FILE* pipe = _popen(cmd, "r"); //打开管道,并执行命令
if (!pipe)
return ret;
char *name[20] = { "UUID", "ProcessorId", "SerialNumber" };
int len0 = strlen(name[0]), len1 = strlen(name[1]), len2 = strlen(name[2]);
bool isOk = false;
while (!feof(pipe)) {
if (fgets(buffer, BUFFER_SIZE, pipe)) {
if (strncmp(name[0], buffer, len0) == 0
|| strncmp(name[1], buffer, len1) == 0
|| strncmp(name[2], buffer, len2) == 0) // 能够正确获取信息
{
isOk = true;
continue;
}
if (isOk == false
|| strcmp("\r\n", buffer) == 0) //去掉windows无用的空行
{
continue;
}
ret = true;
resultList.push_back(string(buffer));
}
}
_pclose(pipe); // 关闭管道
return ret;
}
string getDeviceFingerPrint() {
list<string> strList;
list<string>::iterator it;
hash<string> str_hash;
size_t num;
char tmp[11] = { 0 };
if (getDevcieInfo("wmic csproduct get UUID", strList)) {
num = str_hash(*strList.begin());
sprintf(tmp, "%u", num);
return string(tmp);
}
string otherStr("");
strList.clear();
if (getDevcieInfo("wmic cpu get processorid", strList)) {
otherStr.append(*strList.begin());
}
strList.clear();
if (getDevcieInfo("wmic bios get serialnumber", strList)) {
otherStr.append(*strList.begin());
}
strList.clear();
if (getDevcieInfo("wmic diskdrive get serialnumber", strList)) {
string allDiskNum("");
for (it = strList.begin(); it != strList.end(); it++) {
allDiskNum.append(*it);
}
otherStr.append(*strList.begin());
}
cout << str_hash(otherStr) << endl;
num = str_hash(otherStr);
sprintf(tmp, "%u", num);
return string(tmp);
}