Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 

Uptime eines Remoterechners ermitteln

  Alt 7. Sep 2008, 02:02
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]
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat