求一段获取cpu唯一标识的delphi代码。该怎么处理
求一段获取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;
}
------解决方案--------------------
附上一段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; interface uses 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; implementation uses 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.