Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get date from another PC or Server

Status
Not open for further replies.

meny21

IS-IT--Management
Oct 4, 2002
101
MX
Hi Guys,

Does anyone knows which command can I use in order to get the date from another PC or Server in Visual Basic 6?

For example, I put the time of my PC in this variable:

MyVar = Time

But I would like to take it from another PC?

MyVar = A_Server_Time (or another_PC_Time)

This server doesn't have SQL, I just want to get the time that it has in that moment...

Thanks for any help!

MR
 
One way is via the NetRemoteTOD API call:
Code:
Option Explicit

'Fetch and display Net Remote Time Of Day from a
'remote Windows system.  Supply a UNC hostname
'(or a DNS name), or empty string for the local
'host's time and date.
'
'Form has 3 controls:
'
'   txtServer   TextBox
'   cmdGetTime  CommandButton
'   lblTime     Label

Private Const NERR_SUCCESS As Long = 0

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 Declare Function NetApiBufferFree Lib "netapi32" _
  (ByVal lpBuffer As Long) As Long

Private Declare Function NetRemoteTOD Lib "netapi32" _
  (UncServerName As Byte, _
   BufferPtr As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  (pTo As Any, _
   uFrom As Any, _
   ByVal lSize As Long)

Private Function GetTOD(ByVal Server As String) As Date
    Dim bytServer() As Byte
    Dim lngBufPtr As Long
    Dim todReturned As TIME_OF_DAY_INFO
    
    bytServer = Server & vbNullChar
    If NetRemoteTOD(bytServer(0), lngBufPtr) = NERR_SUCCESS Then
        CopyMemory todReturned, ByVal lngBufPtr, LenB(todReturned)
        NetApiBufferFree lngBufPtr
        With todReturned
            If .tod_timezone = -1 Then .tod_timezone = 0
            GetTOD = DateAdd("n", _
                             -.tod_timezone, _
                             DateSerial(.tod_year, .tod_month, .tod_day) _
                           + TimeSerial(.tod_hours, .tod_mins, .tod_secs))
        End With
    Else
        Err.Raise vbObjectError + 2000, _
                  "GetTOD", _
                  "Failed to obtain server time"
    End If
End Function

Private Sub cmdGetTime_Click()
    Dim dtServerTime As Date
    
    txtServer.Text = Trim$(txtServer.Text)
    If Len(txtServer.Text) > 0 Then
        On Error Resume Next
        dtServerTime = GetTOD(txtServer.Text)
        If Err.Number <> 0 Then
            lblTime.Caption = Err.Description
        Else
            lblTime.Caption = CStr(dtServerTime)
        End If
        On Error GoTo 0
    Else
        Beep
    End If
    txtServer.SetFocus
End Sub
 
The 'DOS' hack;

Private Sub Command1_Click()

Dim Msg$

Shell Environ$("COMSPEC") & " /C net time \\lappy >""C:\Users\Hugh Lerwill\Temp.txt"""

Do Until Len(Dir$("C:\Users\Hugh Lerwill\Temp.txt"))
DoEvents
Loop

Open "C:\Users\Hugh Lerwill\Temp.txt" For Input As #1
Line Input #1, Msg$
Close

Text1 = Msg$

End Sub
 
Or
Code:
[blue]Private Function GetDateTimeFromPC(Optional strComputer As String = ".") As Date
    Dim objItem As Object
    With GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        For Each objItem In .ExecQuery("Select * from Win32_LocalTime")
            With objItem
                GetDateTimeFromPC = Format(.Day & "/" & .Month & "/" & .Year & " " & .Hour & ":" & .Minute & ":" & .Second, "dd mm yyyy hh:nn:ss") ' Should ensure we don't worry about regional settngs
            End With
        Next
    End With
End Function[/blue]
 
Please note that

"dd mm yyyy hh:nn:ss"

should read

"dd mmm yyyy hh:nn:ss"
 
Most likely you won't be interrogating the time frequently anyway, but keep in mind that WMI is a management tool. It's a fairly heavyweight thing to be using in applications, especially in server applications where the machine must service a large number of users.

See WMI Architecture Basics.

WMI is incredibly rich in scope though, and for low volume tasks can save a ton of coding effort.


The Networking API uses infrastructure optimized for production tasks.


Has anyone discovered the proper VB syntax to retrieve a solo item from a WMI collection? I find myself using [tt]For Each[/tt] to enumerate these collections even when there is only one item possible in the collection.
 
Nope. There seem to be eccentricities with the WMI methoda that mean you need to walk the enumeration, even if it has only one member. Indexing into the enumeration always seems to fail
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top