前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >获取CPU型号和序列号

获取CPU型号和序列号

作者头像
大菊观
发布2021-09-27 15:07:38
4.5K0
发布2021-09-27 15:07:38
举报

主要参考文章:关于CPU序列号的问题,以及如何获取×64下CPU的ProcessorID_fudong071234的博客-CSDN博客前几天经过查资料,得到网络上获取CPU序列号的方法是错误的,首先我找到了一篇论文,这篇论文里面是这么说的:这篇论文是错误的。这篇是错误的这篇是错误的!!!!!!!!!2、CPU序列号CPU序列号是一个建立在处理器内部的、唯一的、不能被修改的编号。它由96位数字组成。高32位是CPUID,用来识别CPU类型。低64位每个处理器都不同,唯一地代表了该处理器。CPU号可以用来识别

https://blog.csdn.net/fudong071234/article/details/49612083__cpuid, __cpuidex | Microsoft DocsLearn more about: __cpuid, __cpuidex

https://docs.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex?view=msvc-160之前用的获取CPU型号或者序列号的代码,都是网上找的包含了汇编指令的代码。64位程序里面显然没办法用。参考了上面两篇文章,整理了两个函数做备忘。微软那个下面有例子,但是用到的std::array,这个是c++11的,有时候用vs2008,会编译不过。我稍微改动了下。

代码没有优化和美化,如有需要请自行整理。

获取CPU型号

代码语言:javascript
复制
#include <intrin.h>
#include <vector>
#include <bitset>
#include <string>

using namespace std;

string GetCpuBrand()
{
	typedef struct tagCpui
	{
		tagCpui()
		{
			interArray[0] = 0;
			interArray[1] = 0;
			interArray[2] = 0;
			interArray[3] = 0;
		};
		tagCpui& operator=(const tagCpui& value)
		{
			interArray[0] = value.interArray[0];
			interArray[1] = value.interArray[1];
			interArray[2] = value.interArray[2];
			interArray[3] = value.interArray[3];

			return *this;
		};

		int interArray[4];
	}Cpui;

	int nIds_;
	int nExIds_;
	std::string vendor_;
	std::string brand_;
	bool isIntel_;
	bool isAMD_;
	std::bitset<32> f_1_ECX_;
	std::bitset<32> f_1_EDX_;
	std::bitset<32> f_7_EBX_;
	std::bitset<32> f_7_ECX_;
	std::bitset<32> f_81_ECX_;
	std::bitset<32> f_81_EDX_;
	std::vector<Cpui> data_;
	std::vector<Cpui> extdata_;
	Cpui cpui;
	// Calling __cpuid with 0x0 as the function_id argument
	// gets the number of the highest valid function ID.
	__cpuid(cpui.interArray, 0);
	nIds_ = cpui.interArray[0];

	for (int i = 0; i <= nIds_; ++i)
	{
		__cpuidex(cpui.interArray, i, 0);
		data_.push_back(cpui);
	}

	// Capture vendor string
	char vendor[0x20];
	memset(vendor, 0, sizeof(vendor));
	*reinterpret_cast<int*>(vendor) = data_[0].interArray[1];
	*reinterpret_cast<int*>(vendor + 4) = data_[0].interArray[3];
	*reinterpret_cast<int*>(vendor + 8) = data_[0].interArray[2];
	vendor_ = vendor;
	if (vendor_ == "GenuineIntel")
	{
		isIntel_ = true;
	}
	else if (vendor_ == "AuthenticAMD")
	{
		isAMD_ = true;
	}

	// load bitset with flags for function 0x00000001
	if (nIds_ >= 1)
	{
		f_1_ECX_ = data_[1].interArray[2];
		f_1_EDX_ = data_[1].interArray[3];
	}

	// load bitset with flags for function 0x00000007
	if (nIds_ >= 7)
	{
		f_7_EBX_ = data_[7].interArray[1];
		f_7_ECX_ = data_[7].interArray[2];
	}

	// Calling __cpuid with 0x80000000 as the function_id argument
	// gets the number of the highest valid extended ID.
	__cpuid(cpui.interArray, 0x80000000);
	nExIds_ = cpui.interArray[0];

	char brand[0x40];
	memset(brand, 0, sizeof(brand));

	for (int i = 0x80000000; i <= nExIds_; ++i)
	{
		__cpuidex(cpui.interArray, i, 0);
		extdata_.push_back(cpui);
	}

	// load bitset with flags for function 0x80000001
	if (nExIds_ >= 0x80000001)
	{
		f_81_ECX_ = extdata_[1].interArray[2];
		f_81_EDX_ = extdata_[1].interArray[3];
	}

	// Interpret CPU brand string if reported
	if (nExIds_ >= 0x80000004)
	{
		memcpy(brand, extdata_[2].interArray, sizeof(cpui.interArray));
		memcpy(brand + 16, extdata_[3].interArray, sizeof(cpui.interArray));
		memcpy(brand + 32, extdata_[4].interArray, sizeof(cpui.interArray));
		brand_ = brand;
	}

	return brand_;
}

获取CPU序列号

代码语言:javascript
复制
#include <intrin.h>
#include <string>
#include <vector>
#include <bitset>

string GetCpuIndex()
{
	typedef struct tagCpui
	{
		tagCpui()
		{
			interArray[0] = 0;
			interArray[1] = 0;
			interArray[2] = 0;
			interArray[3] = 0;
		};
		tagCpui& operator=(const tagCpui& value)
		{
			interArray[0] = value.interArray[0];
			interArray[1] = value.interArray[1];
			interArray[2] = value.interArray[2];
			interArray[3] = value.interArray[3];

			return *this;
		};

		int interArray[4];
	}Cpui;

	//string strCpuType = GetCPUType();
	int nIds_;
	int nExIds_;
	std::string vendor_;
	std::string brand_;
	bool isIntel_;
	bool isAMD_;
	std::bitset<32> f_1_ECX_;
	std::bitset<32> f_1_EDX_;
	std::bitset<32> f_7_EBX_;
	std::bitset<32> f_7_ECX_;
	std::bitset<32> f_81_ECX_;
	std::bitset<32> f_81_EDX_;
	std::vector<Cpui> data_;
	std::vector<Cpui> extdata_;

	Cpui cpui;
	// Calling __cpuid with 0x0 as the function_id argument
	// gets the number of the highest valid function ID.
	__cpuid(cpui.interArray, 0);
	nIds_ = cpui.interArray[0];

	for (int i = 0; i <= nIds_; ++i)
	{
		__cpuidex(cpui.interArray, i, 0);
		data_.push_back(cpui);
	}

	// Capture vendor string
	char vendor[0x20];
	memset(vendor, 0, sizeof(vendor));
	*reinterpret_cast<int*>(vendor) = data_[0].interArray[1];
	*reinterpret_cast<int*>(vendor + 4) = data_[0].interArray[3];
	*reinterpret_cast<int*>(vendor + 8) = data_[0].interArray[2];
	vendor_ = vendor;
	if (vendor_ == "GenuineIntel")
	{
		isIntel_ = true;
	}
	else if (vendor_ == "AuthenticAMD")
	{
		isAMD_ = true;
	}

	char vendor_serialnumber[0x14] = { 0 };
	sprintf_s(vendor_serialnumber, sizeof(vendor_serialnumber), "%08X%08X", data_[1].interArray[3], data_[1].interArray[0]);
	string strRet = vendor_serialnumber;
	return strRet;
}

上述两个函数是在微软那个例子基础上改动的,我在32位程序中测试了,与之前使用汇编的那种代码获取到的结果是一样的。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-09-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档