dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: API NetRemoteTOD Problem

  1. #1
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353

    API NetRemoteTOD Problem

    I've got to be doing something wrong here, but just can't see it. Here is the issue. I have a class to get the datetime off a remote server. This will be used for an employee timeclock application. The problem I run into, is when the class method is called after 7:00 PM, the time is incremented 24 hours.

    Here is the output of a test app I ran yesterday. It simply polls the datetime from my machine (Date.Now) and 2 remote servers using my class every 15 minutes. I polled 2 remote servers to try and eliminate any possible server config problem. Note the day changes after 19:00 on the remote servers. Checking the time locally on the server does not reflect this. We are in CST time zone.

    Code:
    Local Time: 10/27/2008 19:15:12 Server1 Time: 10/28/2008 19:15:13 Server2 Time: 10/28/2008 19:15:12
    Local Time: 10/27/2008 19:00:12 Server1 Time: 10/28/2008 19:00:13 Server2 Time: 10/28/2008 19:00:12
    Local Time: 10/27/2008 18:45:12 Server1 Time: 10/27/2008 18:45:13 Server2 Time: 10/27/2008 18:45:12
    Local Time: 10/27/2008 18:30:12 Server1 Time: 10/27/2008 18:30:13 Server2 Time: 10/27/2008 18:30:12
    Local Time: 10/27/2008 18:15:12 Server1 Time: 10/27/2008 18:15:13 Server2 Time: 10/27/2008 18:15:12
    Local Time: 10/27/2008 18:00:12 Server1 Time: 10/27/2008 18:00:13 Server2 Time: 10/27/2008 18:00:13
    Here is the class and method I call:

    Code:
    Public Class ServerTime
    Private Declare Unicode Function NetRemoteTOD Lib "netapi32" ( _
            <MarshalAs(UnmanagedType.LPWStr)> ByVal ServerName As String, _
            ByRef BufferPtr As IntPtr) As Integer
    
        Private Declare Function NetApiBufferFree Lib _
            "netapi32" (ByVal Buffer As IntPtr) As Integer
    
        Structure TIME_OF_DAY_INFO
            Dim tod_elapsedt As Integer
            Dim tod_msecs As Integer
            Dim tod_hours As Integer
            Dim tod_mins As Integer
            Dim tod_secs As Integer
            Dim tod_hunds As Integer
            Dim tod_timezone As Integer
            Dim tod_tinterval As Integer
            Dim tod_day As Integer
            Dim tod_month As Integer
            Dim tod_year As Integer
            Dim tod_weekday As Integer
        End Structure
        Dim pstrservername As String = ""
    
        Public Sub New(ByVal strServername As String)
            pstrservername = strServername
        End Sub
    
    Public Function GetRemoteDateTime() As Date
            Dim PROCNAME As String = "GetRemoteDateTime"
            Dim dDate As Date = Nothing
            Try
                Dim iRet As Integer
                Dim ptodi As IntPtr
                Dim todi As TIME_OF_DAY_INFO
                If pstrservername = String.Empty Then
                    Err_Log("No remote system name supplied for " & PROCNAME, PROCNAME, ClinkLog.LogMsgPriority.HighError)
                    Exit Function
                End If
                pstrservername = pstrservername & vbNullChar
                iRet = NetRemoteTOD(pstrservername, ptodi)
                If iRet = 0 Then
                    todi = CType(Marshal.PtrToStructure(ptodi, GetType(TIME_OF_DAY_INFO)),  _
                      TIME_OF_DAY_INFO)
                    NetApiBufferFree(ptodi)
                    dDate = CDate(DateSerial(todi.tod_year, todi.tod_month, todi.tod_day).ToString("MM/dd/yyyy") & " " & _
                                TimeSerial(todi.tod_hours, todi.tod_mins - todi.tod_timezone, todi.tod_secs).ToString("HH:mm:ss"))
                Else
                    Err_Log("Error retrieving time from " & pstrservername & " for " & PROCNAME, PROCNAME, ClinkLog.LogMsgPriority.HighError)
                End If
            Catch ex As Exception
                Err_Log(ex, PROCNAME, ClinkLog.LogMsgPriority.HighError)
            End Try
            Return dDate
        End Function
    
    End Class
    Can anyone point me in the right direction?

    ...joe

  2. #2
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353
    I found the problem:

    Code:
    dDate = CDate(DateSerial(todi.tod_year, todi.tod_month, todi.tod_day).ToString("MM/dd/yyyy") & " " & _
                                TimeSerial(todi.tod_hours, todi.tod_mins - todi.tod_timezone, todi.tod_secs).ToString("HH:mm:ss"))
    In the sample code I modified the timezone was being subtracted in the TimeSerial, but the Date and Time were seperate strings being appended. After 7:00 PM local time, UTC was returning the next day. By subtracting only from the TimeSerial it was not correcting the day. Using:

    Code:
     Private Const STARTDATE As Date = #1/1/1970#
    dDate = STARTDATE.AddSeconds(todi.tod_elapsedt)
    dDate = dDate.AddMinutes(-todi.tod_timezone)
    appears to solve the issue. That's what I get for copying and pasting from the internet

    ...joe

  3. #3
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Quote Originally Posted by joewmaki
    That's what I get for copying and pasting from the internet
    I hear ya....

    Thanks for coming back and posting your solution. Hopefully it will help others with the same or similiar problem.

Similar Threads

  1. Problem with wininet api - FTPPutFile for IPV6
    By karthik_sc in forum API Programming
    Replies: 0
    Last Post: 10-28-2008, 06:01 AM
  2. Problem with the windows API
    By satheeshkumar in forum C++
    Replies: 0
    Last Post: 06-27-2008, 03:29 AM
  3. Replies: 0
    Last Post: 12-13-2001, 01:06 PM
  4. How to Use SendMessageCallBack API?
    By Armando Cardenas in forum VB Classic
    Replies: 10
    Last Post: 07-12-2000, 02:22 PM
  5. How to Use SendMessageCallBack API?
    By Armando Cardenas in forum VB Classic
    Replies: 0
    Last Post: 07-10-2000, 02:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center