Page 1 of 1

Improvement for 64-bit OS up time

Posted: 2018-05-08, 12:32 UTC
by MarcinW
History.txt wrote:02.05.18 Fixed: Use GetTickCount64 on Windows Vista or newer to show system up time >49 days (32/64)
You may try to further improve the algorithm by using QueryPerformanceXXX APIs, so you will get 64-bit up time even on most Win9x systems:

Code: Select all

{$APPTYPE CONSOLE}

uses
  Windows, SysUtils;

function GetUpTimeInMSecs : Int64;
var
  GetTickCount64 : function : Int64; stdcall;
  Buffer : array[0..3*SizeOf(Int64)-2] of Byte;
  C, F : PInt64;
begin
  GetTickCount64:=GetProcAddress(GetModuleHandle('kernel32.dll'),'GetTickCount64');
  if Assigned(GetTickCount64) then
    Result:=GetTickCount64
  else
  begin
    Result:=GetTickCount;

    {In some cases QueryPerformanceXXX may fail, if argument is not 8-byte aligned}
    C:=@Buffer[0];
    F:=@Buffer[SizeOf(C^)];
    if HINST(C) mod SizeOf(C^) <> 0 then
    begin
      Inc(PByte(C),SizeOf(C^) - HINST(C) mod SizeOf(C^));
      Inc(PByte(F),SizeOf(F^) - HINST(F) mod SizeOf(F^));
    end;

    if QueryPerformanceCounter(C^) and QueryPerformanceFrequency(F^) then
    if F^ <> 0 then
      Result:=Round(C^/F^*1000);
  end;
end;

begin
  Writeln('GetTickCount     = '+IntToStr(GetTickCount));
  Writeln('GetUpTimeInMSecs = '+IntToStr(GetUpTimeInMSecs));
  Readln;
end.
Regards

Posted: 2018-05-09, 17:03 UTC
by ghisler(Author)
I didn't do this because they are no longer relevant. It's not a big problem if this value is wrong on these older systems.

Posted: 2018-05-09, 19:03 UTC
by MarcinW
Ok.