Solved

Getting the time

Posted on 1998-12-29
12
710 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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