========================================================== ==================================
Title: Problem about SetLocalTime failure of the WinCE Release version Program
Note: wince6 + vs2005
Date: 2011.6.14
Name: Zhu minglei
========================================================== ==================================
// Set the system date <br/> void CCalrDlg: OnBnClickedBtnUse () <br/>{< br/> SYSTEMTIME smCurSel, smCurTime, smSetTime; <br/> m_MCC.GetCurSel (& smCurSel); <br/> GetLocalTime (& smCurTime); <br/> smSetTime. wYear = smCurSel. wYear; <br/> smSetTime. wMonth = smCurSel. wMonth; <br/> smSetTime. wDay = smCurSel. wDay; <br/> smSetTime. wHour = smCurTime. wHour; <br/> smSetTime. wMinute = smCurTime. wMinute; <br/> smSetTime. wSecond = smCurTime. wSecond; <br/> SetLocalTime (& smSetTime); <br/> m_MCC.SetToday (smSetTime); </p> <p >}</p> <p>
This code has no problem in the Debug version and can be set successfully. However, problems may occur in the Release version. At first, I thought it was a problem somewhere else. Later I found that the SetLocalTime call failed. The error code returned through GetLastError is 87, which indicates that the parameter is invalid. After a breakpoint was set up, it was found that the value of wMilliseconds, a millisecond member of smSetTime, is a five-digit value. If the parameter is invalid, it means it.
// Set the system date <br/> void CCalrDlg: OnBnClickedBtnUse () <br/>{< br/> SYSTEMTIME smCurSel, smCurTime, smSetTime; <br/> m_MCC.GetCurSel (& smCurSel); <br/> GetLocalTime (& smCurTime); <br/> smSetTime. wYear = smCurSel. wYear; <br/> smSetTime. wMonth = smCurSel. wMonth; <br/> smSetTime. wDay = smCurSel. wDay; <br/> smSetTime. wHour = smCurTime. wHour; <br/> smSetTime. wMinute = smCurTime. wMinute; <br/> smSetTime. wSecond = smCurTime. wSecond; <br/> smSetTime. wMilliseconds = 0; <br/> SetLocalTime (& smSetTime); <br/> m_MCC.SetToday (smSetTime); </p> <p >}</p> <p>
If wMilliseconds is assigned 0, the SetLocalTime call is successful. In fact, if you maintain a good programming habit and initialize the variable during definition, this problem can be avoided.
// Set the system date <br/> void ccalrdlg: onbnclickedbtnuse () <br/>{< br/> systemtime smcursel = {0}, smcurtime = {0 }, smsettime = {0 }; <br/> m_mcc.getcursel (& smcursel); <br/> getlocaltime (& smcurtime); <br/> smsettime. wyear = smcursel. wyear; <br/> smsettime. wmonth = smcursel. wmonth; <br/> smsettime. wday = smcursel. wday; <br/> smsettime. whour = smcurtime. whour; <br/> smsettime. wminute = smcurtime. wminute; <br/> smsettime. wsecond = smcurtime. wsecond; <br/> setlocaltime (& smsettime); <br/> m_mcc.settoday (smsettime); <br/>}</P> <p>
The Release version does have much stricter requirements than the Debug version.