Solved

Getting the time

Posted on 1998-12-29
12
686 Views
Last Modified: 2013-12-03
Does anyone know how te get the system time in a remote machine.  I know there is a funciton in the netapi32 called NetRemoteTOD which is supposed to be the one, but it doesnt work for me.  My actual call to the funtion is:

unit rtime;

interface

uses windows;

type
  TNetAPIStatus = DWord;

   TTimeOfDayInfo = record
      tod_elapsedt,
         tod_msecs,
         tod_hours,
         tod_mins,
         tod_secs,
         tod_hunds: DWord;
      tod_timezone: LongInt;
      tod_tinterval,
         tod_day,
         tod_month,
         tod_year,
         tod_weekday: DWord;
   end;

   PTimeOfDayInfo = ^TTimeOfDayInfo;

function NetRemoteTOD(UncServerName: LPWSTR;
   BufferPtr: PTimeOfDayInfo): TNetAPIStatus; stdcall;

function GetServerTime(Server: string): TTimeOfDayInfo;

const
   netapi32 = 'netapi32.dll';

implementation

function NetRemoteTOD;
   external netapi32 name 'NetRemoteTOD';

function GetServerTime(Server: string): TTimeOfDayInfo;

var
   Time: TTimeOfDayInfo;
   WS: array[0..255] of Char;

begin
   StringToWideChar(Server, @WS, SizeOf(WS));
   NetRemoteTOD(@WS, @Time);
   Result := Time;
end;

end.

Well I always get the same time (The funtion is supposed to work for a local machine).  I suppose it needs to have the net instaled.

TIA.
0
Comment
Question by:clopez
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 1417803
NetRemoteTOD is not supported on Win95, only on NT.
0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1417804
GetLocalTime(SYSTEMTIME lpSystemTime ); LpSystemTime is a variable it can be x.

This is an Win32API thus it should work with any language which uses windows.

try it.

(looks like delphi)
0
 
LVL 11

Expert Comment

by:alexo
ID: 1417805
Say, The_Brain, do you really read the question before you rush in to tock it?
clopes explicitly said "Does anyone know how te get the system time in a remote machine".  GetLocalTime() got its name for a reason.
This is not the first time you lock a question with a completely irrelevant answer.  Why don't you slow down, read the questions more carefully and do more research?
0
 

Author Comment

by:clopez
ID: 1417806
1.- As alexo sayd.  There is no problem getting the time in the local machine.  The problem in getting it in a remote machine.  

2.- I lloks like delphi and It is delphi.

3.- I know it only works in NT.  But I don't care if any one gives a solution that works in any other operating system and NT.

4.- I've been testing.  It may be a problem on how to understan TimeOfDayInfo.
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1417807
There is simple way, if you can write file to remote machine.
You write some dummy file, after that get Creation's Time of this file, delete file.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1417808
What is the return value from NetRemoteTOD()?  Perhaps 2184?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:clopez
ID: 1417809
Ok, here is a dump of a TTimeOfDay record

144780,0,0,21A001E,143C18,11C,1F0E664,12FFC0,77F0E68D,7FFDEC00,428700,C000B
1446E8,0,0,21A001E,143C18,11C,1F0E664,12FFC0,77F0E68D,7FFDEC00,428700,C000B
145790,0,0,21A001E,143C18,11C,1F0E664,12FFC0,77F0E68D,7FFDEC00,428700,C000B
1445C8,0,0,21A001E,143C18,11C,1F0E664,12FFC0,77F0E68D,7FFDEC00,428700,C000B
144538,0,0,21A001E,143C18,11C,1F0E664,12FFC0,77F0E68D,7FFDEC00,428700,C000B
1450D8,0,0,21A001E,143C18,11C,1F0E664,12FFC0,77F0E68D,7FFDEC00,428700,C000B
1449C0,0,0,21A001E,143C18,11C,1F0E664,12FFC0,77F0E68D,7FFDEC00,428700,C000B
144678,0,0,21A001E,143C18,11C,1F0E664,12FFC0,77F0E68D,7FFDEC00,428700,C000B
145280,0,0,21A001E,143C18,11C,1F0E664,12FFC0,77F0E68D,7FFDEC00,428700,C000B
1452C8,0,0,21A001E,143C18,11C,1F0E664,12FFC0,77F0E68D,7FFDEC00,428700,C000B
145310,0,0,21A001E,143C18,11C,1F0E664,12FFC0,77F0E68D,7FFDEC00,428700,C000B

It seemd to me to be the elapsed miliseconds from a fixed time.  But I don't get to understand it.
0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1417810
sorry Alexo, I am a bit on the tired side, so I did probably mis read the question. (no need to be so mean though :)

Really really Sorry K?
Brain
0
 
LVL 15

Accepted Solution

by:
NickRepin earned 100 total points
ID: 1417811
As you can see from the my sample, the last parameter for the NetRemoteTOD is NOT the pointer to TIME struct, but POINTER TO POINTER to time struct.
In other words, this function allocates internal buffer, and copies the pointer to it to your pointer passed by reference.

Here is the working sample for C++ console app
#include <windows.h>
#include <lmcons.h>
#include <lmremutl.h>
#include <iostream.h>

// a.exe [<server>]
void main(int argc,char* argv[])
{
   wchar_t serverName[MAX_COMPUTERNAME_LENGTH+1];
   if(argc>1)
      mbstowcs(serverName,argv[1],strlen(argv[1])+1);
   PTIME_OF_DAY_INFO t; // Pointer to struct, but NOT the struct!
   NET_API_STATUS r=NetRemoteTOD(argc>1 ? serverName : 0,(LPBYTE*)(&t));
   if(r) {
      cout<<"Error!"<<endl;
      return;
   }
   cout<<"Date="<<(t->tod_year)<<"/"<<(t->tod_month)<<"/"<<
      (t->tod_day)<<endl;
   cout<<"Time="<<(t->tod_hours)<<":"<<(t->tod_mins)<<":"<<
      (t->tod_secs)<<endl;
}
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1417812
I don't remember how to declare pointer to var in Pascal, but

.
   Time: POINTER of TTimeOfDayInfo; // Check here
.
   NetRemoteTOD(@WS, @Time);
   Result :=  (OBJECT of Time);    // Check here

0
 

Author Comment

by:clopez
ID: 1417813
To Pascal users

the funtion code should be modified to:

function GetServerTime(Server: string): TTimeOfDayInfo;

    var
       Time: PTimeOfDayInfo;

    begin
       NetRemoteTOD(PWideChar(WideString(WS)), @Time);  
//I just learned the WideString shortcut
       Result := Time^;
    end;

0
 
LVL 11

Expert Comment

by:alexo
ID: 1417814
>> no need to be so mean though :)
Try getting up 4 times in one night because your child is teeting...  Sorry for being harsh.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now