![]() |
Uptime eines Remoterechners ermitteln
Die Uptime und die lokale Zeit eines Remoterechners lässt sich über die API-Funktion
![]()
Delphi-Quellcode:
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.
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 Uptime steht in dem Feld tod_msecs. Anbei noch eine Funktion, welche die Millisekunde in eine benutzerfreudnliche Formatierung umwandelt:
Delphi-Quellcode:
Die Angabe erfolgt in Tagen, Stunden und Minuten (xd yh tm).
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; [edit=fkerber]Änderungen eingefügt wie in Vorschlagsthread besprochen. Mfg, fkerber[/edit] |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:53 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz