CodeItBetter Programming Another VB Programming Blog

Get Time and Date of a Network machine Server or Workstation

Posted on January 5, 2009

The following code will help you to Get Time and Date of a Network machine Server or Workstation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'System & API - Get Time and Date of a Network machine Server or Workstation
Option Explicit
 
'API Structures
Type TIME_OF_DAY_INFO
    tod_elapsed 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
 
'NetAPI Calls
Public Declare Function NetRemoteTOD Lib "netapi32.dll" (yServer As Any, pBuffer As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32.dll" (ByVal pBuffer As Long) As Long
'Kernel API Calls
Private Declare Sub CopyMem Lib "kernel32.dll" Alias "RtlMoveMemory" (pTo As Any, _
    uFrom As Any, ByVal lSize As Long)
 
'Return the Time and Date of a specified Machine on the Net
Public Function GetRemoteTime(ServerName As String) As Date
    Dim lpBuffer As Long
    Dim t_struct As TIME_OF_DAY_INFO
    Dim ret As Long
    Dim bServer() As Byte
 
    If Trim(ServerName) = "" Then
        'Local machine
        ret = NetRemoteTOD(vbNullString, lpBuffer)
    Else
        'Check the syntax of the ServerName string
        If InStr(ServerName, "\\") = 1 Then
            bServer = ServerName & vbNullChar
        Else
            bServer = "\\" & ServerName & vbNullChar
        End If
        ret = NetRemoteTOD(bServer(0), lpBuffer)
    End If
    CopyMem t_struct, ByVal lpBuffer, Len(t_struct)
    If lpBuffer Then
        Call NetApiBufferFree(lpBuffer)
    End If
    GetRemoteTime = DateSerial(t_struct.tod_year, t_struct.tod_month, t_struct.tod_day) + _
        TimeSerial(t_struct.tod_hours, t_struct.tod_mins - t_struct.tod_timezone, t_struct.tod_secs)
End Function
 
'Get the time and date of the local machine
Private Sub Command1_Click()
    MsgBox GetRemoteTime("")
End Sub
 
'Get the time and date a remote Workstation
Private Sub Command2_Click()
    MsgBox GetRemoteTime("\\MYWORKSTATION")
End Sub

Related posts:

  1. How to Get Date/Time From Central Server
  2. How to return the create date/time, last accessed date/time, last modified date/time of a given file
  3. How to set System Date & time (in two ways)
  4. How to Get Machine IP Address
  5. How to Get the Name of the Day from the inputted date
  6. How to get Network Information [current networks, domains, users, and user info] (for Windows NT/2000 only)
  7. How to show Date/Time Properties
  8. How to open the Date and Time Properties Dialog (in two ways)
  9. How to find Last business day for the given date
  10. How to check Current Date and Time

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.