WinCE系統獲取遠程服務器時間來設置本地時間

  在做項目時用datalogic手持終端設備遇到一個問題,系統冷啓動或斷電後再啓動這時系統的時間會還原掉,當然重新設置系統時間這是沒問題的,但就是有點麻煩,每次都要設置,如果我們限制手持機的使用功能,比如說鎖住桌面不讓用戶做其它操作,這時用戶根本就沒辦法手動設置系統時間,那能不能能過代碼獲取遠程服務器時間來設置本地時間呢?可通過API來修改系統的時間,代碼如下:
1. [b]聲明代碼[/b]


//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. [b]調用代碼示例:[/b]

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);
}
發佈了34 篇原創文章 · 獲贊 0 · 訪問量 2601
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章