Thursday, March 08, 2007

Picking up from my last post where I showed the simple web-server for retrieving the time at the server.

He' re the bit of C# Compact Framework code that is used to set the base clock of the device.

Its a Pinvoke, that sets the base time of the device.   Note someone cleverer than me worked out the structures and things and it does work very well.

 

[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public short year;
public short month;
public short dayOfWeek;
public short day;
public short hour;
public short minute;
public short second;
public short milliseconds;
}

[DllImport("coredll.dll")]
private extern static bool SetSystemTime(ref SYSTEMTIME lpSystemTime);

public static void SetBaseTime(DateTime ourtime)
{
SYSTEMTIME st = new SYSTEMTIME();
st.year = (short)ourtime.Year;
st.month = (short)ourtime.Month;
st.dayOfWeek = (short)ourtime.DayOfWeek;
st.day = (short)ourtime.Day;
st.hour = (short)ourtime.Hour;
st.minute = (short)ourtime.Minute;
st.second = (short)ourtime.Second;
st.milliseconds = (short)ourtime.Millisecond;
if (!SetSystemTime(ref st))
{
throw new Exception("Time Not Set");
}

}

 

So now we have a nice way of setting the clock on the device, from the web-service.     I'll leave it to you to join the dots up and work out how to call the above with the result from the web-service.   

It also works nicely when dealing with a multi-timezone deployment as all base clocks are set to UTC.

Comments are closed.

Theme design by Jelle Druyts

Pick a theme: