Windows 2008/2003 マシンで最後の起動/再起動/再起動の日付と時刻を取得するにはどうすればよいですか?
コマンド プロンプトから「ネット統計」を使用できることはわかっていますが、Delphi 経由で行うにはどうすればよいでしょうか。
ありがとう。
WMIクラスのLastBootUpTime
プロパティを使用できます。このプロパティは(注:このプロパティの戻り値はUTC形式です)を返します。Win32_OperatingSystem
Date 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.
これがあなたが話していることを行う完全なコマンドラインアプリケーションです。これを変更して、外部関数呼び出しに依存せずに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
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.
GetTickCount
関数 ( MSDN を参照)は、システムが起動してから経過したミリ秒数を返すため、1000 で割って秒を取得し、60 000 で割って分などを取得します。
リンクしたトピックには、次のビットも含まれています。
コンピューターが起動してからの経過時間を取得するには、レジストリ キー HKEY_PERFORMANCE_DATA のパフォーマンス データの System Up Time カウンターを取得します。返される値は 8 バイトの値です。詳細については、「パフォーマンス カウンター」を参照してください。