Link to home
Start Free TrialLog in
Avatar of lomo74
lomo74Flag for Italy

asked on

How to force NTP synchronization

Hi Experts!
assume Windows is configured to acquire time from an internet NTP service, let's say pool.ntp.org.
how do I force synchronization?
I mean, I know synchronization goes on automatically once configured but I want to tell Windows "synchronize now", using Delphi code.
Thanks --
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

use the following procedure

function SetSystemDateTime(ANewDate: TDateTime): Boolean;
var
  dtSystem: TSystemTime;
begin
  with dtSystem do
  begin
    wYear := StrToInt(FormatDateTime('yyyy', ANewDate));
    wMonth := StrToInt(FormatDateTime('mm', ANewDate));
    wDay := StrToInt(FormatDateTime('dd', ANewDate));
    wHour := StrToInt(FormatDateTime('hh', ANewDate));
    wMinute := StrToInt(FormatDateTime('nn', ANewDate));
    wSecond := StrToInt(FormatDateTime('ss', ANewDate));
    wMilliseconds := 0;
  end;
  Result := SetSystemTime(ANewDate);
end;
Avatar of lomo74

ASKER

ewangoya:
sorry, you completely missed the point --
look at attached image: this is Windows 7 system date/time dialog.
As you can see, system is configured to obtain correct date/time from NTP server ntp.ien.it.
Next synchronization is scheduled for dec 8, 2010 @ 10:16 (blue circle).
Windows allows you to force synchronization *now* (red circle, "Aggiorna" is for "Update" in italian language).
I want to force update from my Delphi code, not from Windows "Update" button.
Hope this is clearer now.

User generated image
Got you

From your delphi program, you can execute this dos command

w32tm /resync /rediscover

This should force a resynchronize
for other options of w32tm, run w32tm /? from dos prompt, i haven't used this in a long time
ASKER CERTIFIED SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lomo74

ASKER

well, that'll do the trick only if "run as administrator"... damn UAC...
but that gave me an idea, so I started tracing w32tm.exe with dependency walker and discovered it is loading w32time.dll to call a function named "W32TimeSyncNow"... interesting...
so I started googling for W32TimeSyncNow and discovered this:
http://us.generation-nt.com/answer/w32time-multiple-ip-addresses-help-30467912.html
ok so now we know what w32tm does under the hood.
I carried out a quick test but W32TimeSyncNow returns 5: access denied.
maybe we must go through rundll32 which is whitelisted.
I'll give it a try
Avatar of lomo74

ASKER

no luck. error 5 again, even if launched by rundll32.
seems Microsoft has fixed this:
http://www.withinwindows.com/2009/02/04/windows-7-auto-elevation-mistake-lets-malware-elevate-freely-easily/
so... this is a UAC issue. I feel I cannot easily bypass it -
what do you think

I don't have access to my Windows 7 box, but I'll look into it.

UAC is a b* sometimes
Yap, had the same problem, I could'nt bypass it
Avatar of lomo74

ASKER

ok; maybe I'd better find another way to do that -- I'm developing a demo period mechanism for my app and I wanted to prevent users from fooling it simply putting clock back.
anyway, question answered -
thanks ewangoya