Click here to Skip to main content
4.05 / 5, 15 votes
1 vote, 6.7%
1
1 vote, 6.7%
2
2 votes, 13.3%
3
4 votes, 26.7%
4
7 votes, 46.7%
5

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

By SeaWater | 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

About the Author

SeaWater


Web Developer

United States United States

Member


Sign Up to vote for this article
Add a reason or comment to your vote:

Comments and Discussions

You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
GeneralDay Increment/Decrement Problem PinmemberInkissar21:22 7 Jul '09  
GeneralNetRemoteTOD Pinmembercocacolacool21:41 9 Jun '09  
Generallinux version Pinmemberctarsoaga8:47 5 Mar '07  
GeneralWhere do I fill in my username and password Pinmemberkarthik_887:16 5 Apr '06  
GeneralDoesn't work for me urgent pls... Pinmemberpugak19:14 23 Aug '05  
GeneralDoesn't work for me urgent pls... Pinmemberpugak19:12 23 Aug '05  
GeneralExtend to include milliseconds Pinmemberjtm17@hotmail.com8:44 3 Aug '05  
GeneralMust have a session first, but how in .NET ? Pinmembermsouphy0:10 16 Jun '05  
GeneralDoesn't work with a unix server Pinmemberweezowazo7:19 19 Jan '05  
GeneralSame function fixed and compressed PinmemberTed140517:39 9 Nov '04  
QuestionRe: Same function fixed and compressed PinmemberDorMarchan6:52 14 May '08  
GeneralDoesn't always work PinsussAnonymous12:54 27 Sep '04  
GeneralRe: Doesn't always work PinsussAnonymous6:17 28 Sep '04  
GeneralRe: Doesn't always work - here's a cleaned up version Pinmemberplq120:00 10 May '08  
GeneralVery Good Article... Pinmemberjkutsor2:18 13 Jan '04  
GeneralRe: Very Good Article... PinmemberSeaWater3:14 13 Jan '04  
GeneralHow about net time Pinmemberhmlinder1:26 9 Jan '04  
GeneralRe: How about net time PinmemberSeaWater3:38 9 Jan '04  
GeneralWhy not just use a web service Pinmemberhmlinder16:27 7 Jan '04  
If I understand what you are trying to say, setting up a web service on the server would accomplish the same thing. Just have a function that returns the date / time .
GeneralRe: Why not just use a web service PinmemberSeaWater3:52 8 Jan '04  
GeneralRe: Why not just use a web service PinsussAnonymous7:40 4 Feb '04  
GeneralRe: Why not just use a web service PinsussAnonymous8:03 4 Feb '04  
GeneralRe: Why not just use a web service PinsussAnonymous8:40 4 Feb '04  
GeneralRe: Why not just use a web service PinmemberGlenn C3:12 13 Sep '04  
GeneralRe: Why not just use a web service PinmemberJockeP3:12 2 Mar '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 6 Jan 2004

Copyright 2004 by SeaWater
Everything else Copyright © CodeProject, 1999-2010
Web22 | Advertise on the Code Project