WinCE系统获取远程服务器时间来设置本地时间

阅读更多
  在做项目时用datalogic手持终端设备遇到一个问题,系统冷启动或断电后再启动这时系统的时间会还原掉,当然重新设置系统时间这是没问题的,但就是有点麻烦,每次都要设置,如果我们限制手持机的使用功能,比如说锁住桌面不让用户做其它操作,这时用户根本就没办法手动设置系统时间,那能不能能过代码获取远程服务器时间来设置本地时间呢?可通过API来修改系统的时间,代码如下:
1.  声明代码

       //imports SetLocalTime function from kernel32.dll 
       [DllImport("coredll.dll", SetLastError = true)]
       public static extern int SetLocalTime(ref SystemTime lpSystemTime);
       //struct for date/time apis 
       public struct SystemTime
       {
           public short wYear;
           public short wMonth;
           public short wDayOfWeek;
           public short wDay;
           public short wHour;
           public short wMinute;
           public short wSecond;
           public short wMilliseconds;
       }

2.  调用代码示例:
try
{
    // 通过WebService获取服务器时间
    SCM.SOAPService soap = new SCM.SOAPService();
    DateTime remoteTime = soap.getSystemTime();
    // 给结构体赋值
    SystemTime newTime = new SystemTime();
    newTime.wYear = (short)remoteTime.Year;
    newTime.wMonth = (short)remoteTime.Month;
    newTime.wDay = (short)remoteTime.Day;
    newTime.wHour = (short)remoteTime.Hour;
    newTime.wMinute = (short)remoteTime.Minute;
    newTime.wSecond = (short)remoteTime.Second;
    newTime.wMilliseconds = (short)remoteTime.Millisecond;
    newTime.wDayOfWeek = (short)remoteTime.DayOfWeek;
    // 设置本地时间
    SetLocalTime(ref newTime);
}
catch 
{
    MessageBox.Show("获取服务器时间设置本地时间失败!", "错误提示",MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
}

你可能感兴趣的:(WinCE,WebService,SOAP,C,C++)