Visual Basic Programming Code Examples
Visual Basic > Date Time Code Examples
Get Date-Time of a Network machine
Get Date-Time of a Network machine
To return the date and time on a remote machine use the following routine:
Option Explicit
Private Declare Function NetRemoteTOD Lib "netapi32.dll" (yServer As Any, pBuffer As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32.dll" (ByVal pBuffer As Long) As Long
Private Declare Sub CopyMem Lib "kernel32.dll" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Private Type TIME_OF_DAY_INFO
telapsed As Long
tmsecs As Long
thours As Long
tmins As Long
tsecs As Long
thunds As Long
ttimezone As Long
ttinterval As Long
tday As Long
tmonth As Long
tyear As Long
tweekday As Long
End Type
'Purpose : Return the time of a remote or local machine
'Inputs : [sServerName] The name of the machine to return the time of.
' If unspecified returns time on local machine.
'Outputs : The date and time on the specified machine
Function MachineTimeGet(Optional sServerName As String) As Date
Dim tTime As TIME_OF_DAY_INFO
Dim lRet As Long, lpBuffer As Long
Dim abServer() As Byte
If Len(sServerName) Then
'Check the syntax of the sServerName string
If InStr(sServerName, "\\") = 1 Then
abServer = sServerName & vbNullChar
Else
abServer = "\\" & sServerName & vbNullChar
End If
lRet = NetRemoteTOD(abServer(0), lpBuffer)
Else
'Local machine
lRet = NetRemoteTOD(vbNullString, lpBuffer)
End If
CopyMem tTime, ByVal lpBuffer, Len(tTime)
If lpBuffer Then
Call NetApiBufferFree(lpBuffer)
End If
MachineTimeGet = DateSerial(tTime.tyear, tTime.tmonth, tTime.tday) + TimeSerial(tTime.thours, tTime.tmins - tTime.ttimezone, tTime.tsecs)
End Function