Advertisement
Linda-chan

Set local time by remote host time

Jul 2nd, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Type TIME_OF_DAY_INFO
  4.   tod_elapsedt As Long
  5.   tod_msecs As Long
  6.   tod_hours As Long
  7.   tod_mins As Long
  8.   tod_secs As Long
  9.   tod_hunds As Long
  10.   tod_timezone As Long
  11.   tod_tinterval As Long
  12.   tod_day As Long
  13.   tod_month As Long
  14.   tod_year As Long
  15.   tod_weekday As Long
  16. End Type
  17.  
  18. Declare Function NetRemoteTOD Lib "NetApi32" (ByRef UncServerName As Any, ByRef BufferPtr As Any) As Long
  19.  
  20. '====================================================================
  21. Public Sub Main()
  22.   asRegisterAppPath
  23.   InitCommonControls
  24.   If Not asIsInIDE Then asSetWindowResIconAll asGetThunderMainHandle(), IDI_APPICON
  25.  
  26.   Dim TOD As TIME_OF_DAY_INFO
  27.   Dim lpTOD As Long
  28.   Dim MachineName() As Byte
  29.   Dim RC As Long
  30.   Dim ST As SYSTEMTIME
  31.   Dim TXT As BStr
  32.   Dim Cmd As BStr
  33.  
  34.   Cmd = asTrimEx2(Command, True, True, True, True, True)
  35.   Cmd = UCase(Cmd)
  36.   If Cmd = "" Then Cmd = "."
  37.  
  38.   #If SIMULATE_ERROR Then
  39.     MachineName = StrConv(Cmd & NUL, vbFromUnicode)
  40.   #Else
  41.     MachineName = Cmd & NUL
  42.   #End If
  43.  
  44.   RC = NetRemoteTOD(MachineName(0), lpTOD)
  45.   If RC <> 0 Then
  46.     asLogError "NetRemoteTOD() failed." & CRLF & _
  47.                "Error " & asGetErrorNumber(RC) & ":" & CRLF & _
  48.                asGetAPIErrorMessage(RC)
  49.   Else
  50.     ' On return a pointer to the return information structure
  51.    ' TIME_OF_DAY_INFO is returned in the address pointed to
  52.    ' by BufferPtr.
  53.    If lpTOD = 0 Then
  54.       asLogError "NetRemoteTOD() returns NULL pointer."
  55.     Else
  56.       CopyMemory TOD.tod_elapsedt, ByVal lpTOD, Len(TOD)
  57.       GetSystemTime ST
  58.      
  59.       TXT = "Local date & time:  " & ST.wDay & "." & _
  60.                                      Format(ST.wMonth, "00") & "." & _
  61.                                      Format(ST.wYear, "0000") & " " & _
  62.                                      ST.wHour & ":" & _
  63.                                      Format(ST.wMinute, "00") & ":" & _
  64.                                      Format(ST.wSecond, "00") & "." & _
  65.                                      ST.wMilliseconds & CRLF & _
  66.             "Remote date & time: " & TOD.tod_day & "." & _
  67.                                      Format(TOD.tod_month, "00") & "." & _
  68.                                      Format(TOD.tod_year, "0000") & " " & _
  69.                                      TOD.tod_hours & ":" & _
  70.                                      Format(TOD.tod_mins, "00") & ":" & _
  71.                                      Format(TOD.tod_secs, "00") & "." & _
  72.                                      TOD.tod_hunds & CRLF & _
  73.             "Remote system name: " & Cmd & CRLFCRLF & _
  74.             "Please note both times are in UTC format."
  75.       Debug.Print TXT
  76.      
  77.       If Not asSetPrivilegeForProcess(SE_SYSTEMTIME_NAME, True) Then
  78.         asLogWarning "Failed to set SE_SYSTEMTIME_NAME privilege."
  79.       End If
  80.      
  81.       ST.wDay = TOD.tod_day
  82.       ST.wDayOfWeek = 0
  83.       ST.wHour = TOD.tod_hours
  84.       ST.wMilliseconds = TOD.tod_hunds
  85.       ST.wMinute = TOD.tod_mins
  86.       ST.wMonth = TOD.tod_month
  87.       ST.wSecond = TOD.tod_secs
  88.       ST.wYear = TOD.tod_year
  89.      
  90.       If SetSystemTime(ST) = C_FALSE Then
  91.         asLogError "SetSystemTime() failed." & CRLFCRLF & TXT
  92.       Else
  93.         asLogInfo "System time was updated successfully." & CRLFCRLF & TXT
  94.       End If
  95.     End If
  96.   End If
  97. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement