__cpuidex Read CPU serial number
Recent research on the use of __cpuidex, the query to the relevant examples are more complex, so I simplified a part of the content, a simple example, to obtain only the CPU serial number, the final result and through the WMIC command query results consistent.
code Example
#include <iostream> #include <array> #include <intrin.h> #include <vector> #include <cstring
> Using Std::cout; int main () {std::array<int, 4> cpui; #include <array>//calling __cpuid with 0x0 as the function_id argument//Gets the number of the Highe St valid function ID.
What is a function ID. __cpuid (Cpui.data (), 0x0);
Cpui[0] = "funcition_id maximum" int nids_ = cpui[0]; Std::vector<std::array<int, 4>> Data_;
Saves the values for all cpui that are traversed for (int i=0;i<=nids_;++i) {__cpuidex (Cpui.data (), I, 0);
Data_.push_back (CPUI); The value of//reinterpret_cast<int*> (vendor)//*reinterpret_cast<int*> (vendor)//Index 0 0+4 0+8 constitutes the name of the CPU chip CH
Ar vendor[0x20] = {0};
*reinterpret_cast<int*> (Vendor) = data_[0][1];
*reinterpret_cast<int*> (vendor + 4) = data_[0][3]; *reinterpret_cast<int*> (vendor + 8) = data_[0][2]; Vendor= "Genuineintel" std::sTring vendor_ = Vendor;
BOOL Isintel_ = false;
BOOL ISAMD = false; if ("genuineintel" = = vendor_) {Isintel_ = true; Vendor is Intel} else if ("AUTHENTICAMD" ==vendor_) {isamd = true;
Vendor is AMD} char vendor_serialnumber[0x14] = {0};
sprintf_s (Vendor_serialnumber, sizeof (Vendor_serialnumber), "%08x%08x", Data_[1][3], data_[1][0]); Compare the results obtained with the "WMIC CPU get Processorid" and the results should be consistent.
Vendor_serialnumber = "Bfebfbff000406e3";
Std::cout << "Processorid is [" << vendor_serialnumber << "]" << Std::endl;
return 0; }
Microsoft specific
Generates the CPUID instruction that are available on x86 and x64. This instruction queries the processor for information about supported features and the CPU type.
Syntax
void __cpuid (
int cpuinfo[4],
int function_id
);
void __cpuidex (
int cpuinfo[4],
int function_id,
int subfunction_id
);
Parameters
[out] CpuInfo
An array of four integers this contains the information returned in EAX, EBX, ECX, and EDX about supported features of the Cpu.
[In] function_id
A code that specifies the "information to retrieve", passed in EAX.
[In] subfunction_id
An additional code this specifies information to retrieve, passed in ECX.
Requirements
Header file <tintrin.h>
Reference Documents
"MSDN" __cpuid, __cpuidex
other
2017-3-14 10:58:23, update function description. Angle brackets need to escape the "<+ semicolon" ">+ semicolon".
2017-2-14 14:39:04, Shanghai Baoshan.