获取CPU型号和序列号
主要參考文章:關于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, __cpuidexhttps://docs.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex?view=msvc-160之前用的獲取CPU型號或者序列號的代碼,都是網上找的包含了匯編指令的代碼。64位程序里面顯然沒辦法用。參考了上面兩篇文章,整理了兩個函數做備忘。微軟那個下面有例子,但是用到的std::array,這個是c++11的,有時候用vs2008,會編譯不過。我稍微改動了下。
代碼沒有優化和美化,如有需要請自行整理。
獲取CPU型號
#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 stringchar 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 0x00000001if (nIds_ >= 1){f_1_ECX_ = data_[1].interArray[2];f_1_EDX_ = data_[1].interArray[3];}// load bitset with flags for function 0x00000007if (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 0x80000001if (nExIds_ >= 0x80000001){f_81_ECX_ = extdata_[1].interArray[2];f_81_EDX_ = extdata_[1].interArray[3];}// Interpret CPU brand string if reportedif (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序列號
#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 stringchar 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位程序中測試了,與之前使用匯編的那種代碼獲取到的結果是一樣的。
總結
以上是生活随笔為你收集整理的获取CPU型号和序列号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高中计算机公式,求高中数学公式大全,高二
- 下一篇: 计算机磁盘序列号是唯一的吗,磁盘id和硬