最終シャットダウン日時を取得          <TOP>


GetTimeZoneInformation タイムゾーン情報を取得
SystemTimeToTzSpecificLocalTime 世界協定時刻(UTC)を、指定したタイムゾーンの対応するローカル時刻に変換
FileTimeToSystemTime ファイルタイムをシステムタイムに変換
RegOpenKeyEx レジストリのキーのハンドルを確保
RegQueryValueEx レジストリの値を取得
RegCloseKey レジストリのハンドルを解放
GetComputerName コンピュータの名前を文字列として取得
lstrlenW (Null文字で終了する)UNICODE文字列の文字数を返す
 

 

'================================================================
'= 最終シャットダウン日時を取得
'=    (SystemTimeToTzSpecificLocalTime.bas)
'================================================================
#include "Windows.bi"

#define MAX_COMPUTERNAME 16
#define REG_BINARY 3                    'バイナリデータ
#define HKEY_LOCAL_MACHINE -2147483646  'PCを利用するユーザーに共通の設定情報
#define ERROR_SUCCESS &H0               '正常終了の戻り値を示す
#define STANDARD_RIGHTS_READ &H20000    '(READ_CONTROL)オブジェクトのセキュリティ記述子の読み取り許可
#define KEY_QUERY_VALUE &H1             'サブキーデータを問い合わせるためのアクセス権
#define KEY_ENUMERATE_SUB_KEYS &H8      'サブキーの列挙を許可
#define KEY_NOTIFY &H10                 '変更の通知を許可
#define SYNCHRONIZE &H100000            '同期をとる

Type SYSTEMTIME
    wYear         As Integer            '年
    wMonth        As Integer            '月(1:1月  2:2月 ...)
    wDayOfWeek    As Integer            '曜(0:日曜 1:月曜 ...)
    wDay          As Integer            '日(1:1日  2:2日 ...)
    wHour         As Integer            '時
    wMinute       As Integer            '分
    wSecond       As Integer            '秒
    wMilliseconds As Integer            'ミリ秒
End Type

Type FILETIME
    dwLowDateTime  As Long              '下位32ビット値
    dwHighDateTime As Long              '上位32ビット値
End Type


Type TIME_ZONE_INFORMATION
    Bias             As Long
    StandardName(32) As Integer
    StandardDate     As SYSTEMTIME
    StandardBias     As Long
    DaylightName(32) As Integer
    DaylightDate     As SYSTEMTIME
    DaylightBias     As Long
End Type

' タイムゾーン情報を取得
Declare Function Api_GetTimeZoneInformation& Lib "kernel32" Alias "GetTimeZoneInformation" (lpTimeZoneInformation As TIME_ZONE_INFORMATION)

' 世界協定時刻(UTC)を、指定したタイムゾーンの対応するローカル時刻に変換
Declare Function Api_SystemTimeToTzSpecificLocalTime& Lib "kernel32" Alias "SystemTimeToTzSpecificLocalTime" (lpTimeZoneInformation As TIME_ZONE_INFORMATION, lpUniversalTime As SYSTEMTIME, lpLocalTime As SYSTEMTIME)

' ファイルタイムをシステムタイムに変換
Declare Function Api_FileTimeToSystemTime& Lib "kernel32" Alias "FileTimeToSystemTime" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME)

' レジストリのキーのハンドルを確保
Declare Function Api_RegOpenKeyEx& Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey&, ByVal lpSubKey$, ByVal ulOptions&, ByVal samDesired&, phkResult&)

' レジストリの値を取得
Declare Function Api_RegQueryValueEx& Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey&, ByVal lpvName$, ByVal lpReserved&, lpType&, lpData As Any, lpcbData&)

' レジストリのハンドルを解放
Declare Function Api_RegCloseKey& Lib "advapi32" Alias "RegCloseKey" (ByVal hKey&)

' コンピュータの名前を文字列として取得
Declare Function Api_GetComputerName& Lib "Kernel32" Alias "GetComputerNameA" (ByVal lpBuffer$, nSize&)

' (Null文字で終了する)UNICODE文字列の文字数を返す
Declare Function Api_lstrlenW& Lib "Kernel32" Alias "lstrlenW" (ByVal lpString&)

Var Shared Edit1 As Object
Var Shared Text1 As Object
Var Shared Check1 As Object
Var Shared Button1 As Object

Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14
Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14
Check1.Attach GetDlgItem("Check1") : Check1.SetFontSize 14
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

'================================================================
'=
'================================================================
Declare Function GetFileToSystemDate(ft As FILETIME, bIncludeTime As Integer) As String
Function GetFileToSystemDate(ft As FILETIME, bIncludeTime As Integer) As String
    Var buff As String
    Var st As SYSTEMTIME    'system(UNC)time
    Var lt As SYSTEMTIME    'local time
    Var tz As TIME_ZONE_INFORMATION
    Var Ret As Long
  
    If Api_FileTimeToSystemTime(ft, st) Then
        Ret = Api_GetTimeZoneInformation(tz)
        Ret = Api_SystemTimeToTzSpecificLocalTime(tz, st, lt)

        buff = Right$(Str$(10000 + lt.wYear), 4) & "年" & Right$(Str$(100 + lt.wMonth), 2) & "月" & Right$(Str$(100 + lt.wDay), 2) & "日"

        If bIncludeTime Then
            buff = buff & " " & Right$(Str$(100 + lt.wHour), 2) & "時" & Right$(Str$(100 + lt.wMinute), 2) & "分" & Right$(Str$(100 + lt.wSecond), 2) & "秒"
        End If
      
        GetFileToSystemDate = buff
    Else
        GetFileToSystemDate = ""
    End If
End Function

'================================================================
'=
'================================================================
Declare Function GetLastSystemShutdown(bIncludeTime As Integer) As String
Function GetLastSystemShutdown(bIncludeTime As Integer) As String
    Var hKey As Long
    Var sKey As String
    Var sValueName As String
    Var ft As FILETIME
    Var cbData As Long
    Var Ret As Long
   
    sKey = "System\CurrentControlSet\Control\Windows"
    sValueName = "ShutdownTime"
   
    If Api_RegOpenKeyEx(HKEY_LOCAL_MACHINE, sKey, 0, (STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or EY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE), hKey) = ERROR_SUCCESS Then
        If hKey <> 0 Then
            cbData = Len(ft)
            If Api_RegQueryValueEx(hKey, sValueName, 0, REG_BINARY, ft, cbData) = ERROR_SUCCESS Then
                GetLastSystemShutdown = GetFileToSystemDate(ft, bIncludeTime)
            End If
        Ret = Api_RegCloseKey(hKey)
        End If
    End If
End Function

'================================================================
'=
'================================================================
Declare Function TrimNull(startstr As String) As String
Function TrimNull(startstr As String) As String
   TrimNull = Left$(startstr, Api_lstrlenW(StrAdr(startstr)))
End Function

'================================================================
'=
'================================================================
Declare Function GetLocalComputerName() As String
Function GetLocalComputerName() As String
    Var tmp As String

    tmp = Space$(MAX_COMPUTERNAME)

    If Api_GetComputerName(tmp, Len(tmp)) <> 0 Then
        GetLocalComputerName = TrimNull(tmp)
    End If
End Function

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
   Text1.SetWindowText GetLocalComputerName() & " の最終シャットダウン日時"
   Edit1.SetWindowText ""
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
   Var buff As String
   Var bIncludeTime As Integer
   
   bIncludeTime = Check1.GetCheck
   buff = GetLastSystemShutdown(bIncludeTime)
   Edit1.SetWindowText buff
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End