Advertisement
Linda-chan

Set local time by remote host time v2

Jul 3rd, 2013
105
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. Private LogBuffer As BStr
  21.  
  22. '====================================================================
  23. Public Sub Main()
  24.   asRegisterAppPath
  25.   InitCommonControls
  26.   If Not asIsInIDE Then asSetWindowResIconAll asGetThunderMainHandle(), IDI_APPICON
  27.  
  28.   Dim Cmd As BStr
  29.   Dim MachineNames() As BStr
  30.   Dim TMP As Variant
  31.   Dim Success As Boolean
  32.  
  33.   Cmd = asTrimEx(Command)
  34.   Cmd = asDestroyDoubleSpaces(Cmd)
  35.   Cmd = UCase(Cmd)
  36.  
  37.   If Cmd = "" Then
  38.     ShowUsage
  39.     Exit Sub
  40.   End If
  41.  
  42.   MachineNames = Split(Cmd, " ")
  43.   Success = False
  44.  
  45.   For Each TMP In MachineNames
  46.     TMP = asTrimEx2(CStr(TMP), True, True, True, True, True)
  47.     If TMP <> "" Then
  48.       If TryToFixTime(TMP) Then
  49.         Success = True
  50.         Exit For
  51.       End If
  52.     End If
  53.   Next TMP
  54.  
  55.   If Not Success Then LogEvent "All tries were failed."
  56.   DumpLog Success
  57. End Sub
  58.  
  59. '====================================================================
  60. Private Sub LogEvent(ByVal Text As BStr)
  61.   Dim Lines() As BStr
  62.   Dim TMP As Variant
  63.  
  64.   Lines = Split(Text, CRLF)
  65.  
  66.   For Each TMP In Lines
  67.     TMP = asTrimEx(CStr(TMP))
  68.     If TMP <> "" Then
  69.       LogBuffer = LogBuffer & "[" & asSystemTimeToStringTime2(Now) & "] " & _
  70.                               "[" & asSystemTimeToStringDate2(Now, False) & "] " & _
  71.                               TMP & CRLF
  72.     End If
  73.   Next TMP
  74. End Sub
  75.  
  76. Private Sub DumpLog(ByVal Success As Boolean)
  77.   If Success Then
  78.     asLogInfo asTrimCRLF(LogBuffer)
  79.   Else
  80.     asLogError asTrimCRLF(LogBuffer)
  81.   End If
  82. End Sub
  83.  
  84. '====================================================================
  85. Private Function TryToFixTime(ByVal sMachineName As BStr) As Boolean
  86.   Dim TOD As TIME_OF_DAY_INFO
  87.   Dim lpTOD As Long
  88.   Dim MachineName() As Byte
  89.   Dim RC As Long
  90.   Dim ST As SYSTEMTIME
  91.   Dim TXT As BStr
  92.  
  93.   ' На всякий пожарный...
  94.  TryToFixTime = False
  95.  
  96.   LogEvent "Trying machine " & sMachineName & "."
  97.  
  98.   #If SIMULATE_ERROR Then
  99.     LogEvent "Error simulation mode."
  100.     MachineName = StrConv(sMachineName & NUL, vbFromUnicode)
  101.   #Else
  102.     MachineName = sMachineName & NUL
  103.   #End If
  104.  
  105.   RC = NetRemoteTOD(MachineName(0), lpTOD)
  106.   If RC <> 0 Then
  107.     LogEvent "NetRemoteTOD() failed. " & _
  108.              "Error " & asGetErrorNumber(RC) & ": " & _
  109.              asGetAPIErrorMessage(RC)
  110.   Else
  111.     ' On return a pointer to the return information structure
  112.    ' TIME_OF_DAY_INFO is returned in the address pointed to
  113.    ' by BufferPtr.
  114.    If lpTOD = 0 Then
  115.       LogEvent "NetRemoteTOD() returns NULL pointer."
  116.     Else
  117.       CopyMemory TOD.tod_elapsedt, ByVal lpTOD, Len(TOD)
  118.       GetSystemTime ST
  119.      
  120.       TXT = "Local date & time:  " & ST.wDay & "." & _
  121.                                      Format(ST.wMonth, "00") & "." & _
  122.                                      Format(ST.wYear, "0000") & " " & _
  123.                                      ST.wHour & ":" & _
  124.                                      Format(ST.wMinute, "00") & ":" & _
  125.                                      Format(ST.wSecond, "00") & "." & _
  126.                                      ST.wMilliseconds & CRLF & _
  127.             "Remote date & time: " & TOD.tod_day & "." & _
  128.                                      Format(TOD.tod_month, "00") & "." & _
  129.                                      Format(TOD.tod_year, "0000") & " " & _
  130.                                      TOD.tod_hours & ":" & _
  131.                                      Format(TOD.tod_mins, "00") & ":" & _
  132.                                      Format(TOD.tod_secs, "00") & "." & _
  133.                                      TOD.tod_hunds & CRLF & _
  134.             "Please note both times are in UTC format."
  135.       Debug.Print TXT
  136.       LogEvent TXT
  137.      
  138.       If Not asSetPrivilegeForProcess(SE_SYSTEMTIME_NAME, True) Then
  139.         LogEvent "Failed to set SE_SYSTEMTIME_NAME privilege."
  140.       End If
  141.      
  142.       ST.wDay = TOD.tod_day
  143.       ST.wDayOfWeek = 0
  144.       ST.wHour = TOD.tod_hours
  145.       ST.wMilliseconds = TOD.tod_hunds
  146.       ST.wMinute = TOD.tod_mins
  147.       ST.wMonth = TOD.tod_month
  148.       ST.wSecond = TOD.tod_secs
  149.       ST.wYear = TOD.tod_year
  150.      
  151.       If SetSystemTime(ST) = C_FALSE Then
  152.         LogEvent "SetSystemTime() failed."
  153.       Else
  154.         LogEvent "System time was updated successfully."
  155.         TryToFixTime = True
  156.       End If
  157.     End If
  158.   End If
  159. End Function
  160.  
  161. '====================================================================
  162. Private Sub ShowUsage()
  163.   MsgBox "Usage: " & App.EXEName & "[.EXE] HostName [HostName [HostName [...]]]", vbInformation
  164. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement