Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Internet / LAN / ASP.NET (https://www.delphipraxis.net/23-library-internet-lan-asp-net/)
-   -   Delphi Uptime eines Remoterechners ermitteln (https://www.delphipraxis.net/120193-uptime-eines-remoterechners-ermitteln.html)

Luckie 7. Sep 2008 02:02


Uptime eines Remoterechners ermitteln
 
Die Uptime und die lokale Zeit eines Remoterechners lässt sich über die API-Funktion MSDN-Library durchsuchenNetRemoteTOD ermitteln:
Delphi-Quellcode:
type
  NET_API_STATUS = DWORD;

type
  PTimeOfDayInfo = ^TTimeOfDayInfo;
  TTimeOfDayInfo = packed record
    tod_elapsedt: DWORD;
    tod_msecs: DWORD;
    tod_hours: DWORD;
    tod_mins: DWORD;
    tod_secs: DWORD;
    tod_hunds: DWORD;
    tod_timezone: Longint;
    tod_tinterval: DWORD;
    tod_day: DWORD;
    tod_month: DWORD;
    tod_year: DWORD;
    tod_weekday: DWORD;
  end;


const
  NERR_Success = 0;
  netapi32lib = 'netapi32.dll';


// [..]

function NetApiBufferFree(Buffer: Pointer): NET_API_STATUS; stdcall; external netapi32lib name 'NetApiBufferFree';
function NetRemoteTOD(UncServerName: LPCWSTR; BufferPtr: PBYTE): NET_API_STATUS; stdcall; external netapi32lib name 'NetRemoteTOD';

// [..]

function GetRemoteToD(machine: WideString): TTimeOfDayInfo;
var
  TimeOfDayInfo: PTimeOfDayInfo;
  dwRetValue: DWORD;
begin
  dwRetValue := NetRemoteTOD(PWideChar(WideString(Machine)), PBYTE(@TimeOfDayInfo));
   if dwRetValue <> NERR_Success then
    raise Exception.Create(SysErrorMessage(dwRetValue));
  with TimeOfDayInfo^ do
  begin
    Result := TimeOfDayInfo^;
    NetApiBufferFree(TimeOfDayInfo);
  end;
end;
Die Funktion wirft eine Exception mit der Systemfehlermeldung, wenn die Abfrage fehlschlägt. Es ist keine Authentifizierung am Remoterechner notwendig und es sind auch keine administrativen Rechte nötig.

Die Uptime steht in dem Feld tod_msecs.

Anbei noch eine Funktion, welche die Millisekunde in eine benutzerfreudnliche Formatierung umwandelt:

Delphi-Quellcode:
function FormatUpTime(msecs: int64): string;
var
  dwSecs           : DWORD;
  dwDays           : DWORD;
  ts               : SysUtils.TTimeStamp;
  UpTime           : TDateTime;
begin
  dwDays := 0;
  dwSecs := msecs div 1000;
  if dwSecs >= SecsPerDay then
  begin
    dwDays := dwSecs div SecsPerDay;
    dwSecs := dwSecs mod SecsPerDay;
  end;
  ts.Time := dwSecs * 1000;
  ts.Date := DateDelta;
  UpTime := SysUtils.TimeStampToDateTime(ts);
  Result := Format('%ud %sh %smin', [dwDays, FormatDateTime('h', UpTime),
    FormatDateTime('n', UpTime)])
end;
Die Angabe erfolgt in Tagen, Stunden und Minuten (xd yh tm).

[edit=fkerber]Änderungen eingefügt wie in Vorschlagsthread besprochen. Mfg, fkerber[/edit]


Alle Zeitangaben in WEZ +1. Es ist jetzt 14:09 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz