Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: FoxPro FAQ

API Functions

A function to synchronize your Local-System-Time to your Server-System-Time? by agit72
Posted: 27 Apr 03 (Edited 27 Apr 03)

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:



Function SyncDateTimeFromServer(tcserver)
    Local lcserver, llretval
    llretval = .F.
    If vartype(tcserver) = "C" ;
            and !empty(tcserver) then
        lcserver = upper(alltrim(tcserver))
        * no server to synchronize with; return .F.
        Return llretval
    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)+;
        nres = SetSystemTime(@lcsystemtime)
        llretval = nres # 0
        =messagebox("Error " + transform(nres) +;
            " occurred attempting to retrieve remote time",0+16+0,"Time Synchronization Error")
        llretval = .F.
    Return llretval
Function DWordtoNum
    Lparameter tcDWORD
    Local ln0,ln1,ln2,ln3
    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

Function NumtoWord
    Lparameter tnNum
    lcresult = chr(0)+chr(0)
    If tnNum < (2^15 - 1) then
        lcresult = chr(mod(tnNum,256))+chr(int(tnNum/256))
        * not a valid number for a WORD value
    Return lcresult
* Agit Permana (08561052915)
* Mitra Solusi Pratama, PT
* http://www.msp-works.com

Back to Microsoft: FoxPro FAQ Index
Back to Microsoft: FoxPro Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close