How to obtain the serial number of a specified CPU in a multi-core or multi-CPU system
Author: skyjacker
(The post must be complete and indicate the author and source)
Http://www.cnpack.org
Cnpack iv qq group: 130970
2007-01-23
Thanks for passion, bahamut, good morning, air, skyjacker...
Without the magical power of bahamut, there is no such article.
In multiple CPUs and multiple cores, different serial numbers are randomly obtained, which makes it very difficult for us to create a registration Machine Based on the CPU serial number.
Windows 2000/XP allows you to set the affinity of processes and threads. In other words, you can control which CPU can run certain threads. This is called hard affinity. Windows provides the setprocessaffinitymask function for setting affinity. It can be used to control the acquisition of the serial number of the specified CPU.
This article consists of two parts:
1. How to obtain the serial number of the CPU.
2. How to obtain the serial number of a specified CPU or CPU core.
1. How to obtain the serial number of the CPU.
Use the cpuid command.
Before calling cpuid, eax stores the function code. After cpuid is called, eax, EBX, ECx, and EDX store various CPU features. This information is what we usually call the CPU serial number.
MoV eax, 0 // get manufacturer information
Cpuid
MoV eax, 1 // obtain the CPU serial number
Cpuid
The following three functions are available for reference:
Function newcpuid: string;
Const
Cpuinfo = 'cpu manufacturer: % s serial number: % x ';
VaR
S: array [0 .. 19] of char;
Mycpuid: integer;
Begin
Fillchar (S, 20, 0 );
ASM
Push EBX
Push ECx
Push edX
MoV eax, 0
Cpuid
MoV dword ptr s [0], EBX
MoV dword ptr s [4], EDX
MoV dword ptr s [8], ECx
MoV eax, 1
Cpuid
MoV mycpuid, EDX
Pop edX
Pop ECx
Pop EBX
End;
Result: = format (cpuinfo, [s, mycpuid]);
End;
Function getcpuid: tcpuid; assembler; register;
ASM
Push EBX {save affected register}
Push EDI
MoV EDI, eax [email ={@ resukt] {@ resukt [/Email]}
MoV eax, 1
DW $ a20f {cpuid command}
Stosd {cpuid [1]}
MoV eax, EBX
Stosd {cpuid [2]}
MoV eax, ECx
Stosd {cpuid [3]}
MoV eax, EDX
Stosd {cpuid [4]}
Pop EDI {restore registers}
Pop EBX
End;
// Obtain the serial number of the CPU:
Function getcncpuid (): string;
Const
Cpuinfo = '%. 8x-%. 8x-%. 8x-%. 8x ';
VaR
Ieax: integer;
Iebx: integer;
Iecx: integer;
Iedx: integer;
Begin
ASM
Push EBX
Push ECx
Push edX
MoV eax, 1
DW $ a20f // cpuid
MoV ieax, eax
MoV iebx, EBX
MoV iecx, ECx
MoV iedx, EDX
Pop edX
Pop ECx
Pop EBX
End;
Result: = format (cpuinfo, [ieax, iebx, iecx, iedx]);
End;
2. How to obtain the serial number of a specified CPU or CPU core.
Based on the affinity of processes and threads that can be set in windows, the setprocessaffinitymask function is used to control which CPU runs the process for obtaining the serial number. Therefore, the serial number of the specified CPU is obtained. To be compatible with a single CPU, we recommend that you always obtain the serial number of the first CPU.
Procedure setcpu (H: thandle; cpuno: integer );
// Cpuno: determines the number of CPU cores to be obtained.
VaR
Processaffinity: Cardinal;
_ Systemaffinity: Cardinal;
Begin
Getprocessaffinitymask (H, processaffinity, _ systemaffinity );
Processaffinity: = cpuno; // This sets the process to only run on CPU 0
// For CPU 1 only use 2 and for CPUs 1 & 2 Use 3
Setprocessaffinitymask (H, processaffinity)
End;
Usage:
Setcpu (getcurrentprocess, 1); // The first CPU kernel of the first CPU
Showmessage (getcncpuid );
Note: This article is due to a discussion in the cnpack group about how to obtain the ID of a multi-core CPU.
Thanks to all the Members in the cnpack group.
BS said, "you have spent a lot of time doing this worthless thing in your short life "-_-!! Super BS, BS, BS ...... Pai_^
For I: = 0 to 10000000000000000000000000000 do
BS;
Appendix "Good Morning Air" quotations:
'Give up, the API is not suitable for you'
'You spend a lot of time doing this worthless thing in your short life'