Microsoft KB Archive/249716: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
 
Line 68: Line 68:
<div class="indent">
<div class="indent">


server_name = &quot;yourservername&quot;
server_name = "yourservername"




Line 103: Line 103:
* whether the target is a Win2000 computer or not -
* whether the target is a Win2000 computer or not -
*
*
* If Win2000, the servername must be preceded by &quot;\\&quot;;
* If Win2000, the servername must be preceded by "\\";
* otherwise, it must not.
* otherwise, it must not.


server_name = &quot;yourservername&quot;
server_name = "yourservername"
try_server_name = STRCONV(server_name, 5)
try_server_name = STRCONV(server_name, 5)
rc = NetRemoteTOD(@try_server_name, @tdbuffin)
rc = NetRemoteTOD(@try_server_name, @tdbuffin)
Line 115: Line 115:
ELSE
ELSE
   * call failed. Therefore, the target is possibly a Win2000 box;
   * call failed. Therefore, the target is possibly a Win2000 box;
   * Retry the function call, prepending &quot;\\&quot; to the server_name
   * Retry the function call, prepending "\\" to the server_name
   try_server_name = STRCONV(&quot;\\&quot; + server_name, 5)
   try_server_name = STRCONV("\\" + server_name, 5)
   rc = NetRemoteTOD(@try_server_name, @tdbuffin)
   rc = NetRemoteTOD(@try_server_name, @tdbuffin)
   IF rc = 0
   IF rc = 0
Line 123: Line 123:
       =RtlMoveMemory(@tdbuffout, tdbuffin, 48)
       =RtlMoveMemory(@tdbuffout, tdbuffin, 48)
   ELSE
   ELSE
       ? &quot;NetRemoteTOD() call failed. Return code is: &quot;, rc
       ? "NetRemoteTOD() call failed. Return code is: ", rc
       RETURN
       RETURN
   ENDIF
   ENDIF
Line 155: Line 155:
   tod_hours, tod_mins, tod_secs)
   tod_hours, tod_mins, tod_secs)


? &quot;UTC time of server is: &quot;, serverdatetime
? "UTC time of server is: ", serverdatetime
? &quot;Server's local time is: &quot;, serverdatetime - tod_timezone
? "Server's local time is: ", serverdatetime - tod_timezone


*************************************************************
*************************************************************
Line 164: Line 164:
* returns:  long integer value
* returns:  long integer value
* example:
* example:
*  m.longstr = &quot;1111&quot;
*  m.longstr = "1111"
*  m.longval = str2long(m.longstr)
*  m.longval = str2long(m.longstr)



Latest revision as of 12:51, 21 July 2020

Knowledge Base


How to use the NetRemoteTOD function to obtain date and time information from a server

Article ID: 249716

Article Last Modified on 2/24/2005



APPLIES TO

  • Microsoft Visual FoxPro 6.0 Professional Edition
  • Microsoft Visual FoxPro 7.0 Professional Edition
  • Microsoft Visual FoxPro 8.0 Professional Edition
  • Microsoft Visual FoxPro 9.0 Professional Edition



This article was previously published under Q249716

SUMMARY

In Microsoft Windows NT 4.0, Windows 2000, Windows XP, and Windows Server 2003, you can obtain the date and time from a server by using the NET command line utility with the following syntax:

NET TIME \\ServerName


To avoid the expense of shelling to a new process, you can obtain this information programmatically by using the NetRemoteTOD API function on computers that are running Windows NT and later versions of the Windows operating system.

MORE INFORMATION

Note This code does not work in Microsoft Visual FoxPro when hosted on Windows 2000 Terminal Server.

Save and execute the following code.

Note Specify the server name whose time you want to query in the following line:

server_name = "yourservername"


* NetRemoteTOD's first parameter is a pointer to a
* Unicode string that contains the server name.
*
* The second parameter is a pointer to a byte array
* that contains a pointer to a TIME_OF_DAY_INFO structure

