12

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

有没有办法监控windows何时触发时间同步以及何时成功或失败?

4

2 回答 2

10

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

在 C/C++ 中,您可以执行以下操作:

include <process.h>

...

system("w32tm /resync /nowait");

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

于 2010-05-05T11:30:47.807 回答
3

这似乎有效:

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.

于 2016-07-13T00:39:44.083 回答