Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A function to synchronize your Local-System-Time to your Server-System-Time?

API Functions

A function to synchronize your Local-System-Time to your Server-System-Time?

by  agit72  Posted    (Edited  )
In some instances (e.g. auditing), it is important that the times of systems be in synch.
To this end, you can use the WinAPI to synchronize two NT machines using the following.
It is important to use the SetSystemtime and not SetLocalTime.
In Your main main program put this syntax:

=SyncDateTimeFromServer("\\YourServerName")

******************************************************


Function SyncDateTimeFromServer(tcserver)
Local lcserver, llretval
llretval = .F.
If vartype(tcserver) = "C" ;
and !empty(tcserver) then
lcserver = upper(alltrim(tcserver))
Else
* no server to synchronize with; return .F.
Return llretval
Endif
Declare integer NetRemoteTOD in netapi32 STRING@, INTEGER@
Declare RtlMoveMemory IN WIN32API as CopyMemory STRING@, INTEGER, INTEGER
Declare integer SetSystemTime in kernel32 STRING@

* NetRemoteTOD requires a NULL terminated UNICODE string
* The server name should be in the format e.g: \\Agit72, or \\MSP-Works.com

lcserver = strconv(strconv(tcserver,1),5)+chr(0)
lnptr = 0
* create string corresponding in size to the TIME_OF_DAY_INFO structure
lctimeofday = replicate(chr(0),48)
lcsystemtime = space(0)
nres = NetRemoteTOD(@lcserver,@lnptr)
Local lnyear, lnmonth, lndayofweek, lnday, lnhour, lnminute, lnsecond, lnmillsecond
If nres = 0 then
=CopyMemory(@lctimeofday, @lnptr, 48)
lnyear = DWordtoNum(substr(lctimeofday,41,4))
lnmonth = DWordtoNum(substr(lctimeofday,37,4))
lndayofweek = DWordtoNum(substr(lctimeofday,45,4))
lnday = DWordtoNum(substr(lctimeofday,33,4))
lnhour = DWordtoNum(substr(lctimeofday,9,4))
lnminute = DWordtoNum(substr(lctimeofday,13,4))
lnsecond = DWordtoNum(substr(lctimeofday,17,4))
lcsystemtime = NumtoWord(lnyear)+NumtoWord(lnmonth)+NumtoWord(lndayofweek)+;
NumtoWord(lnday)+NumtoWord(lnhour)+NumtoWord(lnminute)+NumtoWord(lnsecond)+NumtoWord(0)
nres = SetSystemTime(@lcsystemtime)
llretval = nres # 0
Else
=messagebox("Error " + transform(nres) +;
" occurred attempting to retrieve remote time",0+16+0,"Time Synchronization Error")
llretval = .F.
Endif
Return llretval
Endfunc
Function DWordtoNum
Lparameter tcDWORD
Local ln0,ln1,ln2,ln3
ln0=asc(subs(tcDWORD,1,1))
ln1=asc(subs(tcDWORD,2,1)) * (256)
ln2=asc(subs(tcDWORD,3,1)) * (256^2)
ln3=asc(subs(tcDWORD,4,1)) * (256^3)
Return ln3 + ln2 + ln1 + ln0
Endfunc

Function NumtoWord
Lparameter tnNum
lcresult = chr(0)+chr(0)
If tnNum < (2^15 - 1) then
lcresult = chr(mod(tnNum,256))+chr(int(tnNum/256))
Else
* not a valid number for a WORD value
Endif
Return lcresult
Endfunc
* Agit Permana (08561052915)
* Mitra Solusi Pratama, PT
* http://www.msp-works.com
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top