Tuesday, April 24, 2007

Strangely I get asked lots about setting the clock on a Windows Mobile device.     I use this bit Compact Framework C#  code all the time,   remember it sets the base GMT date/time of the device.   To use copy and paste the below and simply call  SetBaseTime(<your datetime>)

[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");
}

}

Tuesday, April 24, 2007 9:31:39 AM UTC  #    Comments [0]  | 

Theme design by Jelle Druyts

Pick a theme: