BrewGuru99
January 18th, 2000, 11:13 PM
How can I synchronize the clock of the computer the vb app is on with the clock of another computer located on the same lan?
I don't know anything about networks, so I don't know how or what to do to "talk" with another computer. I know the ip address of the computer I want to sync with if that helps.
Brewguru99
Chris Eastwood
January 19th, 2000, 03:46 AM
Here's some simple code that shows how to get the time on a remote server (using a standard UNC name, eg '\\yourservername')
The value is returned in a TIME_OF_DAY structure so you can get any part of the structure you like :
option Explicit
'
'
private Declare Function NetRemoteTOD Lib "NETAPI32.DLL" _
(byval server as string, buffer as Any) as Long
'
private Declare Function NetApiBufferFree Lib "NETAPI32.DLL" _
(byval buffer as Long) as Long
'
private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _
(hpvDest as Any, hpvSource as Any, byval cbCopy as Long)
'
private Type TIME_OF_DAY
t_elapsedt as Long
t_msecs as Long
t_hours as Long
t_mins as Long
t_secs as Long
t_hunds as Long
t_timezone as Long
t_tinterval as Long
t_day as Long
t_month as Long
t_year as Long
t_weekday as Long
End Type
'
private Sub Command1_Click()
Dim t as TIME_OF_DAY
Dim tPtr as Long
Dim res as Long
Dim szServer as string
Dim days as date
Dim todays as date
'
' Replace \\earth with your NT server that you want to get the time from
'
szServer = StrConv("\\earth", vbUnicode) 'Convert the server name to unicode
res = NetRemoteTOD(szServer, tPtr) 'You could also pass vbNullString for the server name
If res = 0 then
CopyMemory t, byval tPtr, len(t) 'Copy the pointer returned to a TIME_OF_DAY structure
days = DateSerial(70, 1, 1) + (t.t_elapsedt / 60 / 60 / 24) 'Convert the elapsed time since 1/1/70 to a date
days = days - (t.t_timezone / 60 / 24) 'Adjust for TimeZone differences
'
'get local computer information for comparison
'
todays = DateSerial(70, 1, 1) + (DateDiff("s", DateSerial(70, 1, 1), Now()) / 60 / 60 / 24)
me.Cls
print DateDiff("s", DateSerial(70, 1, 1), Now()), todays, t.t_elapsedt, days
NetApiBufferFree (tPtr) 'Free the memory at the pointer
'
else
MsgBox "error occurred Call NetRemoteTOD: " & res, vbOKOnly, "NetRemoteTOD"
'error 53: cannot find server
End If
End Sub
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
BrewGuru99
January 19th, 2000, 03:08 PM
Thanks! I see how to do this now, the only problem is I get the error "Cant find DLL Entry point NetRemoteTOD in netapi32.dll"!!
What version of the dll do I need? Mine is 4.10.1998.
Where can I find a resource of up to date DLL's and api listings (I can't find anything that says what is available in my netapi32)?
Brewguru99
Aaron Young
January 19th, 2000, 03:25 PM
Is this an NT Machine?
If so, another way would be:
private Sub SyncTime(byval sServerName as string)
Shell "NET time \\" & sServerName & " /set /Y", vbHide
End Sub
Aaron Young
Analyst Programmer
ajyoung@pressenter.com
aarony@redwingsoftware.com
BrewGuru99
January 19th, 2000, 03:50 PM
WOW Thanks!
I tried this on my win 98 machine as well as my nt machine, and it works on both just fine!
Thanks again!
Brewguru99