是否有一个 Windows API 可以实现相当于单击“日期和时间属性”/“Internet 时间”选项卡中的“立即更新”按钮(通过双击任务栏中的时钟打开)?
有没有办法监控windows何时触发时间同步以及何时成功或失败?
是否有一个 Windows API 可以实现相当于单击“日期和时间属性”/“Internet 时间”选项卡中的“立即更新”按钮(通过双击任务栏中的时钟打开)?
有没有办法监控windows何时触发时间同步以及何时成功或失败?
时间服务没有公开 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();
}
}
}
It's undocumented so I'm not exactly sure what the possible flags are, but 8 seems to do the job just fine - tested with my system clock. If you're running Windows 64-bit, compile for 64-bit or you'll be getting access violation exceptions.
W32TimeQueryStatus
should be able to get the last successful sync time. I'll work on that when I have more time.