Click here to Skip to main content
14,362,963 members

Retrieving date and time from remote server using NetRemoteTOD in VB.NET

Rate this:
4.27 (16 votes)
Please Sign up or sign in to vote.
4.27 (16 votes)
6 Jan 2004
This article will show how to retrieve the date and time from a remote server using the NetRemoteTOD function

Introduction

This project shows how to use the NetRemoteTOD to retrieve the date/time from a remote server using VB.NET

Imports

Add the following imports line to your code:

Imports System.Runtime.InteropServices

API Functions

Add the following functions to your code:

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

Structures

Add the following structure to your code:

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

Functions

Add the following function to your code:

Function GetNetRemoteTOD(ByVal strServerName As String) As Date
Try
Dim iRet As Integer
Dim ptodi As IntPtr
Dim todi As TIME_OF_DAY_INFO
Dim dDate As Date
strServerName = strServerName & vbNullChar
iRet = NetRemoteTOD(strServerName, ptodi)
If iRet = 0 Then
todi = CType(Marshal.PtrToStructure(ptodi, GetType(TIME_OF_DAY_INFO)), _
  TIME_OF_DAY_INFO)
NetApiBufferFree(ptodi)
dDate = DateSerial(todi.tod_year, todi.tod_month, todi.tod_day) + " " + _
TimeSerial(todi.tod_hours, todi.tod_mins - todi.tod_timezone, todi.tod_secs)
GetNetRemoteTOD = dDate
Else
MsgBox("Error retrieving time")
End If
Catch
MsgBox("Error in GetNetRemoteTOD: " & Err.Description)
End Try
End Function

Calling the function

Here is a sample code used to call the function. "servername" is replaced with the name of the actual server that you want to get the date/time from:

Dim dRemoteDate As Date
dRemoteDate = GetNetRemoteTOD("servername")
MsgBox("The remote date is " & dRemoteDate)

Conclusion

That's all folks! I wanted to make this as easy to understand as possible. I am also attaching a complete sample project which has this interface:

Sample screenshot

Let me know if you have any questions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

SeaWater
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
Questionnote for test Pin
Member 100456472-Jul-17 4:49
memberMember 100456472-Jul-17 4:49 
GeneralMy vote of 1 Pin
Member 857381017-Jan-12 1:13
memberMember 857381017-Jan-12 1:13 
GeneralDay Increment/Decrement Problem Pin
M. Ousama Ghazali7-Jul-09 21:22
memberM. Ousama Ghazali7-Jul-09 21:22 
Hi,

We have been using this code since long but could not observe a problem till now. The problem we are facing is that the server time gets 1 day decremented at certain times. After careful analaysis we figured out that this is something related to time zone computation because we are in GMT+3 and also found a similar problem/solution reported on net.

Here is the page which reported the problem and solution: http://forums.devx.com/showpost.php?p=512474&postcount=2[^]

And here is the page with a more stable solution: http://vbnet.mvps.org/index.html?code/network/netremotetod.htm[^]

I request the author to look if the code needs updating and this has been shared for community welfare.
GeneralNetRemoteTOD Pin
cocacolacool9-Jun-09 21:41
membercocacolacool9-Jun-09 21:41 
Generallinux version Pin
ctarsoaga5-Mar-07 8:47
memberctarsoaga5-Mar-07 8:47 
GeneralWhere do I fill in my username and password Pin
karthik_885-Apr-06 7:16
memberkarthik_885-Apr-06 7:16 
GeneralDoesn't work for me urgent pls... Pin
pugak23-Aug-05 19:14
memberpugak23-Aug-05 19:14 
GeneralDoesn't work for me urgent pls... Pin
pugak23-Aug-05 19:12
memberpugak23-Aug-05 19:12 
GeneralExtend to include milliseconds Pin
Member 17661643-Aug-05 8:44
memberMember 17661643-Aug-05 8:44 
QuestionMust have a session first, but how in .NET ? Pin
msouphy16-Jun-05 0:10
membermsouphy16-Jun-05 0:10 
GeneralDoesn't work with a unix server Pin
weezowazo19-Jan-05 7:19
memberweezowazo19-Jan-05 7:19 
GeneralSame function fixed and compressed Pin
Ted14059-Nov-04 17:39
memberTed14059-Nov-04 17:39 
QuestionRe: Same function fixed and compressed Pin
DorMarchan14-May-08 6:52
memberDorMarchan14-May-08 6:52 
GeneralDoesn't always work Pin
Anonymous27-Sep-04 12:54
memberAnonymous27-Sep-04 12:54 
GeneralRe: Doesn't always work Pin
Anonymous28-Sep-04 6:17
memberAnonymous28-Sep-04 6:17 
GeneralRe: Doesn't always work - here's a cleaned up version Pin
plq110-May-08 20:00
memberplq110-May-08 20:00 
GeneralVery Good Article... Pin
jkutsor13-Jan-04 2:18
memberjkutsor13-Jan-04 2:18 
GeneralRe: Very Good Article... Pin
SeaWater13-Jan-04 3:14
memberSeaWater13-Jan-04 3:14 
QuestionHow about net time Pin
hmlinder9-Jan-04 1:26
memberhmlinder9-Jan-04 1:26 
AnswerRe: How about net time Pin
SeaWater9-Jan-04 3:38
memberSeaWater9-Jan-04 3:38 
GeneralWhy not just use a web service Pin
hmlinder7-Jan-04 16:27
memberhmlinder7-Jan-04 16:27 
GeneralRe: Why not just use a web service Pin
SeaWater8-Jan-04 3:52
memberSeaWater8-Jan-04 3:52 
GeneralRe: Why not just use a web service Pin
Anonymous4-Feb-04 7:40
memberAnonymous4-Feb-04 7:40 
GeneralRe: Why not just use a web service Pin
Anonymous4-Feb-04 8:03
memberAnonymous4-Feb-04 8:03 
GeneralRe: Why not just use a web service Pin
Anonymous4-Feb-04 8:40
memberAnonymous4-Feb-04 8:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 6 Jan 2004

Stats

180.6K views
2.6K downloads
24 bookmarked