get CPU id (很全的)

//This unit can be used to detect the cpu model.
unit CpuId;
interface
uses Windows, Mmsystem, Sysutils, Math, Dialogs;
type
    TCpuRec=record
       Name:string[128];
       Vendor:string[12];
       Frequency:word;
       Family:integer;
       Model:integer;
       Stepping:integer;
       L1DCache:word;
       L1ICache:word;
       L2Cache:word;
     end;
    TCpuType = (cpu8086, cpu286, cpu386, cpu486, cpuPentium);
    TCpuData=object
      function GetCPUIDSupport:Boolean;
      function GetVendorString:string;
      function GetCPUFrequency:word;
      procedure GetFMS(var Family,Model,Stepping:byte);
      function GetMaxCpuId:dword;
      function CheckFPU:Boolean;
      function CheckTSC:Boolean;
      function CheckMSR:Boolean;
      function CheckMPS:Boolean;
      function GetNoCpus:cardinal;
      function CheckPN:Boolean;
      function CheckCMPXCHG8B:Boolean;
      function CheckCMOVe:Boolean;
      function CheckSelfSnoop:Boolean;
      function CheckDebugTraceStore:Boolean;
      function CheckFXSAVEFXRSTOR:Boolean;
      function CheckMMX:Boolean;
      function CheckMMXplus:Boolean;
      function CheckSSE:Boolean;
      function CheckSSE2:Boolean;
      function CheckAMD3DNow:Boolean;
      function CheckAMD3DNowPlus:Boolean;
      function GetMaxExtendedFunctions:dword;
      procedure GetExtendedFMS(var Family,Model,Stepping:byte);
      function GetExtendedCpuName:string;
      function GetExtendedL1DCache:word;
      function GetExtendedL1ICache:word;
      function GetExtendedL2Cache:word;

      function CheckCeleron:Boolean;
      function CheckPentiumIII:Boolean;
      function CheckXeon:Boolean;
      function CheckPentium4:Boolean;
      function CheckIthanium:Boolean;
      function IntelP5N:string;
      function IntelP6N:string;
      function AMDK5N:string;
      function Cyrix686N:string;
      function GenericCpuN:string;
      function P5CacheL1DI:word;
      function P6CacheL1DI:word;
      function P6CacheL2:word;

      function AuthenticAMD:TCpuRec;

      function GenuineIntel:TCpuRec;
      function CyrixInstead:TCpuRec;
      function GenericCPU:TCpuRec;
     end;
const
Intel486:array[0..8] of string=
('Intel 486 DX',
  'Intel 486 DX',
  'Intel 486 SX',
  'Intel 486 DX2',
  'Intel 486 SL',
  'Intel 486 SX2',
  'Intel 486 DX2',
  'Intel 486 DX4',
  'Intel 486 DX4');
UMC486:array[0..1] of string=
('UMC U5D',
  'UMC U5S');
AMD486:array[0..5] of string=
('AMD 486 DX2',
  'AMD 486 DX2',
  'AMD 486 DX4',
  'AMD 486 DX4',
  'AMD 5x86',
  'AMD 5x86');
IntelP5:array[0..6] of string=
('Intel Pentium P5 A-Step',
  'Intel Pentium P5',
  'Intel Pentium P54C',
  'Intel Pentium P24T Overdrive',
  'Intel Pentium MMX P55C',
  'Intel Pentium P54C',
  'Intel Pentium MMX P55C');
  NexGenNx586='NexGen Nx586';
  Cyrix4x86='VIA Cyrix 4x86';
  Cyrix5x86='VIA Cyrix 5x86';
  CyrixMediaGX='VIA Cyrix Media GX';
  CyrixM1='VIA Cyrix 6x86';
  CyrixM2='VIA Cyrix 6x86MX';
  CyrixIII='VIA Cyrix III';
  AMDK5:array[0..3] of string=
  ('AMD SSA5 (PR75/PR90/PR100)',
   'AMD 5k86 (PR120/PR133)',
   'AMD 5k86 (PR166)',
   'AMD 5k86 (PR200)');
  AMDK6:array[0..4] of string=
  ('AMD K6 (166~233)',
   'AMD K6 (266~300)',
   'AMD K6-2',
   'AMD K6-III',
   'AMD K6-2+ or K6-III+');
   Centaur:array[0..2] of string=
   ('Centaur C6',
    'Centaur C2',
    'Centaur C3');
   Rise:array[0..1] of string=
   ('Rise mP6',
    'Rise mP6');
   IntelP6:array[0..7] of string=
   ('Intel Pentium Pro A-Step',
    'Intel Pentium Pro',
    'Intel Pentium II',
    'Intel Pentium II',
    'Intel Pentium II',
    'Intel Pentium III',
    'Intel Pentium III',
    'Intel Pentium III');
   AMDK7:array[0..3] of string=
    ('AMD Athlon(tm) Processor',
     'AMD Athlon(tm) Processor',
     'AMD Duron(tm) Processor',
     'AMD Thunderbird Processor');
   IntelP4='Intel Pentium 4';
