求一段获取cpu唯一标识的delphi代码。
附上一段C++代码,希望把这段c++代码修改成delphi的。多谢各位大神~
CString CGetCpuIDDlg::GetCPUID()
{
CString CPUID;
unsigned long s1,s2;
unsigned char vendor_id[]="------------";
char sel;
sel='1';
CString VernderID;
CString MyCpuID,CPUID1,CPUID2;
switch(sel)
{
case '1':
__asm{
xor eax,eax //eax=0:取Vendor信息
cpuid //取cpu id指令,可在Ring3级使用
mov dword ptr vendor_id,ebx
mov dword ptr vendor_id[+4],edx
mov dword ptr vendor_id[+8],ecx
}
VernderID.Format("%s-",vendor_id);
__asm{
mov eax,01h //eax=1:取CPU序列号
xor edx,edx
cpuid
mov s1,edx
mov s2,eax
}
CPUID1.Format("%08X%08X",s1,s2);
__asm{
mov eax,03h
xor ecx,ecx
xor edx,edx
cpuid
mov s1,edx
mov s2,ecx
}
CPUID2.Format("%08X%08X",s1,s2);
break;
case '2':
{
__asm{
mov ecx,119h
rdmsr
or eax,00200000h
wrmsr
}
}
AfxMessageBox("CPU id is disabled.");
break;
}
MyCpuID = CPUID1+CPUID2;
CPUID = MyCpuID;
return CPUID;
}
[解决办法]
- Delphi(Pascal) code
Delphi编程 -- 使用CPUID指令获取CPU信息 unit CPU;interfaceuses Types;const //Version Information bitmask BITMASK_ExtendedFamilyID=$0FF00000; BITMASK_ExtendedModelID= $000F0000; BITMASK_ProcessorType= $00003000; BITMASK_FamilyID= $00000F00; BITMASK_ModelID= $000000F0; BITMASK_SteppingID= $0000000F; //Processor Type ProcessorType_OriginalOEMProcessor= $00000000; ProcessorType_IntelOverDriveProcessor= $00001000; ProcessorType_DualProcessor= $00002000; ProcessorType_IntelReserved= $00003000;type //register used in cpuid instruction TCPUIDRegister=packed record EAX:DWORD; case Integer of 0://register ( EBX:DWORD; ECX:DWORD; EDX:DWORD; ); 1://version information string ( VIS1:array[1..4] of Char; VIS2:array[1..4] of Char; VIS3:array[1..4] of Char; ); end; PCPUIDRegister=^TCPUIDRegister; //basic cupid information TCPUIDBasicInformation=packed record //EAX=0 HighestBasicProcessorInformationValue:Cardinal; VendorIdentificationString:string[12]; //EAX=1 ExtendedFamileyID, ExtendedModelID, ProcessorType, FamilyID, ModelID, SteppingID:DWORD; end; PCPUIDBasicInformation=^TCPUIDBasicInformation; TCPUInfo=class private fSupportForCPUID:Boolean; private function CPUID(Value:DWORD):TCPUIDRegister; function Get_CPUIDBasicInformation:TCPUIDBasicInformation; public constructor Create; public property SupportForCPUID:Boolean read fSupportForCPUID; property CPUIDBasicInformation:TCPUIDBasicInformation read Get_CPUIDBasicInformation; end;implementationuses Dialogs,SysUtils;{ TCPUInfo }function TCPUInfo.CPUID(Value:DWORD): TCPUIDRegister;var VEAX,VEBX,VECX,VEDX:DWORD;begin asm pushad mov eax,Value cpuid mov VEAX,eax mov VEBX,ebx mov VECX,ecx mov VEDX,edx popad end; with Result do begin EAX:=VEAX; EBX:=VEBX; ECX:=VECX; EDX:=VEDX; end;end;constructor TCPUInfo.Create;const EF_ID:DWORD=$200000;var EFID:DWORD;begin fSupportForCPUID:=False; asm //save current register pushad //move old eflags to ebx pushfd pop eax //move old eflags to ebx mov ebx,eax //revert EF_ID xor eax,EF_ID push eax popfd //move new eflags to eax pushfd pop eax //test EF_ID xor eax,ebx mov EFID,eax //restore register popad end; if (EFID xor EF_ID)=0 then fSupportForCPUID:=True;end;function TCPUInfo.Get_CPUIDBasicInformation: TCPUIDBasicInformation;var CPUIDRegister:TCPUIDRegister;begin CPUIDRegister:=CPUID(0); with CPUIDRegister,Result do begin HighestBasicProcessorInformationValue:=EAX; VendorIdentificationString:=VIS1+VIS3+VIS2; end; CPUIDRegister:=CPUID(1); with CPUIDRegister,Result do begin ExtendedFamileyID:=EAX and BITMASK_ExtendedFamilyID; ExtendedModelID:=EAX and BITMASK_ExtendedModelID; ProcessorType:=EAX and BITMASK_ProcessorType; FamilyID:=EAX and BITMASK_FamilyID; ModelID:=EAX and BITMASK_ModelID; SteppingID:=EAX and BITMASK_SteppingID; end;end;end.
[解决办法]
用 Delphi 得到 CPU 的序列号
unit Main;
interface
uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
ExtCtrls,
StdCtrls,
Buttons;
type
TDemoForm = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
GetButton: TBitBtn;
CloseButton: TBitBtn;
Bevel1: TBevel;
Label5: TLabel;
FLabel: TLabel;
MLabel: TLabel;
PLabel: TLabel;
SLabel: TLabel;
PValue: TLabel;
FValue: TLabel;
MValue: TLabel;
SValue: TLabel;
procedure GetButtonClick(Sender: TObject);
end;
var
DemoForm: TDemoForm;
implementation
{$R *.DFM}
const
ID_BIT = $200000; // EFLAGS ID bit
type
TCPUID = array[1..4] of Longint;
TVendor = array [0..11] of char;
function IsCPUID_Available : Boolean; register;
asm
PUSHFD {direct access to flags no possible, only via stack}
POP EAX {flags to EAX}
MOV EDX,EAX {save current flags}
XOR EAX,ID_BIT {not ID bit}
PUSH EAX {onto stack}
POPFD {from stack to flags, with not ID bit}
PUSHFD {back to stack}
POP EAX {get back to EAX}
XOR EAX,EDX {check if ID bit affected}
JZ @exit {no, CPUID not availavle}
MOV AL,True {Result=True}
@exit:
end;
function GetCPUID : TCPUID; assembler; register;
asm
PUSH EBX {Save affected register}
PUSH EDI
MOV EDI,EAX {@Resukt}
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;
function GetCPUVendor : TVendor; assembler; register;
asm
PUSH EBX {Save affected register}
PUSH EDI
MOV EDI,EAX {@Result (TVendor)}
MOV EAX,0
DW $A20F {CPUID Command}
MOV EAX,EBX
XCHG EBX,ECX {save ECX result}
MOV ECX,4
@1:
STOSB
SHR EAX,8
LOOP @1
MOV EAX,EDX
MOV ECX,4
@2:
STOSB
SHR EAX,8
LOOP @2
MOV EAX,EBX
MOV ECX,4
@3:
STOSB
SHR EAX,8
LOOP @3
POP EDI {Restore registers}
POP EBX
end;
procedure TDemoForm.GetButtonClick(Sender: TObject);
var
CPUID : TCPUID;
I : Integer;
S : TVendor;
begin
for I := Low(CPUID) to High(CPUID) do CPUID := -1;
if IsCPUID_Available then begin
CPUID := GetCPUID;
Label1.Caption := ’CPUID[1] = ’ + IntToHex(CPUID[1],8);
Label2.Caption := ’CPUID[2] = ’ + IntToHex(CPUID[2],8);
Label3.Caption := ’CPUID[3] = ’ + IntToHex(CPUID[3],8);
Label4.Caption := ’CPUID[4] = ’ + IntToHex(CPUID[4],8);
PValue.Caption := IntToStr(CPUID[1] shr 12 and 3);
FValue.Caption := IntToStr(CPUID[1] shr 8 and $f);
MValue.Caption := IntToStr(CPUID[1] shr 4 and $f);
SValue.Caption := IntToStr(CPUID[1] and $f);
S := GetCPUVendor;
Label5.Caption := ’Vendor: ’ + S; end
else begin
Label5.Caption := ’CPUID not available’;
end;
end;
end.