-2

I would like to use a Label or a Memo to view information:

function GetCPUSpeed: Double;
const
  DelayTime = 500;
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(10);
  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 * DelayTime);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(Format('Your CPU speed: %f MHz', [GetCPUSpeed]));
  Label1.caption := GetCPUSpeed;
  Memo1.Lines := GetCPUSpeed;
end;

To use the Label or a Memo, I have to convert data into a string. How can I do that?

[DCC Error] speed_cpu.pas(62): E2010 Incompatible types: 'string' and 'Double'

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
MrCamarium
  • 5
  • 1
  • 5

2 Answers2

0

First off, you can't assign a string to the TMemo.Lines property, which is a TStrings object. You would have to assign to either TMemo.Lines.Text or TMemo.Text instead.

As for the actual conversion, you already know one way to do it, via SysUtils.Format():

uses
  ..., SysUtils;

var
  Speed: Double;
...
Speed := GetCPUSpeed;
ShowMessage(Format('Your CPU speed: %f MHz', [Speed]));
Label1.Caption := Format('%f', [Speed]);
Memo1.Text := Format('%f', [Speed]);

You can also use SysUtils.FloatToStr():

uses
  ..., SysUtils;

var
  Speed: Double;
...
Speed := GetCPUSpeed;
ShowMessage(Format('Your CPU speed: %f MHz', [Speed]));
Label1.Caption := FloatToStr(Speed);
Memo1.Text := FloatToStr(Speed);

In XE4+, you can also use SysUtils.TDoubleHelper.ToString():

uses
  ..., SysUtils;

var
  Speed: Double;
...
Speed := GetCPUSpeed;
ShowMessage(Format('Your CPU speed: %f MHz', [Speed]));
Label1.Caption := Speed.ToString;
Memo1.Text := Speed.ToString;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
-1

Thank you problem solved.

    function Tipo_cpu: string;
        var
          aVendor: array[0..2] of DWord;
          iI, iJ : Integer;
        begin
          asm
            push ebx
            xor   eax, eax
            dw    $A20F // CPUID instruction
            mov   DWord ptr aVendor, ebx
            mov   DWord ptr aVendor[+4], edx
            mov   DWord ptr aVendor[+8], ecx
            pop   ebx
          end;
          for iI := 0 to 2 do
            for iJ := 0 to 3 do
            Result := Result + Chr((aVendor[iI] and ($000000FF shl (iJ * 8))) shr (iJ * 8));
        end;
    
    function GetCPUSpeed: Double;
    const
      DelayTime = 500;
    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(10);
      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 * DelayTime);
    end;
    
procedure TForm1.FormCreate(Sender: TObject);
var
  ids: TidIpWatch;
  Speed: Double;
begin
 ids := TidIpWatch.Create;
 Speed := GetCPUSpeed;
 Memo1.Text := 'IP:' + (ids.LocalIP);
 Memo1.Text := (Tipo_cpu);
 Memo1.Text := Format('%f', [Speed]);
 ids.Free;
 end;
MrCamarium
  • 5
  • 1
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 08 '22 at 08:29
  • This doesn't answer the question that was asked. – David Heffernan Feb 10 '22 at 07:11