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

时间:2011-06-29 13:19:43

标签: windows delphi uptime

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

我知道从命令提示符我们可以使用“net statistics”,但是如何通过Delphi实现呢?

感谢。

4 个答案:

答案 0 :(得分:7)

您可以使用Win32_OperatingSystem WMI类的LastBootUpTime属性,该属性返回Date 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.

答案 1 :(得分:2)

GetTickCount函数(参见MSDN)返回自系统启动以来经过的毫秒数,因此将其除以1000得到秒,得到60 000得到分钟等。< / p>

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

  

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

答案 2 :(得分:2)

这是一个完整的命令行应用程序,可以完成您所说的内容。 我修改了这个以避免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.

答案 3 :(得分:1)

以下是使用GetTickCount64(如果可用)的一些代码,如果不可用于计算系统启动的日期和时间,则会回退到GetTickCount。这不是一个完美的解决方案,因为只有Vista +支持GetTickCount64:如果您使用的是旧版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.