Search code examples
delphi64-bitdelphi-xe5

How to get CPU speed from Delphi 64 bit application (XE5)?


We are in the process of migrating our application suite to 64 bit using Delphi XE5. In our about boxes we display the CPU speed. The routine we currently use is 32 bit assembly and therefore will not compile under 64 bit.

Is there a native way to retrieve CPU speed from within Delphi 64 bit?


Solution

  • If your platform is windows and you want the advertised CPU speed then you can simply check the HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0\~MHz DWORD value in the Registry:

    program SO21757165;
    
    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      Registry,
      Windows,
      System.SysUtils;
    
    function GetCPUSpeed : String;
    
    var
      Reg : TRegistry;
    
    begin
     Reg := TRegistry.Create(KEY_QUERY_VALUE);
     try
      Reg.RootKey := HKEY_LOCAL_MACHINE;
      if Reg.OpenKeyReadOnly('HARDWARE\DESCRIPTION\System\CentralProcessor\0') then
       begin
        Result := Format('CPU Speed is %dMHz', [Reg.ReadInteger('~MHz')]);
        Reg.CloseKey;
       end;
     finally
      Reg.Free;
     end;
    end;
    
    begin
      try
        { TODO -oUser -cConsole Main : Insert code here }
        Writeln(GetCPUSpeed);
        Readln;
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    end.
    

    EDIT

    rewrote above code snippet to a compileable example. This example has been verified in XE7and works in 32bit and 64 bit environments (with UAC enabled)