Improvement for 64-bit OS up time

Here you can propose new features, make suggestions etc.

Moderators: white, Hacker, petermad, Stefan2

Post Reply
User avatar
MarcinW
Power Member
Power Member
Posts: 852
Joined: 2012-01-23, 15:58 UTC
Location: Poland

Improvement for 64-bit OS up time

Post 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
User avatar
ghisler(Author)
Site Admin
Site Admin
Posts: 48077
Joined: 2003-02-04, 09:46 UTC
Location: Switzerland
Contact:

Post 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.
Last edited by ghisler(Author) on 2018-05-10, 15:17 UTC, edited 1 time in total.
Author of Total Commander
https://www.ghisler.com
User avatar
MarcinW
Power Member
Power Member
Posts: 852
Joined: 2012-01-23, 15:58 UTC
Location: Poland

Post by *MarcinW »

Ok.
Post Reply