Linux C++(QT) 下获取 CPU 序列号(processor serial number)
亲测(Intel core-i)可用,不过需要注意的是这个序列号并非唯一。如果需要唯一的编码可以或者硬盘的序列号。
#include <cpuid.h>
#include <sstream>
std::string GetCPUId()
{
std::string strCPUId;
unsigned int level = 1;
unsigned eax = 3 /* processor serial number */, ebx = 0, ecx = 0, edx = 0;
__get_cpuid(level, &eax, &ebx, &ecx, &edx);
// byte swap
int first = ((eax >> 24) & 0xff) | ((eax << 8) & 0xff0000) | ((eax >> 8) & 0xff00) | ((eax << 24) & 0xff000000);
int last = ((edx >> 24) & 0xff) | ((edx << 8) & 0xff0000) | ((edx >> 8) & 0xff00) | ((edx << 24) & 0xff000000);
// tranfer to string
std::stringstream ss;
ss << std::hex << first;
ss << std::hex << last;
ss >> strCPUId;
return strCPUId;
}