UpgradeDlg.cpp
上传用户:zhanglf88
上传日期:2013-11-19
资源大小:6036k
文件大小:8k
源码类别:

金融证券系统

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "UpgradeDlg.h"
  3. #define ST_UPGRADE_PAUSED 1
  4. #define ST_UPGRADE_DLOK 2
  5. #define ST_UPGRADE_NOTFOUND 3
  6. #define ST_UPGRADE_CANNOTOPENFILE 4
  7. HANDLE CUpgradeDlg::m_hEventKillUpgradeThread = NULL;
  8. HANDLE CUpgradeDlg::m_hEventUpgradeThreadKilled = NULL;
  9. HFILE  CUpgradeDlg::m_hFileUpgrade = NULL;
  10. BOOL CALLBACK UpgradeCallback(DWORD dwCode, DWORD dwProgress, LPCTSTR lpszMsg, void* cookie)
  11. {
  12. HWND hMainWnd = (HWND)cookie;
  13. if (PROG_PROGRESS == dwCode)
  14. {
  15. ::SendMessage(hMainWnd, WM_USER_UPGRADE_PROGRESS, dwProgress, (LPARAM)lpszMsg);
  16. }
  17. else
  18. {
  19. ::SendMessage(hMainWnd, WM_USER_UPGRADE_MESSAGE, dwCode, (LPARAM)lpszMsg);
  20. }
  21. // User wants to quit program
  22. if (WAIT_OBJECT_0 == WaitForSingleObject(CUpgradeDlg::m_hEventKillUpgradeThread, 0))
  23. {
  24. ::PostMessage(hMainWnd, WM_USER_UPGRADE_END, ST_UPGRADE_PAUSED, 0);
  25. SetEvent(CUpgradeDlg::m_hEventUpgradeThreadKilled);
  26. if (NULL != CUpgradeDlg::m_hFileUpgrade)
  27. {
  28. ::CloseHandle((HANDLE)CUpgradeDlg::m_hFileUpgrade);
  29. CUpgradeDlg::m_hFileUpgrade = NULL;
  30. }
  31. AfxEndThread(0, TRUE);
  32. return FALSE;
  33. }
  34. return TRUE;
  35. }
  36. char szUpgradeFileName[] = "clking.exe";
  37. UINT UpgradeMain(LPVOID pParam)
  38. {
  39. LPUPGRADE_INFO pInfo = (LPUPGRADE_INFO)pParam;
  40. PostMessage(pInfo->hMainWnd, WM_USER_UPGRADE_PROGRESS, 0, 0);
  41. CHttpClient http;
  42. http.SetServer(AfxGetProfile().GetHTTPServerDomain(), AfxGetProfile().GetHTTPServerPort());
  43. http.SetAccessType(AfxGetQSProfile().GetAccessType(), AfxGetQSProfile().GetProxyType(),
  44. AfxGetQSProfile().GetProxyAddress(), AfxGetQSProfile().GetProxyPort(),
  45. AfxGetQSProfile().GetProxyUser(), AfxGetQSProfile().GetProxyPasswd());
  46. CFile file;
  47. CString sFileName = AfxGetProfile().GetWorkDirectory() + szUpgradeFileName;
  48. if (file.Open(sFileName, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite))
  49. {
  50. CUpgradeDlg::m_hFileUpgrade = (HFILE)file.m_hFile;
  51. DWORD dwHttpCode = http.WGet(AfxGetProfile().GetUpgradeURL(), &file, UpgradeCallback, pInfo->hMainWnd);
  52. file.Close();
  53. CUpgradeDlg::m_hFileUpgrade = NULL;
  54. if (HTTP_STATUS_OK == dwHttpCode)
  55. {
  56. ::SendMessage(pInfo->hMainWnd, WM_USER_UPGRADE_END, ST_UPGRADE_DLOK, 0);
  57. }
  58. else
  59. {
  60. ::SendMessage(pInfo->hMainWnd, WM_USER_UPGRADE_END, ST_UPGRADE_NOTFOUND, 0);
  61. }
  62. }
  63. else
  64. {
  65. ::SendMessage(pInfo->hMainWnd, WM_USER_UPGRADE_END, ST_UPGRADE_CANNOTOPENFILE, 0);
  66. if (NULL != CUpgradeDlg::m_hFileUpgrade)
  67. {
  68. ::CloseHandle((HANDLE)CUpgradeDlg::m_hFileUpgrade);
  69. CUpgradeDlg::m_hFileUpgrade = NULL;
  70. }
  71. }
  72. AfxEndThread(0, TRUE);
  73. return 0;
  74. }
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CUpgradeDlg dialog
  77. BEGIN_MESSAGE_MAP(CUpgradeDlg, CDialog)
  78. //{{AFX_MSG_MAP(CUpgradeDlg)
  79. ON_BN_CLICKED(IDC_PROXY_SET, OnProxySet)
  80. ON_MESSAGE(WM_USER_UPGRADE_MESSAGE, OnUpgradeMessage)
  81. ON_MESSAGE(WM_USER_UPGRADE_PROGRESS, OnUpgradeProgress)
  82. ON_MESSAGE(WM_USER_UPGRADE_END, OnUpgradeEnd)
  83. //}}AFX_MSG_MAP
  84. END_MESSAGE_MAP()
  85. CUpgradeDlg::CUpgradeDlg(CWnd* pParent /*=NULL*/) : CDialog(CUpgradeDlg::IDD, pParent)
  86. {
  87. m_pUpgradeInfo = NULL;
  88. m_bAutoRun = FALSE;
  89. }
  90. CUpgradeDlg::~CUpgradeDlg()
  91. {
  92. if (m_pUpgradeInfo)
  93. {
  94. delete m_pUpgradeInfo;
  95. m_pUpgradeInfo = NULL;
  96. }
  97. }
  98. void CUpgradeDlg::SetAutoRun(BOOL bAutoRun)
  99. {
  100. m_bAutoRun = bAutoRun;
  101. }
  102. void CUpgradeDlg::DoDataExchange(CDataExchange* pDX)
  103. {
  104. CDialog::DoDataExchange(pDX);
  105. //{{AFX_DATA_MAP(CUpgradeDlg)
  106. DDX_Control(pDX, IDOK, m_btnOK);
  107. DDX_Control(pDX, IDCANCEL, m_btnCancel);
  108. DDX_Control(pDX, IDC_STATIC_STATUS, m_staticStatus);
  109. DDX_Control(pDX, IDC_STATIC_PROGRESS, m_staticProgress);
  110. DDX_Control(pDX, IDC_PROXY_SET, m_btnProxySet);
  111. DDX_Control(pDX, IDC_EDIT_EXPLAIN, m_editExplain);
  112. DDX_Control(pDX, IDC_DL_PROGRESS, m_ctrlProgress);
  113. //}}AFX_DATA_MAP
  114. }
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CUpgradeDlg message handlers
  117. BOOL CUpgradeDlg::OnInitDialog() 
  118. {
  119. CDialog::OnInitDialog();
  120. HRSRC hReSrc = NULL;
  121. HGLOBAL hRead = NULL;
  122. LPCTSTR lpText;
  123. HINSTANCE hModule = AfxGetResourceHandle();
  124. hReSrc = ::FindResource(hModule, MAKEINTRESOURCE(IDR_TEXT_UPGRADEEXPLAIN), _T("Text"));
  125. if (hReSrc)
  126. hRead = ::LoadResource(hModule, hReSrc);
  127. if (hRead)
  128. {
  129. lpText = (LPCTSTR)::LockResource(hRead);
  130. if (lpText)
  131. {
  132. m_editExplain.SetWindowText(lpText);
  133. }
  134. ::UnlockResource(hRead);
  135. }
  136. m_staticStatus.SetWindowPos(NULL, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_HIDEWINDOW);
  137. m_ctrlProgress.SetWindowPos(NULL, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_HIDEWINDOW);
  138. m_ctrlProgress.SetRange(0, STKLIB_MAX_PROGRESS);
  139. m_ctrlProgress.SetPos(0);
  140. if (m_bAutoRun)
  141. OnOK();
  142. return TRUE;
  143. }
  144. void CUpgradeDlg::OnOK() 
  145. {
  146. UpdateData();
  147. // disable and enble buttons
  148. m_btnOK.EnableWindow(FALSE);
  149. m_btnProxySet.EnableWindow(FALSE);
  150. // show message
  151. m_staticStatus.SetWindowPos(NULL, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_SHOWWINDOW);
  152. m_ctrlProgress.SetWindowPos(NULL, 0,0,0,0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_SHOWWINDOW);
  153. CUpgradeDlg::m_hEventKillUpgradeThread = CreateEvent(NULL,FALSE,FALSE,NULL);
  154. CUpgradeDlg::m_hEventUpgradeThreadKilled = CreateEvent(NULL,FALSE,FALSE,NULL);
  155. if (NULL == m_pUpgradeInfo)
  156. m_pUpgradeInfo = new UPGRADE_INFO;
  157. memset(m_pUpgradeInfo, 0, sizeof(UPGRADE_INFO));
  158. m_pUpgradeInfo->hMainWnd = GetSafeHwnd();
  159. AfxBeginThread(UpgradeMain, (LPVOID)m_pUpgradeInfo, THREAD_PRIORITY_NORMAL);
  160. }
  161. void CUpgradeDlg::OnCancel() 
  162. {
  163. SetEvent(m_hEventKillUpgradeThread);
  164. // WaitForSingleObject(m_hEventUpgradeThreadKilled, INFINITE);
  165. CDialog::OnCancel();
  166. }
  167. void CUpgradeDlg::OnProxySet() 
  168. {
  169. AfxDoProxySetting();
  170. }
  171. LRESULT CUpgradeDlg::OnUpgradeMessage(WPARAM wParam, LPARAM lParam)
  172. {
  173. CString strNewMsg;
  174. CString strOldMsg;
  175. m_staticStatus.GetWindowText(strOldMsg);
  176. CString strMsg = CNetDatabase::GetProgressMessage(wParam);
  177. if (strMsg.GetLength() > 0)
  178. {
  179. strNewMsg = strOldMsg;
  180. int nIndex = strOldMsg.Find(" -- ");
  181. if (-1 != nIndex)
  182. strNewMsg = strOldMsg.Left(nIndex);
  183. strNewMsg += " -- " + strMsg;
  184. }
  185. if (strNewMsg.GetLength() > 0)
  186. {
  187. if (0 != strNewMsg.Compare(strOldMsg))
  188. m_staticStatus.SetWindowText(strNewMsg);
  189. }
  190. return 1;
  191. }
  192. LRESULT CUpgradeDlg::OnUpgradeProgress(WPARAM wParam, LPARAM lParam)
  193. {
  194. // set message and enable buttons
  195. if (wParam < 0)
  196. wParam = 0;
  197. if (wParam > STKLIB_MAX_PROGRESS-1)
  198. wParam = STKLIB_MAX_PROGRESS-1;
  199. m_ctrlProgress.SetPos(wParam);
  200. CString strProgressNew;
  201. strProgressNew.Format("%.2f%%", float(wParam)/100.);
  202. CString strProgressOld;
  203. m_staticProgress.GetWindowText(strProgressOld);
  204. if (0 != strProgressNew.Compare(strProgressOld))
  205. m_staticProgress.SetWindowText(strProgressNew);
  206. return 1;
  207. }
  208. LRESULT CUpgradeDlg::OnUpgradeEnd(WPARAM wParam, LPARAM lParam)
  209. {
  210. m_ctrlProgress.SetPos(STKLIB_MAX_PROGRESS);
  211. CString strProgressNew;
  212. strProgressNew.Format("%.2f%%", 100.);
  213. m_staticProgress.SetWindowText(strProgressNew);
  214. // set message and enable buttons
  215. CString strMsg;
  216. if (ST_UPGRADE_PAUSED == wParam)
  217. strMsg.LoadString(IDS_UPGRADE_PAUSED);
  218. else if (ST_UPGRADE_NOTFOUND == wParam)
  219. strMsg.LoadString(IDS_UPGRADE_NOTFOUND);
  220. else if (ST_UPGRADE_DLOK == wParam)
  221. strMsg.LoadString(IDS_UPGRADE_DLOK);
  222. else if (ST_UPGRADE_CANNOTOPENFILE == wParam)
  223. strMsg.LoadString(IDS_UPGRADE_CANNOTOPENFILE);
  224. else
  225. ASSERT(FALSE);
  226. m_staticStatus.SetWindowText(strMsg);
  227. if (ST_UPGRADE_DLOK == wParam)
  228. {
  229. // Upgrade Now
  230. AfxGetMainFrame()->PostMessage(WM_CLOSE);
  231. ShellExecute(NULL, "open", AfxGetProfile().GetWorkDirectory()+szUpgradeFileName,
  232.   NULL, AfxGetProfile().GetWorkDirectory(), SW_SHOW);
  233. }
  234. m_btnOK.EnableWindow(TRUE);
  235. m_btnCancel.EnableWindow(TRUE);
  236. m_btnProxySet.EnableWindow(TRUE);
  237. CloseHandle(CUpgradeDlg::m_hEventKillUpgradeThread);
  238. CloseHandle(CUpgradeDlg::m_hEventUpgradeThreadKilled);
  239. return 1;
  240. }