meds
meds

Reputation: 22916

Setting Windows Mobile device system time from computer?

I'm trying to sync the system date and time of a device to the desktop it's docked to, I'm working through compact framework (C#).

My goal is to detect when the last time the device was synced to a database on the desktop machine was.

How can I do this?

Upvotes: 0

Views: 5672

Answers (4)

RTK
RTK

Reputation: 243

Remote API 2 (RAPI2). => You lanch two rapi.CreateProcess, one for the date and one for the time.

rapi.CreateProcess("cmd", "/c date " + DateTime.Now.ToString("dd-MM-yyyy"));
rapi.CreateProcess("cmd", "/c time " + DateTime.Now.ToString("HH:mm:ss"));

It will open two windows command prompt to synchronise the mobile with the clock on your PC.

Upvotes: 0

user153923
user153923

Reputation:

Here's the one I have been using. I make my devices sync up the first time the database object is initiated.

#if PocketPC
[DllImport("coredll.dll")]
#else
[DllImport("kernel32.dll")]
#endif
private static extern bool SetLocalTime([In] ref SYSTEMTIME lpLocalTime);

...and yes, I use the same code for my Windows PCs as for my WinMobile PCs.

This NODATE variable is global to my entire project, and is defined as Jan. 1, 1900. Hopefully, nothing at this company happened on that date in the SQL Server!

Start with a structure:

/// <summary>
/// SYSTEMTIME structure with some useful methods
/// </summary>
public struct SYSTEMTIME {
  public short Year, Month, DayOfWeek, Day, Hour, Minute, Second, Millisecond;
  /// <summary>
  /// Convert form System.DateTime
  /// </summary>
  /// <param name="time">Creates System Time from this variable</param>
  public void FromDateTime(DateTime time) {
    Year = (short)time.Year;
    Month = (short)time.Month;
    DayOfWeek = (short)time.DayOfWeek;
    Day = (short)time.Day;
    Hour = (short)time.Hour;
    Minute = (short)time.Minute;
    Second = (short)time.Second;
    Millisecond = (short)time.Millisecond;
  }
  /// <summary>
  /// Convert to System.DateTime
  /// </summary>
  public DateTime ToDateTime() {
    return new DateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
  }
  /// <summary>
  /// STATIC: Convert to System.DateTime
  /// </summary>
  public static DateTime ToDateTime(SYSTEMTIME time) {
    return time.ToDateTime();
  }
}

Then just use that structure like I do in this method that makes the call (I sync with our SQL Server).

/// <summary>
/// Synchronize the Time on the SQL Server with the time on the device
/// </summary>
public static int SyncWithSqlTime() { // pass in SQL Time and set time on device
  SYSTEMTIME st = new SYSTEMTIME();
  DateTime sqlTime = Global.NODATE;
  using (SqlCommand cmd = new SqlCommand("SELECT GETDATE() AS CurrentDateTime;", new SqlConnection(DataSettings.DatabaseConnectionString))) {
    try {
      cmd.Connection.Open();
      SqlDataReader r = cmd.ExecuteReader();
      while (r.Read()) {
        if (!r.IsDBNull(0)) {
          sqlTime = (DateTime)r[0];
        }
      }
    } catch (Exception) {
      return -1;
    }
  }
  if (sqlTime != Global.NODATE) {
    st.FromDateTime(sqlTime); // Convert to SYSTEMTIME
    if (SetLocalTime(ref st)) { //Call Win32 API to set time
      return 1;
    }
  }
  return 0;
}

That last if condition at the bottom is where I set the time using SetLocalTime.

Hope this gets you somewhere,
~Joe

Upvotes: 2

josef
josef

Reputation: 5959

I am not sure what the final question is. If you ask, how to sync the time of the device with the PCs time when the device is dockked, then you can use the free ITSUtils and some registry change on your PC to automate time snycing: The registry change is

REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows CE Services\AutoStartOnConnect]
"OnConnect"="\"c:\\windows\\system32\\cmd.exe\" /c d:\\OnConnect\\install.bat"

see also here and here and here

If you have access, you can also look at my post at experts-exchange.

Instead of install.bat you can use any bat or cmd file.The itsutils contain a PC app called psynctime. You just need to start psynctime in the bat/cmd file you created and linked in the registry. The device time is then synced to your PC's time. Enure that the bat file can access the itsutil files, either have the itsutils in a dir of your PATH environment or in the bat/cmd's directoty.

As an alternative you have to write a DLL and a activesync remote calling code to sync the time. So it is easier to use the psynctime tool.

Do you know that the device can use the internet using your PC's internet connection? See ActiveSync connection settings and look for "PC is connected to Work/Internet/Automatic". If set to Automatic or Internet your device should be able to access the internet passing thru the ActiveSync connection. Just try Internet Explorer Mobile on the device to test that. If it works for you, you can also use the SNTP service with the code provided by Mitch. But then you also have to add code that detects when the device is connected via ActiveSync.

Upvotes: 2

Mitch Wheat
Mitch Wheat

Reputation: 300489

Why not synchronise both to international standard time using SNTP (Network Time Protocol):

C# SNTP Client

Then you can just check the database last updated time. [I've used the linked-to code on a mobile device]

That code is used like so (called every hour, or whatever refresh interval you require):

   SNTPClient client = new SNTPClient(sntpTimeServerString);
   client.Connect(5000, true);

   // Set system clock to timenow 
   DateTime timenow = client.ToTime();

Update (based on OP's comments): Since you have no internet connection, I think you need to use something other than time to determine last sync. For instance, use a last sequence Id to determine if device's local database has changes compared to desktop.

Upvotes: 1

Related Questions