var CpuData:TCpuData;
implementation
function TCpuData.GetCPUIDSupport:Boolean;
var TempDetect:dword;
begin
asm
  pushf
  pushfd
  push eax
  push ebx
  push ecx
  push edx

  pushfd
  pop eax
  mov ebx,eax
  xor eax,$00200000
  push eax
  popfd
  pushfd
  pop eax
  push ebx
  popfd
  xor eax,ebx
  mov TempDetect,eax

  pop edx
  pop ecx
  pop ebx
  pop eax
  popfd
  popf
end;
GetCPUIDSupport:=(TempDetect=$00200000);
end;
function TCpuData.GetVendorString:string;
var s1,s2,s3:array[0..3] of char;
    TempVendor:string;
    i:integer;
begin
asm
  push eax
  push ebx
  push ecx
  push edx
  mov eax,0
  db $0F,$A2               /// cpuid
  mov s1,ebx
  mov s2,edx
  mov s3,ecx
  pop edx
  pop ecx
  pop ebx
  pop eax
end;
TempVendor:='';
for i:=0 to 3 do
  TempVendor:=TempVendor+s1[i];
for i:=0 to 3 do
  TempVendor:=TempVendor+s2[i];
for i:=0 to 3 do
  TempVendor:=TempVendor+s3[i];
GetVendorString:=TempVendor;
end;
function TCpuData.GetCPUFrequency:word;
const
  timePeriod= 1000;
var
  HighFreq,TestFreq,Count1,Count2:int64;
  TimeStart:integer;
  TimeStop:integer;
  ElapsedTime:dword;
  StartTicks:dword;
  EndTicks:dword;
  TotalTicks:dword;
begin
  StartTicks:=0;
  EndTicks:=0;
  if QueryPerformanceFrequency(HighFreq) then begin

    TestFreq:=HighFreq div 100;

    QueryPerformanceCounter(Count1);
    repeat
      QueryPerformanceCounter(Count2);
    until Count1<>Count2;

    asm
      push ebx
      xor eax,eax
      xor ebx,ebx
      xor ecx,ecx
      xor edx,edx
      db $0F,$A2               /// cpuid
      db $0F,$31               /// rdtsc
      mov StartTicks,eax
      pop ebx
    end;

    repeat
      QueryPerformanceCounter(Count1);
    until Count1-Count2>=TestFreq;

    asm
      push ebx
      xor eax,eax
      xor ebx,ebx
      xor ecx,ecx
      xor edx,edx
      db $0F,$A2               /// cpuid
      db $0F,$31               /// rdtsc
      mov EndTicks,eax
      pop ebx
    end;

    ElapsedTime:=MulDiv(Count1-Count2,1000000,HighFreq);
  end
  else begin
    time
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在golang中,要完整详细地获取CPU ID、硬盘等各种硬件信息,通常需要使用操作系统(OS)提供的相关接口和系统工具。下面分别介绍如何获取这些信息: 1. 获取CPU ID: 要获取CPU ID,可以使用golang的相关库如`github.com/shirou/gopsutil/cpu`。 该库提供了获取CPU信息的接口。下面是一个示例代码片段: ```go package main import ( "fmt" "github.com/shirou/gopsutil/cpu" ) func main() { infos, err := cpu.Info() if err != nil { fmt.Printf("Failed to get CPU info: %v", err) return } for _, info := range infos { fmt.Printf("CPU ID: %v\n", info.SerialNumber) // 获取CPU序列号 } } ``` 2. 获取硬盘信息: 要获取硬盘信息,可以使用golang的库`github.com/shirou/gopsutil/disk`。该库提供了获取硬盘信息的接口。下面是一个示例代码片段: ```go package main import ( "fmt" "github.com/shirou/gopsutil/disk" ) func main() { partitions, err := disk.Partitions(false) // 获取分区信息 if err != nil { fmt.Printf("Failed to get disk partitions: %v", err) return } for _, partition := range partitions { fmt.Printf("Partition: %v\n", partition.Device) info, err := disk.Usage(partition.Mountpoint) // 获取分区使用情况 if err != nil { fmt.Printf("Failed to get disk usage: %v", err) continue } fmt.Printf("Usage: %v\n", info.Used) } } ``` 以上代码用于获取硬盘的分区信息和使用情况。 除了使用golang的库,也可以通过调用外部系统工具的方式获取硬件信息。例如,可以使用`os/exec`库调用`wmic`命令(Windows环境下)或`lshw`命令(Linux环境下)来获取更详细的硬件信息。 例如,在Linux环境下,可以使用以下代码来获取硬件信息: ```go package main import ( "bytes" "fmt" "os/exec" ) func main() { cmd := exec.Command("lshw", "-short") var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() if err != nil { fmt.Printf("Failed to get hardware info: %v", err) return } fmt.Printf("Hardware Info:\n%s", out.String()) } ``` 以上代码使用`lshw -short`命令获取硬件信息。 需要注意的是,具体获取硬件信息的方式和结果可能因操作系统而异。可以根据实际情况选择合适的方法来获取硬件信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ypyRock

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值