#include <Windows.h> #include #include #include #include #include
using namespace std; #define BUFFER_SIZE 128
bool getDevcieInfo(const char* cmd, string* resultStr, string key) { char buffer[BUFFER_SIZE]; bool ret = false; char tmp[1024] = { 0 }; FILE* pipe = _popen(cmd, "r"); //打开管道,并执行命令 if (!pipe) return ret; int len = key.length(); bool isOk = false; while (!feof(pipe)) { if (fgets(buffer, BUFFER_SIZE, pipe)) { if (strncmp(key.c_str(), buffer, len) == 0) // 能够正确获取信息 { isOk = true; continue; } if (isOk == false || strcmp("\r\n", buffer) == 0) //去掉windows无用的空行 { continue; } ret = true; *resultStr = string(buffer); cout << "cmd is " << cmd << "\n ret is " << resultStr << endl; } } _pclose(pipe); // 关闭管道 return ret; }
string getDeviceFingerPrint() { string uuid; // 主板UUID存在,就使用主板UUID生成机器指纹 if (getDevcieInfo("wmic csproduct get UUID", &uuid, "UUID")) { cout << "UUID is " << uuid; return uuid; } else { cout << "UUID not exist"; } return string("UUID not exist"); }
void test() { string deviceFingerPrint = getDeviceFingerPrint(); cout << deviceFingerPrint << endl; }
int main(int args, char* argv[]) { test(); system("pause"); return 0; }