使用Delphi的Windows上次启动日期和时间

Kaw*_*hii 2 windows delphi uptime

如何在Windows 2008/2003计算机上获取上次启动/重启/重启的日期和时间?

我知道从命令提示符我们可以使用"网络统计",但是如何通过Delphi实现呢?

谢谢.

RRU*_*RUZ 7

您可以使用 返回WMI类的LastBootUpTime属性(注意:此属性的返回值是UTC格式).Win32_OperatingSystemDate and time the operating system was last restarted

检查此示例应用

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  Variants,
  ComObj;


//Universal Time (UTC) format of YYYYMMDDHHMMSS.MMMMMM(+-)OOO.
//20091231000000.000000+000
function UtcToDateTime(const V : OleVariant): TDateTime;
var
  Dt : OleVariant;
begin
  Result:=0;
  if VarIsNull(V) then exit;
  Dt:=CreateOleObject('WbemScripting.SWbemDateTime');
  Dt.Value := V;
  Result:=Dt.GetVarDate;
end;

procedure  GetWin32_OperatingSystemInfo;
const
  WbemUser            ='';
  WbemPassword        ='';
  WbemComputer        ='localhost';
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_OperatingSystem','WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  if oEnum.Next(1, FWbemObject, iValue) = 0 then
  begin
    Writeln(Format('Last BootUp Time    %s',[FWbemObject.LastBootUpTime]));// In utc format
    Writeln(Format('Last BootUp Time    %s',[formatDateTime('dd-mm-yyyy hh:nn:ss',UtcToDateTime(FWbemObject.LastBootUpTime))]));// Datetime
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      GetWin32_OperatingSystemInfo;
    finally
      CoUninitialize;
    end;
 except
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.
Run Code Online (Sandbox Code Playgroud)

  • @Downvoter:我必须同意RRUZ.通过MS Platform SDK可以很好地记录WMI.这里毫无疑问. (3认同)