Click here to Skip to main content
Licence 
First Posted 6 Jan 2004
Views 116,494
Bookmarked 24 times

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

By | 6 Jan 2004 | Article
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   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberMember 85738100:13 17 Jan '12  
GeneralDay Increment/Decrement Problem PinmemberInkissar20:22 7 Jul '09  
GeneralNetRemoteTOD Pinmembercocacolacool20:41 9 Jun '09  
Generallinux version Pinmemberctarsoaga7:47 5 Mar '07  
GeneralWhere do I fill in my username and password Pinmemberkarthik_886:16 5 Apr '06  
GeneralDoesn't work for me urgent pls... Pinmemberpugak18:14 23 Aug '05  
GeneralDoesn't work for me urgent pls... Pinmemberpugak18:12 23 Aug '05  
GeneralExtend to include milliseconds Pinmemberjtm17@hotmail.com7:44 3 Aug '05  
QuestionMust have a session first, but how in .NET ? Pinmembermsouphy23:10 15 Jun '05  
Hi,
 
I'm using this piece of code and I must say it works like a charm but....
 
I'm trying to connect to a remote server which has not the same administrator password as my client.
 
When I haven't connected to the server yet, the code doesn't work.
 
If I open a session (by for example navigating to \\myserver\c$) and filling in my administrator credentials, and then I execute the code through .NET, it works...
 

My question: how to avoid I have to first manually create a session to the remote server ? Is there any way to do that through VB.NET by sending credentials or so ?
 
Thanks for your replies !!
 
Greetings,
 
Mathieu
GeneralDoesn't work with a unix server Pinmemberweezowazo6:19 19 Jan '05  
GeneralSame function fixed and compressed PinmemberTed140516:39 9 Nov '04  
QuestionRe: Same function fixed and compressed PinmemberDorMarchan5:52 14 May '08  
GeneralDoesn't always work PinsussAnonymous11:54 27 Sep '04  
GeneralRe: Doesn't always work PinsussAnonymous5:17 28 Sep '04  
GeneralRe: Doesn't always work - here's a cleaned up version Pinmemberplq119:00 10 May '08  
GeneralVery Good Article... Pinmemberjkutsor1:18 13 Jan '04  
GeneralRe: Very Good Article... PinmemberSeaWater2:14 13 Jan '04  
QuestionHow about net time Pinmemberhmlinder0:26 9 Jan '04  
AnswerRe: How about net time PinmemberSeaWater2:38 9 Jan '04  
GeneralWhy not just use a web service Pinmemberhmlinder15:27 7 Jan '04  
GeneralRe: Why not just use a web service PinmemberSeaWater2:52 8 Jan '04  
GeneralRe: Why not just use a web service PinsussAnonymous6:40 4 Feb '04  
GeneralRe: Why not just use a web service PinsussAnonymous7:03 4 Feb '04  
GeneralRe: Why not just use a web service PinsussAnonymous7:40 4 Feb '04  
GeneralRe: Why not just use a web service PinmemberGlenn C2:12 13 Sep '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120528.1 | Last Updated 7 Jan 2004
Article Copyright 2004 by SeaWater
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid