从应用程序设置系统日期和时间

时间:2010-05-04 06:11:38

标签: .net compact-framework pinvoke windows-ce

我有一个使用.Net Compact框架工作3.5开发的应用程序,我需要在每次Web服务调用时将设备日期和时间与中央服务器同步。

感谢。

2 个答案:

答案 0 :(得分:2)

如果您的设备上有NTP客户端,则可以使用它。服务器配置为多字符串注册表值:

[HKLM\Services\TIMESVC]
server

以下是强制客户端同步的代码。否则,有一个注册表值,表示客户端同步的频率(对于ms为DWORD):

[HKLM\Services\TIMESVC]
Refresh

强制同步:

internal static class NativeMethods
{
    internal const int INVALID_HANDLE_VALUE = -1;
    internal const int IOCTL_SERVICE_CONTROL = (0x104 << 16) | (7 << 2);
    internal const uint GENERIC_READ = 0x80000000;
    internal const uint GENERIC_WRITE = 0x40000000;
    internal const int OPEN_EXISTING = 3;

    [DllImport("coredll.dll", SetLastError = true)]
    internal static extern IntPtr CreateFile(
        string lpFileName, 
        uint dwDesiredAccess, 
        uint dwShareMode, 
        IntPtr lpSecurityAttributes, 
        uint dwCreationDisposition, 
        uint dwFlagsAndAttributes, 
        IntPtr hTemplateFile);

    [DllImport("coredll.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool DeviceIoControl(
        IntPtr hDevice,
        int dwIoControlCode,
        byte[] lpInBuffer,
        int nInBufferSize,
        byte[] lpOutBuffer,
        int nOutBufferSize,
        ref int lpBytesReturned,
        IntPtr lpOverlapped);

    [DllImport("coredll.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool CloseHandle(IntPtr hObject);
}

internal static class TimeServer
{
    public static bool SyncTime()
    {
        byte[] input = System.Text.Encoding.Unicode.GetBytes("Sync\0");
        return TimeServer.DeviceIOCTL("NTP0:", NativeMethods.IOCTL_SERVICE_CONTROL, input);
    }

    private static bool DeviceIOCTL(string deviceName, int ioctl, byte[] input)
    {
        int size = 0;
        return TimeServer.DeviceIOCTL(deviceName, ioctl, input, null, ref size);
    }

    public static bool DeviceIOCTL(string deviceName, int ioctl, byte[] input, byte[] output, ref int bytesReceived)
    {
        bool result = false;

        IntPtr deviceHandle = NativeMethods.CreateFile(
            deviceName, 
            NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 
            0, 
            IntPtr.Zero, 
            (uint)NativeMethods.OPEN_EXISTING, 
            0, 
            IntPtr.Zero);

        if (deviceHandle.ToInt32() != NativeMethods.INVALID_HANDLE_VALUE)
        {
            try
            {
                result = NativeMethods.DeviceIoControl(
                    deviceHandle,
                    ioctl,
                    input,
                    (input == null) ? 0 : input.Length,
                    output,
                    (output == null) ? 0 : output.Length,
                    ref bytesReceived,
                    IntPtr.Zero);
            }
            finally
            {
                NativeMethods.CloseHandle(deviceHandle);
            }
        }

        return result;
    }
}

答案 1 :(得分:1)

我能够使用SNTP Client Project from the Codeproject

同步时间

但是,必须更改Pinvoke语句才能使用[DllImport("coredll.dll")] 而不是[DllImport("kernal32.dll")]

我这样做运行windows mobile 6.1,紧凑框架3.5