2010-05-04 3 views
1

У меня есть приложение, которое разработано с использованием. Compact Compact frame work 3.5, мне нужно синхронизировать дату и время устройства с центральным сервером при каждом вызове веб-службы.Установка системной даты и времени из приложения

Спасибо.

+2

Возможный дубликат http://stackoverflow.com/questions/738615/set-device-time-on-net-cf –

+2

@ Сундар: так много вопросов без единого ответа. Начинать маркировку ответов или не удивляться тому, что люди не отвечают на вас. – Shaihi

ответ

2

Если на вашем устройстве установлен клиент NTP, вы можете использовать его. Сервер (ы) настраиваются в параметре реестра MultiString по адресу:

[HKLM\Services\TIMESVC] 
server 

Вот код, как заставить клиент для синхронизации. В противном случае значение реестра для как часто клиент синхронизирует (DWORD для мс):

[HKLM\Services\TIMESVC] 
Refresh 

синхронизации Force:

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

Я был в состоянии синхронизировать время с помощью SNTP Client Project from the Codeproject

Однако, пришлось изменить заявление Pinvoke на использование [DllImport("coredll.dll")] вместо [DllImport("kernal32.dll")]

Я сделал этот бег ветра ows mobile 6.1, компактный каркас 3.5

+0

Более новая версия здесь: http://bocan.ro/Media/Default/Downloads/InternetTime.zip – franDayz

Смежные вопросы