[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 742
  • Last Modified:

Getting the time

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
clopez
Asked:
clopez
  • 4
  • 3
  • 2
  • +2
1 Solution
 
alexoCommented:
NetRemoteTOD is not supported on Win95, only on NT.
0
 
The_BrainCommented:
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
 
alexoCommented:
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
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
clopezAuthor Commented:
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
 
AlexVirochovskyCommented:
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
 
alexoCommented:
What is the return value from NetRemoteTOD()?  Perhaps 2184?
0
 
clopezAuthor Commented:
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
 
The_BrainCommented:
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
 
NickRepinCommented:
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
 
NickRepinCommented:
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
 
clopezAuthor Commented:
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
 
alexoCommented:
>> 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now