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 undefined
Last Comment

8/22/2022 - Mon
Ephraim Wangoya

use the following procedure

function SetSystemDateTime(ANewDate: TDateTime): Boolean;
  dtSystem: TSystemTime;
  with dtSystem do
    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;
  Result := SetSystemTime(ANewDate);

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.

Screenshot of Windows Date/time dialog
Ephraim Wangoya

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ephraim Wangoya

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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:
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

no luck. error 5 again, even if launched by rundll32.
seems Microsoft has fixed this:
so... this is a UAC issue. I feel I cannot easily bypass it -
what do you think
Ephraim Wangoya

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

UAC is a b* sometimes
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Ephraim Wangoya

Yap, had the same problem, I could'nt bypass it

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