Geeks With Blogs

Arthur Zubarev Compudicted
Last week I was asked to test a small applet written by my colleague that could make Windows Mobile 5 device's local (system) date and time updated or set based on date/time received from an outside. The application used the .Net technology.
 
What stroked me was how elegant and simple the application was.
I also knew how to do this on a Windows server or client machine, but then spotted a difference: the only change was in using coredll.dll versa kernel32.dll (for a PC)! So simple, but the difference can be more than subtle for someone. So I have decided to share the code:
 
01 using System;
02 using System.Runtime.InteropServices;
03 using System.Collections.Generic;
04 using System.Text;
05 using System.IO;
06
07 namespace TimeSyncer
08 {
09     class TimeSync
10     {
11         [DllImport("coredll.dll")]
12         private extern static uint SetSystemTime(ref
13         SYSTEMTIME lpSystemTime);
14
15         private struct SYSTEMTIME
16         {
17             public ushort wYear;
18             public ushort wMonth;
19             public ushort wDayOfWeek;
20             public ushort wDay;
21             public ushort wHour;
22             public ushort wMinute;
23             public ushort wSecond;
24             public ushort wMilliseconds;
25         }
26
27         static int Main(string[] args)
28         {
29
30             LogMsgToFile("Application Started.");
31
32             DateTime serverDateTime;
33
34             if (args.Length == 0)
35             {
36                 System.Environment.Exit(1);
37             }
38             else
39             {
40                 try
41                 {
42                     // Obtained the date and time from the
43                     outside, e.g. a time server
44                     serverDateTime = DateTime.Parse(args[0]);
45                 }
46                 catch (System.FormatException)
47                 {
48                     System.Environment.Exit(1);
49                 }
50             }
51
52             SYSTEMTIME st = new SYSTEMTIME();
53
54             TimeSync tsync = new TimeSync();
55
56             DateTime updatedDateTime = tsync.GetDateTime(
57             serverDateTime);
58
59             st.wYear = (ushort)updatedDateTime.Year;
60             st.wMonth = (ushort)updatedDateTime.Month;
61             st.wDay = (ushort)updatedDateTime.Day;
62             st.wHour = (ushort)updatedDateTime.Hour;
63             st.wMinute = (ushort)updatedDateTime.Minute;
64             st.wSecond = (ushort)updatedDateTime.Second;
65
66             uint nReturn = SetSystemTime(ref st);
67
68             return 0; // as success
69         }
70
71         public DateTime GetDateTime(DateTime currentDateTime)
72         {
73             int hourOffsetValue;
74
75             // *** Converting time to GMT
76             // e.g. add 5 hours to the server supplied time
77             to make it GMT if it was sent from EST
78             // * modify the value below to suite your time
79             server input zone location
80             hourOffsetValue = 5;
81             currentDateTime = currentDateTime.AddHours(
82             hourOffsetValue);
83
84             return currentDateTime;
85         }
86     }
87 }
Share it: submit to reddit Digg It! or Bookmark it!
Posted on Monday, January 11, 2010 2:24 PM | Back to top


Comments on this post: How to Update or Set a Windows Mobile Device System Time

# re: How to Update or Set a Windows Mobile Device System Time
Requesting Gravatar...
I am very interested in what is happening here in the code snippet above. Just a couple of questions though, how can i actually use the code and what are the references needed?
And also coredll.dll is a windows CE component am i right?
Left by Ray on Jan 21, 2010 11:50 PM

# re: How to Update or Set a Windows Mobile Device System Time
Requesting Gravatar...
To use it you may want to create a Windows Mobile console application, but the code is not necessarily confined to it. No special references are needed.
Yes, coredll.dll is part of the Windows CE and Windows Mobile image, roughly an equivalent of kernel32.dll on a PC.
Left by Arthur on Jan 22, 2010 10:16 AM

# re: How to Update or Set a Windows Mobile Device System Time
Requesting Gravatar...
Okay, i am not sure how to use it anyway. I have another question though, do you know if its possible to sync the "Regional Settings" of your desktop PC to a PDA? Currently, when i connect my PDA to my desktop PC, the date and time is automatically synchronize but not the regional settings. Any idea how to force the PDA regional settings to follow that of the PC?
Left by Ray on Jan 22, 2010 2:09 PM

# re: How to Update or Set a Windows Mobile Device System Time
Requesting Gravatar...
Hey, very useful, but one note, uint is not the same to every platform, it must be an unsigned 16 bit datatype.
Left by Nati on Feb 21, 2012 7:52 AM

# re: How to Update or Set a Windows Mobile Device System Time
Requesting Gravatar...
Sorry for the question, but, is that a java applet? im starting with MS mobile SDK using VB but im having problems impementing this code, can you help me?
Left by Omar on Apr 22, 2012 11:58 PM

# re: How to Update or Set a Windows Mobile Device System Time
Requesting Gravatar...
No Omar, this is a VB code. What problem did you encounter?
Left by Arthur on Apr 26, 2012 2:58 PM

# re: How to Update or Set a Windows Mobile Device System Time
Requesting Gravatar...
Thanks!!! it works
Left by JuanVega on Feb 12, 2014 4:20 PM

# re: How to Update or Set a Windows Mobile Device System Time
Requesting Gravatar...
Could you please tell me how can I write code to force time synchronization in Windows Mobile 6.5? I am looking for a C++ code to do that.

Thanks in advance.
Left by Noor on Sep 05, 2014 5:25 AM

# re: How to Update or Set a Windows Mobile Device System Time
Requesting Gravatar...
Noor,
you can take my code and convert.

E.g. using

http://www.tangiblesoftwaresolutions.com/Product_Details/CSharp_to_CPlusPlus_Converter_Details.html
Left by Arthur on Sep 27, 2014 5:08 PM

# re: How to Update or Set a Windows Mobile Device System Time
Requesting Gravatar...
do you have a code for this one:

program and time on server, then set time to mobile device.

i also catch an error: index out of range

this DateTime.Parse(args[0]) catch the error

thanks!
Left by Angelo on Oct 09, 2015 4:21 AM

Your comment:
 (will show your gravatar)


Copyright © Compudicted | Powered by: GeeksWithBlogs.net