我需要CPU的PSN。 我写了像
这样的代码int info[4] = { -1 };
__cpuid(info, 1);
int family = info[0] & 0xf00;
int features = info[3] & 0xf000;
std::stringstream psn_id;
我如何获得处理器序列号? 谁能帮帮我吗。 谢谢。
答案 0 :(得分:0)
什么操作系统?如果使用Windows而不是CPU序列,则可以使用系统调用GetVolumeInformation()
使用与文件系统关联的卷序列号来唯一标识pcDWORD serial;
::GetVolumeInformation(_T("C:\\"), NULL, 0, &serial, NULL, NULL, NULL, 0);
然后使用serial
作为您的唯一标识符。
答案 1 :(得分:-1)
#include <stdio.h>
void getPSN(char *PSN)
{int varEAX, varEBX, varECX, varEDX;
char str[9];
//%eax=1 gives most significant 32 bits in eax
__asm__ __volatile__ ("cpuid": "=a" (varEAX), "=b" (varEBX), "=c" (varECX), "=d" (varEDX) : "a" (1));
sprintf(str, "%08X", varEAX); //i.e. XXXX-XXXX-xxxx-xxxx-xxxx-xxxx
sprintf(PSN, "%C%C%C%C-%C%C%C%C", str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
//%eax=3 gives least significant 64 bits in edx and ecx [if PN is enabled]
__asm__ __volatile__ ("cpuid": "=a" (varEAX), "=b" (varEBX), "=c" (varECX), "=d" (varEDX) : "a" (3));
sprintf(str, "%08X", varEDX); //i.e. xxxx-xxxx-XXXX-XXXX-xxxx-xxxx
sprintf(PSN, "%s-%C%C%C%C-%C%C%C%C", PSN, str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
sprintf(str, "%08X", varECX); //i.e. xxxx-xxxx-xxxx-xxxx-XXXX-XXXX
sprintf(PSN, "%s-%C%C%C%C-%C%C%C%C", PSN, str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
}
int main()
{
char PSN[30]; //24 Hex digits, 5 '-' separators, and a '\0'
getPSN(PSN);
printf("%s\n", PSN); //compare with: lshw | grep serial:
return 0;
}