I have a application that is developed using .Net Compact frame work 3.5, i need to synchronize the device date and time with the central server on every web service call.
Thanks.
I have a application that is developed using .Net Compact frame work 3.5, i need to synchronize the device date and time with the central server on every web service call.
Thanks.
If you have the NTP client on your device, you can use that. The server(s) are configured in a multistring registry value at:
[HKLM\Services\TIMESVC]
server
Here is code how to force the client to sync. Otherwise there is a registry value for how often the client syncs (DWORD for ms):
[HKLM\Services\TIMESVC]
Refresh
Force sync:
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;
}
}