日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

获取CPU型号和序列号

發布時間:2023/12/8 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 获取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型号和序列号的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。