获取硬件、CPUID等信息


unit CommonUnit;

interface

uses
  Windows, SysUtils, DateUtils;

Const
  CPUVendorIDs: array [0 .. 5] of string = ('GenuineIntel', 'UMC UMC UMC',
    'AuthenticAMD', 'CyrixInstead', 'NexGenDriven', 'CentaurHauls');
  // 将CPU厂家信息转换成字串形式
  CPUVendors: array [0 .. 5] of string = ('Intel', 'UMC', 'AMD', 'Cyrix',
    'NexGen', 'CentaurHauls');

type
  TVendor = array [0 .. 11] of AnsiChar;

  { 将AnsiString的乱码转换成能正常显示的Utf8编码的字符串 }
function DecodeUtf8Str(const S: string): WideString;
function DateToInt64(date: TDateTime): Int64;
function Int64ToDate(num: Int64): TDateTime;

function GetCPUID: string;
function GetIdeSerialNumber: string;
function GetCPUVendor: TVendor;
function GetCPUV: string;

implementation

function DecodeUtf8Str(const S: string): WideString;
var
  lenSrc, lenDst: Integer;
begin
  lenSrc := Length(S);
  if (lenSrc = 0) then
    Exit;
  lenDst := MultiByteToWideChar(CP_UTF8, 0, Pointer(S), lenSrc, nil, 0);
  SetLength(Result, lenDst);
  MultiByteToWideChar(CP_UTF8, 0, Pointer(S), lenSrc, Pointer(Result), lenDst);
end;

function DateToInt64(date: TDateTime): Int64;
var
  Bias: Integer;
  a1, a2: Extended;
  T1, T2: TDateTime;
  TS, TS2: TTimeStamp;
  pTime: _TIME_ZONE_INFORMATION;
begin
  GetTimeZoneInformation(pTime); // 获取时区
  Bias := pTime.Bias;
  T1 := IncMinute(date, Bias);
  T2 := EncodeDateTime(1970, 1, 1, 0, 0, 0, 0);
  TS := DateTimeToTimeStamp(T1);
  TS2 := DateTimeToTimeStamp(T2);
  a1 := TimeStampToMSecs(TS);
  a2 := TimeStampToMSecs(TS2);

  Result := StrToInt64Def(FloatToStr(a1 - a2), 0);
end;

function Int64ToDate(num: Int64): TDateTime;
var
  Bias: Integer;
  a1, a2: Extended;
  T1, T2: TDateTime;
  TS, TS2: TTimeStamp;
  pTime: _TIME_ZONE_INFORMATION;
begin
  GetTimeZoneInformation(pTime); // 获取时区
  Bias := pTime.Bias;
  // Bias := Bias + pTime.DaylightBias;
  T2 := EncodeDateTime(1970, 1, 1, 0, 0, 0, 0);
  TS2 := DateTimeToTimeStamp(T2);
  a2 := TimeStampToMSecs(TS2);
  a1 := StrToFloat(IntToStr(num));
  TS := MSecsToTimeStamp(a1 + a2);
  T1 := TimeStampToDateTime(TS);
  T1 := IncMinute(T1, -Bias);
  Result := T1;
end;

function GetCPUID: string;
  procedure SetCPU(Handle: THandle; CPUNO: Integer);
  var
    ProcessAffinity: Cardinal;
    _SystemAffinity: Cardinal;
  begin
    GetProcessAffinityMask(Handle, ProcessAffinity, _SystemAffinity);
    ProcessAffinity := CPUNO;
    SetProcessAffinityMask(Handle, ProcessAffinity);
  end;

const
  CPUINFO = '%s-%.8x%.8x';
var
  iEax: Integer;
  iEbx: Integer;
  iEcx: Integer;
  iEdx: Integer;
begin
  SetCPU(GetCurrentProcess, 1);
  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, [GetCPUV, iEdx,iEax]);
end;

function GetCPUV: string;
var
  Vendor: string;
  VendorID, I: Integer;
begin
  Vendor := GetCPUVendor;
  {for I := 0 to High(CPUVendorIDs) do
  begin
    If Vendor = CPUVendorIDs[I] then
    begin
      Vendor := CPUVendorIDs[I];
      VendorID := I;
      break;
    end;
  end; }
  Result := Vendor;
end;

// 获取CPU厂家信息,返回值为TVendor类型
function GetCPUVendor: TVendor;assembler;register;
asm
  PUSH    EBX
  PUSH    EDI
  MOV     EDI,EAX
  MOV     EAX,0
  DW      $A20F  // CPUID指令
  MOV     EAX,EBX
  XCHG      EBX,ECX
  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
  POP     EBX
end;

function GetIdeSerialNumber: string; // 获取硬盘的出厂系列号;
var
  RootPath: array [0 .. 20] of char;
  VolName: array [0 .. 255] of char;
  SerialNumber: DWORD;
  MaxCLength: DWORD;
  FileSysFlag: DWORD;
  FileSysName: array [0 .. 255] of char;
begin
  RootPath := 'C:\';

  GetVolumeInformation(RootPath, VolName, 255, @SerialNumber, MaxCLength,
    FileSysFlag, FileSysName, 255);
  Result := Format('%s', [IntToHex(SerialNumber, 8)]);
end;

end.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值