* The '@' in front of the second parameter ('integer @')
* dereferences this pointer to the byte array. Later in the
* program, the program uses RTLMoveMemory() to
* dereference the pointer this byte array contains
DECLARE INTEGER NetRemoteTOD IN netapi32 STRING @,  INTEGER @

* Note that the source address ('inbuffer') is declared as an integer,
* to be consistent with the second parameter in NetRemoteTOD above.
DECLARE INTEGER RtlMoveMemory IN win32api ;
   STRING @outbuffer, ;
   INTEGER inbuffer, ;
   INTEGER bytes2copy


* the TIME_OF_DAY_INFO structure
* contains 11 DWORDs and 1 long, for
* a total of 48 bytes. Therefore, tdbuffout is
* initialized as:
tdbuffout=REPLICATE(CHR(0), 48)
tdbuffin = 0

* the server name must be converted to Unicode
* This API function behaves differently depending on
* whether the target is a Win2000 computer or not -
*
* If Win2000, the servername must be preceded by "\\";
* otherwise, it must not.

server_name = "yourservername"
try_server_name = STRCONV(server_name, 5)
rc = NetRemoteTOD(@try_server_name, @tdbuffin)
IF rc = 0
   * copy the contents pointed to by the address in tdbuffin to
   * tdbuffout
   =RtlMoveMemory(@tdbuffout, tdbuffin, 48)
ELSE
   * call failed. Therefore, the target is possibly a Win2000 box;
   * Retry the function call, prepending "\\" to the server_name
   try_server_name = STRCONV("\\" + server_name, 5)
   rc = NetRemoteTOD(@try_server_name, @tdbuffin)
   IF rc = 0
      * copy the contents pointed to by the address in tdbuffin to
      * tdbuffout
      =RtlMoveMemory(@tdbuffout, tdbuffin, 48)
   ELSE
      ? "NetRemoteTOD() call failed. Return code is: ", rc
      RETURN
   ENDIF
ENDIF

* Pick out the appropriate parts of the TIME_OF_DAY_INFORMATION
* buffer. This buffer will contain the UTC (Universal Coordinated
* Time) of the server, and must be adjusted by TOD_TIMEZONE minutes
* for the correct local time.

* str2long() converts the DWORDS and LONGS from their string
* representation back to numbers.
tod_month = str2long(SUBSTR(tdbuffout, 37, 4))
tod_day = str2long(SUBSTR(tdbuffout, 33, 4))
tod_year = str2long(SUBSTR(tdbuffout, 41, 4))
tod_hours = str2long(SUBSTR(tdbuffout, 9, 4))
tod_mins = str2long(SUBSTR(tdbuffout, 13, 4))
tod_secs = str2long(SUBSTR(tdbuffout, 17, 4))

* Subtract this bias (times 60, to obtain seconds)
* from the datetime value to obtain the
* server's local time
*
* Alternatively, to convert the server's local time to
* the workstation's local time, use the Win32 API function
* SystemTimeToTzSpecificLocalTime, available under
* Windows NT only.
tod_timezone = str2long(SUBSTR(tdbuffout, 25, 4)) * 60

serverdatetime = DATETIME(tod_year, tod_month, tod_day, ;
   tod_hours, tod_mins, tod_secs)

? "UTC time of server is: ", serverdatetime
? "Server's local time is: ", serverdatetime - tod_timezone

*************************************************************
FUNCTION str2long
*************************************************************
* passed:  4-byte character string (m.longstr) in low-high ASCII format
* returns:  long integer value
* example:
*   m.longstr = "1111"
*   m.longval = str2long(m.longstr)

PARAMETERS m.longstr

PRIVATE i, m.retval

m.retval = 0
FOR i = 0 TO 24 STEP 8
   m.retval = m.retval + (ASC(m.longstr) * (2^i))
   m.longstr = RIGHT(m.longstr, LEN(m.longstr) - 1)
NEXT
RETURN m.retval

                

The remote UTC time and local time of the server appear on the Visual FoxPro desktop.

Keywords: kbhowto kbapi kbcodesnippet KB249716