Windows上次启动date和时间使用Delphi

如何获取Windows 2008/2003机器上最后一次启动/重启/重启的date和时间?

我知道从命令提示符我们可以使用“networking统计”,但如何通过delphi做到这一点?

谢谢。

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

    检查此示例应用程序

     {$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. 

    GetTickCount函数(参见MSDN )返回自系统启动以来经过的毫秒数,所以将它除以1000得到秒数,用60 000得到分钟等等。

    我链接的主题也包含这一点:

    要获取计算机启动以来的时间,请在注册表项HKEY_PERFORMANCE_DATA中的性能数据中检索系统启动时间计数器。 返回的值是一个8字节的值。 有关更多信息,请参阅性能计数器。

    这是一个完整的命令行应用程序,它可以完成你正在谈论的任务。 我已经修改此以避免GetTickCount溢出问题,而不依赖于外部函数调用。

    示例输出:

     Windows was last rebooted at: 06/29/2011 9:22:47 AM 

    玩的开心!

     program lastboottime; {$APPTYPE CONSOLE} uses SysUtils, Windows; function UptimeInDays: double; const c_SecondsInADay = 86400; var cnt, freq: Int64; begin QueryPerformanceCounter(cnt); QueryPerformanceFrequency(freq); Result := (cnt / freq) / c_SecondsInADay; end; function LastBootTime: TDateTime; begin Result := Now() - UptimeInDays; end; begin try WriteLn('Windows was last rebooted at: ' + DateTimeToStr(LastBootTime)); ReadLn; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. 

    下面是一些使用GetTickCount64如果可用)的代码,如果不能计算系统启动的日期和时间,则退回到GetTickCount 。 这不是一个完美的解决方案,因为GetTickCount64仅在Vista +上受支持:如果您在较旧的Windows上,计数器会每49天返回到0。

     program Project29; {$APPTYPE CONSOLE} uses SysUtils, Windows; type TGetTickCount64 = function : Int64; stdcall; var H_K32: HMODULE; Tick64Proc: TGetTickCount64; function BootTime: TDateTime; var UpTime: Int64; Seconds, Minutes, Hours: Int64; begin if H_K32 = 0 then begin H_K32 := LoadLibrary(kernel32); if H_K32 = 0 then RaiseLastOSError else begin Tick64Proc := GetProcAddress(H_K32, 'GetTickCount64'); end; end; if Assigned(Tick64Proc) then UpTime := Tick64Proc else UpTime := GetTickCount; Result := Now - EncodeTime(0, 0, 0, 1) * UpTime; end; begin WriteLn(DateTimeToStr(BootTime)); ReadLn; end.