CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 1999
    Location
    CA
    Posts
    91

    Sync'ing Clocks over a lan

    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

  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Sync'ing Clocks over a lan

    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

  3. #3
    Join Date
    Oct 1999
    Location
    CA
    Posts
    91

    Re: Sync'ing Clocks over a lan

    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

  4. #4
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Sync'ing Clocks over a lan

    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
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  5. #5
    Join Date
    Oct 1999
    Location
    CA
    Posts
    91

    Re: Sync'ing Clocks over a lan

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured