【注】本文是参考百度百科对CPUID指令的介绍而简要写的一个总结;但并不一定所有CPU都能支持获取CPUID;关于CPU序列号的表示可能不同人有不同写法。还有关于CPU缓存的信息的获取,需要根据返回的信息去查一个对照表,看起来不够简洁明了,所以关于CPU缓存信息获取的代码我就不写进去了。
相关代码如下:
code_getCpuId
1 // GetCPUID.cpp : Defines the entry point for the console application.
2 //
3
4 #include "stdafx.h"
5 #include <string.h>
6
7 typedef unsigned long DWORD;
8
9 #define CI_VENDOR 0x01
10 #define CI_BRAND 0x02
11 #define CI_SERIAL 0x04
12 #define CI_ALL (CI_VENDOR|CI_BRAND|CI_SERIAL)
13
14 typedef struct _CPUINFO
15 {
16 unsigned int nMask;
17 char szVendor[16];
18 char szBrand[52];
19 char szSerial[32];
20 unsigned char Serial[12];
21
22 } CPUINFO, *LPCPUINFO;
23
24 //获取CPUINFO
25 void GetCPUInfo(LPCPUINFO lpInfo)
26 {
27 DWORD _eax, _ebx, _ecx, _edx;
28 DWORD i, j;
29
30 if(lpInfo->nMask & CI_VENDOR) //vendor
31 {
32 __asm
33 {
34 mov eax, 00h;
35 cpuid;
36 mov _ebx, ebx;
37 mov _ecx, ecx;
38 mov _edx, edx;
39 }
40 memcpy(lpInfo->szVendor, &_ebx, 4);
41 memcpy(lpInfo->szVendor + 4, &_edx, 4);
42 memcpy(lpInfo->szVendor + 8, &_ecx, 4);
43 lpInfo->szVendor[12] = 0;
44 }
45
46 if(lpInfo->nMask & CI_BRAND) //brand
47 {
48 for(i=0x80000002, j=0; i<0x80000005; ++i, ++j)
49 {
50 __asm
51 {
52 mov eax, i;
53 cpuid;
54 mov _eax, eax;
55 mov _ebx, ebx;
56 mov _ecx, ecx;
57 mov _edx, edx;
58 }
59 memcpy(lpInfo->szBrand + j*16, &_eax, 4);
60 memcpy(lpInfo->szBrand + j*16+4, &_ebx, 4);
61 memcpy(lpInfo->szBrand + j*16+8, &_ecx, 4);
62 memcpy(lpInfo->szBrand + j*16+12, &_edx, 4);
63 }
64 lpInfo->szBrand[48] = 0;
65 }
66
67 if(lpInfo->nMask & CI_SERIAL) //serial
68 {
69 __asm
70 {
71 mov eax, 01h;
72 xor edx, edx;
73 cpuid;
74 mov _eax, eax;
75 mov _edx, edx;
76 }
77 if(_edx & (1<<18)) //
78 {
79 memcpy(lpInfo->Serial+8, &_eax, 4);
80 __asm
81 {
82 mov eax, 03h;
83 xor ecx, ecx;
84 xor edx, edx;
85 cpuid;
86 mov _ecx, ecx;
87 mov _edx, edx;
88 }
89 memcpy(lpInfo->Serial, &_ecx, 4);
90 memcpy(lpInfo->Serial+4, &_edx, 4);
91 sprintf(lpInfo->szSerial,
92 "%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X",
93 lpInfo->Serial[0], lpInfo->Serial[1],
94 lpInfo->Serial[2], lpInfo->Serial[3],
95 lpInfo->Serial[4], lpInfo->Serial[5],
96 lpInfo->Serial[6], lpInfo->Serial[7],
97 lpInfo->Serial[8], lpInfo->Serial[9],
98 lpInfo->Serial[10], lpInfo->Serial[11]);
99 }
100 else
101 {
102 memset(lpInfo->Serial, 0, 12);
103 strcpy(lpInfo->szSerial, "null");
104 }
105 }
106 }
107
108 int main(int argc, char* argv[])
109 {
110 CPUINFO info;
111 info.nMask = CI_ALL;
112 GetCPUInfo(&info);
113 printf("Vendor: %s\n", info.szVendor);
114 printf("Brand: %s\n", info.szBrand);
115 printf("Serial: %s\n", info.szSerial);
116 return 0;
117 }
2 //
3
4 #include "stdafx.h"
5 #include <string.h>
6
7 typedef unsigned long DWORD;
8
9 #define CI_VENDOR 0x01
10 #define CI_BRAND 0x02
11 #define CI_SERIAL 0x04
12 #define CI_ALL (CI_VENDOR|CI_BRAND|CI_SERIAL)
13
14 typedef struct _CPUINFO
15 {
16 unsigned int nMask;
17 char szVendor[16];
18 char szBrand[52];
19 char szSerial[32];
20 unsigned char Serial[12];
21
22 } CPUINFO, *LPCPUINFO;
23
24 //获取CPUINFO
25 void GetCPUInfo(LPCPUINFO lpInfo)
26 {
27 DWORD _eax, _ebx, _ecx, _edx;
28 DWORD i, j;
29
30 if(lpInfo->nMask & CI_VENDOR) //vendor
31 {
32 __asm
33 {
34 mov eax, 00h;
35 cpuid;
36 mov _ebx, ebx;
37 mov _ecx, ecx;
38 mov _edx, edx;
39 }
40 memcpy(lpInfo->szVendor, &_ebx, 4);
41 memcpy(lpInfo->szVendor + 4, &_edx, 4);
42 memcpy(lpInfo->szVendor + 8, &_ecx, 4);
43 lpInfo->szVendor[12] = 0;
44 }
45
46 if(lpInfo->nMask & CI_BRAND) //brand
47 {
48 for(i=0x80000002, j=0; i<0x80000005; ++i, ++j)
49 {
50 __asm
51 {
52 mov eax, i;
53 cpuid;
54 mov _eax, eax;
55 mov _ebx, ebx;
56 mov _ecx, ecx;
57 mov _edx, edx;
58 }
59 memcpy(lpInfo->szBrand + j*16, &_eax, 4);
60 memcpy(lpInfo->szBrand + j*16+4, &_ebx, 4);
61 memcpy(lpInfo->szBrand + j*16+8, &_ecx, 4);
62 memcpy(lpInfo->szBrand + j*16+12, &_edx, 4);
63 }
64 lpInfo->szBrand[48] = 0;
65 }
66
67 if(lpInfo->nMask & CI_SERIAL) //serial
68 {
69 __asm
70 {
71 mov eax, 01h;
72 xor edx, edx;
73 cpuid;
74 mov _eax, eax;
75 mov _edx, edx;
76 }
77 if(_edx & (1<<18)) //
78 {
79 memcpy(lpInfo->Serial+8, &_eax, 4);
80 __asm
81 {
82 mov eax, 03h;
83 xor ecx, ecx;
84 xor edx, edx;
85 cpuid;
86 mov _ecx, ecx;
87 mov _edx, edx;
88 }
89 memcpy(lpInfo->Serial, &_ecx, 4);
90 memcpy(lpInfo->Serial+4, &_edx, 4);
91 sprintf(lpInfo->szSerial,
92 "%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X",
93 lpInfo->Serial[0], lpInfo->Serial[1],
94 lpInfo->Serial[2], lpInfo->Serial[3],
95 lpInfo->Serial[4], lpInfo->Serial[5],
96 lpInfo->Serial[6], lpInfo->Serial[7],
97 lpInfo->Serial[8], lpInfo->Serial[9],
98 lpInfo->Serial[10], lpInfo->Serial[11]);
99 }
100 else
101 {
102 memset(lpInfo->Serial, 0, 12);
103 strcpy(lpInfo->szSerial, "null");
104 }
105 }
106 }
107
108 int main(int argc, char* argv[])
109 {
110 CPUINFO info;
111 info.nMask = CI_ALL;
112 GetCPUInfo(&info);
113 printf("Vendor: %s\n", info.szVendor);
114 printf("Brand: %s\n", info.szBrand);
115 printf("Serial: %s\n", info.szSerial);
116 return 0;
117 }
输出如下:
Vendor: GenuineIntel
Brand: Intel(R) Core(TM) i5 CPU M 430 @ 2.27GHz
Serial: null
参考文档:
来源:https://www.cnblogs.com/hoodlum1980/archive/2010/08/15/1799852.html