Get server time using NetRemoteTOD 
Author Message
 Get server time using NetRemoteTOD

Has anyone got this to work under VB 6?

If so please post I'd very much appreciate it.

Eddie



Fri, 10 Aug 2001 03:00:00 GMT  
 Get server time using NetRemoteTOD
Hi Edward,

Before you go and try the code below read this: NetRemoteTOD() API is
supported only on NT3.1 and later (no Wndows9x). Also, it can retrieve
the server time ONLY from NT server.

If you want your code to work on Wndows9x, you should look at the NET
TIME Dos prompt command. If you want a working code for it E-mail me.

With that said, here is the code:

1. Start a new Project
2. Place a Command1 button on Form1
3. Copy the following code to Form1:

Private Type TIME_OF_DAY_INFO
    tod_elapsedt As Long
    tod_msecs As Long
    tod_hours As Long
    tod_mins As Long
    tod_secs As Long
    tod_hunds As Long
    tod_timezone As Long
    tod_tinterval As Long
    tod_day As Long
    tod_month As Long
    tod_year As Long
    tod_weekday As Long
End Type
Private ti As TIME_OF_DAY_INFO

Private Type HiLoInt
    loInt As Integer
    hiInt As Integer
End Type

Dim TwoInt As HiLoInt

Private Type LongType
    l As Long
End Type

Dim lLong As LongType

Private Declare Function NetRemoteTOD Lib "netapi32.dll" (ServerName As
Any, buffer As Any) As Long

Private Declare Function DataFromPtr Lib "kernel32" Alias "lstrcpynW"
(RetVal As Any, ByVal Ptr As Long, ByVal nCharCount As Long) As Long

Private Declare Function NetAPIBufferFree Lib "netapi32.dll" Alias
"NetApiBufferFree" (ByVal Ptr As Long) As Long

Private Sub Command1_Click()
    Dim sName As String
    Dim pbServer() As Byte
    Dim ptmpBuffer As Long

    Dim lRetVal As Long

    'Set the NT server name
    sName = "\\ServerName"

    'Convert the name to a Unicode byte array
    pbServer = sName & vbNullChar

    'Call the NetRemoteTOD function
    lRetVal = NetRemoteTOD(pbServer(0), ptmpBuffer)

    If lRetVal <> 0 Then
        'Function failed
        Exit Sub
    End If

    'Extract the information into a TIME_OF_DAY_INFO structure

    'Get first element of the structure
    lRetVal = DataFromPtr(ti.tod_elapsedt, ptmpBuffer, 4)
    'Get second element of the structure
    lRetVal = DataFromPtr(ti.tod_msecs, ptmpBuffer + 4, 4)
    'Get third element of the structure
    lRetVal = DataFromPtr(ti.tod_hours, ptmpBuffer + 8, 4)
    '....
    lRetVal = DataFromPtr(ti.tod_mins, ptmpBuffer + 12, 4)
    lRetVal = DataFromPtr(ti.tod_secs, ptmpBuffer + 16, 4)
    lRetVal = DataFromPtr(ti.tod_hunds, ptmpBuffer + 20, 4)
    lRetVal = DataFromPtr(ti.tod_timezone, ptmpBuffer + 24, 4)
    lRetVal = DataFromPtr(ti.tod_tinterval, ptmpBuffer + 28, 4)
    lRetVal = DataFromPtr(ti.tod_day, ptmpBuffer + 32, 4)
    lRetVal = DataFromPtr(ti.tod_month, ptmpBuffer + 36, 4)
    lRetVal = DataFromPtr(ti.tod_year, ptmpBuffer + 40, 4)
    'Get last element of the structure
    lRetVal = DataFromPtr(ti.tod_weekday, ptmpBuffer + 44, 4)

    'Convert to local time
    If ti.tod_timezone <> -1 Then
        ti.tod_hours = ti.tod_hours - ti.tod_timezone \ 60
    End If

    'Release the memory, allocated by NetRemoteTOD function
    NetAPIBufferFree ptmpBuffer

    Me.Print "Time: "; ti.tod_hours; ":"; ti.tod_mins; ":"; ti.tod_secs
    Me.Print "Date: "; ti.tod_day; "/"; ti.tod_month; "/"; ti.tod_year
End Sub

3. Go to the Command1_Click() routine and SET theCORRECT Server name.
4. Run the project and press Command1.

The server time will be printed in the Debug window.

Regards,
                 Stoil

Quote:

> Has anyone got this to work under VB 6?

> If so please post I'd very much appreciate it.

> Eddie



Fri, 10 Aug 2001 03:00:00 GMT  
 Get server time using NetRemoteTOD
Here is some sample code, it rus om WinNt 4.0.

Jan.

Option Explicit

Option Base 0
Type TIME_OF_DAY_INFO
  tod_elapsedt As Long
  tod_msecs As Long
  tod_hours As Long
  tod_mins As Long
  tod_secs As Long
  tod_hunds As Long
  tod_timezone As Long
  tod_tinterval As Long
  tod_day As Long
  tod_month As Long
  tod_year As Long
  tod_weekday As Long
End Type

Global Const TOD_OFFSET = 2209161600#
Global Const TOD_DAYSEC = 86400#
Declare Function NetRemoteTOD Lib "netapi32.dll" (UncServerName As Byte,
BufferPtr As Long) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As
Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Declare Sub CopyMemoryToAPIBuffer Lib "kernel32" Alias "RtlMoveMemory"
(ByVal hpvDest As Long, hpvSource As Any, ByVal cbCopy As Long)
Declare Function NetAPIBufferFree Lib "netapi32.dll" Alias
"NetApiBufferFree" ( _
       ByVal Ptr As Long) As Long

Declare Function NetAPIBufferAllocate Lib "netapi32.dll" Alias
"NetApiBufferAllocate" ( _
       ByVal ByteCount As Long, Ptr As Long) As Long
Function NetTime(Server As String, TimeInfo As TIME_OF_DAY_INFO) As Long

  Dim sByte() As Byte
  Dim sPathname() As Byte
  Dim sUsername() As Byte
  ReDim sByte(512)
  Dim result As Long, result2 As Long
  Dim pServer() As Byte
  Dim BufLen As Long
  Dim ptmpBuffer As Long

  pServer = Server & vbNullChar

  result = NetRemoteTOD(pServer(0), ptmpBuffer)
  If result = 0 Then
    CopyMemory TimeInfo, ptmpBuffer, LenB(TimeInfo)
  Else
    NetTime = result
  End If
  result2 = NetAPIBufferFree(ptmpBuffer)
  NetTime = result

End Function

Quote:

> Has anyone got this to work under VB 6?

> If so please post I'd very much appreciate it.

> Eddie



Sun, 12 Aug 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Get server time using NetRemoteTOD

2. Get server time using NetRemoteTOD

3. Problem Using Time Part of a Date-Time field in SQL-Server

4. Using NetRemoteTOD in VB5

5. Getting Server time

6. Getting Server date and time

7. Getting Date and time from the server

8. Getting just the time from another computer/server

9. Loggin in to a NTP-SERVER and getting the Time

10. Getting Server Time

11. Netware API for getting Server Time

12. Getting DATE/TIME from a Server

 

 
Powered by phpBB® Forum Software