WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DllCall Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Using DllStructAlloc

Keywords:  DllStructAlloc DllCall UTC FileTime SystemTime

Get the last time the current system was shutdown in an easily readable form using the Win32 API function FileTimeToSystemTime to convert from the registry's FILETIME to SYSTEMTIME.

The SYSTEMTIME structure is something like this:

typedef struct _SYSTEMTIME {
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, FAR *LPSYSTEMTIME;

This structure is passed to the FileTimeToSystemTime function as a memory pointer.

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; TimeAdjustCurrentZone
  ;; Input - WIL YMDHMS UTC time.
  ;;
  ;; Returns - WIL YMDHMS time converted to local
  ;;           time.
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  #DefineFunction TimeAdjustCurrentZone(strUtcTime)
     nActiveBias =  RegQueryDword(@regmachine,"SYSTEM\CurrentControlSet\Control\TimeZoneInformation[ActiveTimeBias]", 0)
     If nActiveBias >= 0
        Return TimeSubtract(strUtcTime,"0000:00:00:00:":nActiveBias:":00")
     Else
        Return TimeAdd(strUtcTime,"0000:00:00:00:":Abs(nActiveBias):":00")
  #EndFunction
  
  ;; Retrieve the system's last shutdown time from the system registry.
  ;; The time is represended by a space delimited, little endian, hexidecimal, 64-bit number.
  RegShutdownTime = RegQueryBin(@regmachine,"System\CurrentControlSet\Control\Windows[ShutdownTime]")
  RegShutdownTime = StrReplace(RegShutdownTime, ' ', '')  ; Remove spaces.
  hFileTime = BinaryAlloc(8)
  
  ;; Copy the time into a binary buffer
  BinaryPokeHex(hFileTime, 0, RegShutdownTime)
  
  ;; Allocate a C/C++ SYSTEMTIME structure.
  hSysTime = DllStructAlloc('WORD:wYear WORD:wMonth WORD:wDayOfWeek WORD:wDay WORD:wHour WORD:wMinute WORD:wSecond WORD:wMilliseconds')
  
  ;; Fill the SYSTEMTIME structure using the "FileTimeToSystemTime" Win32 function.
  bResult = DllCall('Kernel32.dll',long:'FileTimeToSystemTime', lpBinary:hFileTime, lpstruct:hSysTime)
  BinaryFree(hFileTime)
  
  ;; Convert systemtime to WIL YMDHMS time.
  strYMS = StrFixCharsL(DllStructPeek(hSysTime ,'wYear'),'0',4):':'
  strYMS := StrFixCharsL(DllStructPeek(hSysTime ,'wMonth'),'0',2):':'
  strYMS := StrFixCharsL(DllStructPeek(hSysTime ,'wDay'),'0',2):':'
  strYMS := StrFixCharsL(DllStructPeek(hSysTime ,'wHour'),'0',2):':'
  strYMS := StrFixCharsL(DllStructPeek(hSysTime ,'wMinute'),'0',2):':'
  strYMS := StrFixCharsL(DllStructPeek(hSysTime ,'wSecond'),'0',2)
  DllStructFree(hSysTime)
  
  ; Convert from UTC to local time and format it.
  strYMS = TimeAdjustCurrentZone(strYMS)
  strTime = TimeFormat(strYms, "dddd', 'MMMM dd', 'yyyy hh':'mm':'ss t")
  
  Pause('Shutdown Time', strTime)
  Exit

Article ID:   W15133
File Created: 2020:11:19:15:53:32
Last Updated: 2020:11:19:15:53:32