Delphi(程序人生)

Delphi Programer DoubleCat

导航

获取CPU的ID号

program Project2;
$APPTYPE CONSOLE}
uses
  SysUtils, StrUtils,
  Windows; //注重,要加入这个文件
type
  //CPUID  一共有4组,4组连接成串是世界唯一的,常用于软件注册
  TCPUID = array[1..4] of Longint;
function GetCPUID: TCPUID; assembler; register;
asm
    PUSH         EBX
    PUSH         EDI
    MOV         EDI,EAX
    MOV         EAX,1
    DW             $A20F
    STOSD
    MOV         EAX,EBX
    STOSD
    MOV         EAX,ECX
    STOSD
    MOV         EAX,EDX
    STOSD
    POP         EDI
    POP         EBX
end;

function GetCPUSpeed: Double;
const
  DelayTime = 100;
var
  TimerHi, TimerLo: DWORD;
  PriorityClass, Priority: Integer;
begin
  PriorityClass := GetPriorityClass(GetCurrentProcess);
  Priority := GetThreadPriority(GetCurrentThread);
  SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS);
  SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL);
  Sleep(DelayTime);
  asm
        DW     310FH
        MOV   TimerLo,   EAX
        MOV   TimerHi,   EDX
  end;
  Sleep(DelayTime);
  asm
        DW     310FH
        SUB   EAX,   TimerLo
        SBB   EDX,   TimerHi
        MOV   TimerLo,   EAX
        MOV   TimerHi,   EDX
  end;
  SetThreadPriority(GetCurrentThread, Priority);
  SetPriorityClass(GetCurrentProcess, PriorityClass);
  Result := TimerLo / (1000.0 * DelayTime);
end;
var
  sCmd: string;
  i:integer;
  label here;
begin
  { TODO -oUser -cConsole Main : Insert code here }
  Writeln('你可以选择一下命令输入:');
  Writeln('cpuspd  返回本机CPU的速度');
  Writeln('cpuid -n/a 返回本机CPU的速度,参数n为1到4的整数,参数a返回全部CPU的ID');
  Writeln('exit 退出程序');
  here:
  Readln(sCmd);
  if Trim(sCmd)='exit' then Exit;
  if Trim(sCmd)='cpuspd' then Writeln(GetCPUSpeed)
  else
  if (LeftStr(TrimLeft(sCmd),8)='cpuid -a') then
    Writeln(inttostr(GetCPUID[1]) inttostr(GetCPUID[2]) inttostr(GetCPUID[3]) inttostr(GetCPUID[4]))
  else
    if (LeftStr(TrimLeft(sCmd),7)='cpuid -') then
    begin
       TryStrToInt(RightStr(Trim(sCmd),1),i);
       if (i>=1) and (i<=4) then Writeln(GetCPUID[i])
       else Writeln(sCmd '不是合法命令!');
    end
    else
      Writeln(sCmd '不是合法命令!');
  goto here;

end.

posted on 2008-06-14 17:39  DoubleCat  阅读(863)  评论(0编辑  收藏  举报