Windows API来触发时间同步

是否有一个Windows API,可以实现相当于单击“date和时间属性”/“Internet时间”选项卡中的“立即更新”button(通过双击任务栏中的时钟打开)?

有什么办法来监视时间同步是由窗口触发何时成功或失败?

时间服务没有暴露的API,但要触发时间同步,您可以使用w32tm命令行工具。

在C / C ++中,你可以这样做:

 include <process.h> ... system("w32tm /resync /nowait"); 

查看w32tm文档以获取更多选项。

这似乎工作:

 using System; using System.Runtime.InteropServices; namespace ClockResync { class Program { [DllImport("w32time.dll")] public static extern uint W32TimeSyncNow([MarshalAs(UnmanagedType.LPWStr)]String computername, bool wait, uint flag); static void Main(string[] args) { Console.WriteLine(W32TimeSyncNow("computername", true, 8).ToString()); Console.ReadLine(); } } } 

这是没有记录的,所以我不确定可能的标志是什么,但是8似乎做的很好 – 我的系统时钟测试。 如果你正在运行Windows 64位,编译为64位或者你会得到访问冲突异常。

W32TimeQueryStatus应该能够获得上次成功的同步时间。 当我有更多的时间,我会努力。