INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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!

*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.

Jobs

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:

=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

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

My Archive

Resources

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