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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get server date and time ?? 1

Status
Not open for further replies.

ummul21

Programmer
Jun 23, 2003
21
0
0
SG
Hi,
I'm having problem to get the time and date from file server to my vb program.

I don't want to pull the PC date and time coz, user can change it anytime they want. It will not be accurate....

 

What OS is the client? I ask because the solution I have has a requirement that the client needs to be at least Win2k Pro, WinXP, or NT, and the server must be NT, Win2k, or Win2k3.

 
My file server is running on the Win 2k server.

The client is Win95, Win98 and Win2k
 

Well this will not work on the Win95 or Win98 but here goes...

Place the following in a module
[tt]
Option Explicit

Public Declare Function NetRemoteTOD Lib "Netapi32.dll" (ByVal yServer As Long, ByRef pBuffer As Long) As Long
Public Declare Sub CopyTimeValue Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, ByVal Source As Long, ByVal Length As Long)
Public Declare Function IsBadPointer Lib "kernel32" Alias "IsBadReadPtr" (ByVal lp As Long, ByVal ucb As Long) As Long
Public Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As Long) As Long

Private 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

Private Const NERR_Succes = 0

Public Function GetServerDateTime(ServerName As String) As String

On Error GoTo GetServerDateTimeError

Dim TOD As TIME_OF_DAY_INFO
Dim MemoryPointer As Long

'check to make sure we have at least something
If Trim(ServerName) = vbNullString Then Exit Function

'make sure it address is good
If IsBadPointer(StrPtr(ServerName), Len(ServerName)) > 0 Then

'error reading memory address
Exit Function

End If

'Retrieve the servers date time
If NetRemoteTOD(StrPtr(ServerName), MemoryPointer) = NERR_Succes Then

'make sure we have valid data
If IsBadPointer(MemoryPointer, Len(TOD)) > 0 Then

'must be bad data
Exit Function

End If

'lets get the data into our udt so we can use it
Call CopyTimeValue(TOD, MemoryPointer, Len(TOD))

'now we have the date and time
GetServerDateTime = CStr(TOD.TOD_MONTH) & "/" & CStr(TOD.TOD_DAY) & "/" & CStr(TOD.TOD_YEAR) & " " & Format(TOD.TOD_HOURS, "00") & ":" & Format(TOD.TOD_MINS, "00") & ":" & Format(TOD.TOD_SECS, "00")

'but we must adjust for the time zone
GetServerDateTime = DateAdd("n", -1 * TOD.TOD_TIMEZONE, GetServerDateTime)

'now we need to free the memory allocated by the system
If Not NetApiBufferFree(MemoryPointer) = NERR_Succes Then

'bad news we have just created a memory leak. as long as this function is not call constantly it should not be that bad (Yeah right!)
Exit Function

End If

Else

'some error getting the time
Exit Function

End If

Exit Function
GetServerDateTimeError:

MsgBox Err.Description

End Function
[/tt]

Good Luck

 
Hi,
Thanks a lot for the coding.....

But, majority of my client is Win95 & 98.... Did anyone else have a soultion for that....

Thanks...
Ummul
 
A rather crude and inelegant solution:

Shell "NET TIME \\SERVERNAME > C:\MYTIME.TXT"

then open MYTIME.TXT and read the date/time


Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
ummul,

AndyWatt is quite correct as far as I can tell at this time that using the shell with the redirect to a text file is not only the easiest way to go to get the date time from a server but may be the only way also.

here is some reference material for you...



Also I tried my code in the 95 machine that I have and it failed but at the command prompt using net time \\servername worked.

Oh yeah! the function above needs you to have it formatted the same way "\\servername" or it will use the local machines time.

Good Luck
 
There is a FAQ on this.
faq222-1065

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Hi Andy,

I tried to create a file called mytime.txt in my C drive and execute below statement in the Form Load

Shell "NET TIME \\bpm1 > C:\mytime.TXT"

'my server name is \\bpm1

But it didn't update anything to the text file.... the text file is blank....

Rgd & Thanks,
Ummul.
 
Ummul
Code:
  Const cstrN As String = "C:\MYTIME.TXT"
  Const cstrD As String = "YYYY-MMM-DD HH:NN"
  Const cstrS As String = "SERVERNAME"
  Dim lngR As Long
  Dim intF As Integer
  Dim strX As String
  
  ' Shell Command.com to get time to a file in root dir
  lngR = Shell("COMMAND.COM /C NET TIME \\" & cstrS & " > " & cstrN)
  
  ' Open file and read in text
  intF = FreeFile
  Open cstrN For Input As #intF
  Line Input #intF, strX
  Close intF
  
  ' Search for the " is " - date and time follows
  lngR = InStr(1, strX, " is ")
  
  ' Show date reformatted, just to show it worked
  strX = Format(Mid(strX, lngR + 4), cstrD)
  MsgBox strX

If you're using XP/NT/2K then substitute "CMD.EXE" for "COMMAND.COM"

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
I have the same question.

AndyWatt, I tried the code you posted but I really can't figure out what strX stands for. Please help.

Thanks in advance
 
strX is a variable of type string as per
Code:
Dim strX As String

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"A computer program does what you tell it to do, not what you want it to do." -- Greer's Third Law
 

Ummul,

Did someone here solve your problem or have you been able to solve it yourself???

 
Oh,
so sorry for a very late reply.

vb5prgrmr,
Actually I use your procedure to get the server date & it is w'king fine with win 95 & 98 clients. This is b'coz I have only one .exe file which I will put in the server and all the client will access it from their side. So, there is no problem for my server to get the date and show it to the users.

Thank you so much [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top