// EMsgBoxUpdate.cpp : 定义应用程序的类行为。 // #include "stdafx.h" #include "EMsgBoxUpdate.h" #include "EMsgBoxUpdateDlg.h" #include <afxinet.h> #ifdef _DEBUG #define new DEBUG_NEW #endif // CEMsgBoxUpdateApp BEGIN_MESSAGE_MAP(CEMsgBoxUpdateApp, CWinApp) ON_COMMAND(ID_HELP, &CWinApp::OnHelp) END_MESSAGE_MAP() // CEMsgBoxUpdateApp 构造 CEMsgBoxUpdateApp::CEMsgBoxUpdateApp() { // 支持重新启动管理器 m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART; // TODO: 在此处添加构造代码, // 将所有重要的初始化放置在 InitInstance 中 } // 唯一的一个 CEMsgBoxUpdateApp 对象 CEMsgBoxUpdateApp theApp; // CEMsgBoxUpdateApp 初始化 BOOL CEMsgBoxUpdateApp::InitInstance() { // 如果一个运行在 Windows XP 上的应用程序清单指定要 // 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式, //则需要 InitCommonControlsEx()。否则,将无法创建窗口。 INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); // 将它设置为包括所有要在应用程序中使用的 // 公共控件类。 InitCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&InitCtrls); CWinApp::InitInstance(); AfxEnableControlContainer(); // 创建 shell 管理器,以防对话框包含 // 任何 shell 树视图控件或 shell 列表视图控件。 CShellManager *pShellManager = new CShellManager; try { // 关闭小助手进程 HANDLE hProcessHandle; ULONG nProcessID; HWND theWindow; theWindow=::FindWindowW(NULL,_T("******")); ::GetWindowThreadProcessId(theWindow,&nProcessID); hProcessHandle = ::OpenProcess( PROCESS_TERMINATE, FALSE, nProcessID ); ::TerminateProcess( hProcessHandle, 4 ); //下载更新文件 CString basePath=m_Recorder.GetIsSetIP()? _T("http://")+m_Recorder.GetServerIp()+_T(":")+m_Recorder.GetServerPort(): m_Recorder.GetHttpHead()+m_Recorder.GetServerHttpAddr(); CString m_theUrl = basePath+_T("/LoadFile/update/update.zip"); CString localFilename=m_Recorder.GetAppPath()+_T("/update.dat"); //删除上次更新的文件 DeleteFile(localFilename); CInternetSession session; CInternetFile* remoteFile =(CInternetFile*)session.OpenURL(m_theUrl,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_RELOAD ); if(!remoteFile ) { remoteFile->Close(); delete remoteFile; session.Close(); MessageBox(NULL,_T("下载更新文件失败! "),_T("提示"),MB_ICONERROR); return FALSE; } DWORD statusCode; ((CHttpFile *)remoteFile)->QueryInfoStatusCode(statusCode); if (statusCode==404) { MessageBox(NULL,_T("下载更新文件失败! "),_T("提示"),MB_ICONERROR); return FALSE; } CFile localFile;//MZ? if(localFile.Open(localFilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,NULL)) { int readlen = -1; char buf[1024]; readlen = remoteFile->Read(buf,1024); if(readlen == 0||buf[0]!='M'||buf[1]!='Z')//是否为可执行文件 { MessageBox(NULL,_T("下载更新文件失败! "),_T("提示"),MB_ICONERROR); remoteFile->Close(); delete remoteFile; session.Close(); return FALSE; } localFile.Write(buf,readlen); ZeroMemory(buf,1024); while(1) { readlen = remoteFile->Read(buf,1024); if(readlen == 0) { break; } localFile.Write(buf,readlen); ZeroMemory(buf,1024); } localFile.Close(); } else { MessageBox(NULL,_T("下载更新文件失败! "),_T("提示"),MB_ICONERROR); remoteFile->Close(); delete remoteFile; session.Close(); return FALSE; } remoteFile->Close(); session.Close(); delete remoteFile; CString curPath=m_Recorder.GetAppPath(); WIN32_FIND_DATA fileinfo; HANDLE hFile= FindFirstFile(localFilename,&fileinfo); if(hFile==INVALID_HANDLE_VALUE) { MessageBox(NULL,_T("下载更新文件失败! "),_T("提示"),MB_ICONERROR); return FALSE; } BOOL ret=FALSE; ret=MoveFileEx(curPath+_T("/E时代邮件小助手.exe"),curPath+_T("/update.bak"), MOVEFILE_REPLACE_EXISTING); if(!ret) { MessageBox(NULL,_T("备份原文件失败! "),_T("提示"),MB_ICONERROR); return FALSE; } ret=MoveFileEx(localFilename,curPath+_T("/*******.exe"), MOVEFILE_REPLACE_EXISTING); if(!ret) { MessageBox(NULL,_T("未能找到更新包update.dat! "),_T("提示"),MB_ICONERROR); MoveFileEx( m_Recorder.GetAppPath()+_T("/update.bak"), m_Recorder.GetAppPath()+_T("/*****.exe"), MOVEFILE_REPLACE_EXISTING); return FALSE; } CString updateHtmPath=basePath+_T("/LoadFile/update/update.htm"); CEMsgBoxUpdateDlg dlg(updateHtmPath); m_pMainWnd = &dlg; INT_PTR nResponse = dlg.DoModal(); m_Recorder.SetIsAutoLogin(FALSE); m_Recorder.SetCurrentVersion(m_lpCmdLine); return TRUE; } catch(...) { MessageBox(NULL,_T("更新文件失败! "),_T("提示"),MB_ICONERROR); //还原文件 MoveFileEx( m_Recorder.GetAppPath()+_T("/update.bak"), m_Recorder.GetAppPath()+_T("/*****.exe"), MOVEFILE_REPLACE_EXISTING); return FALSE; } // 删除上面创建的 shell 管理器。 if (pShellManager != NULL) { delete pShellManager; } // 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序, // 而不是启动应用程序的消息泵。 return FALSE; }
MFC 下载文件
最新推荐文章于 2023-05-09 17:57:34 发布