?? updatedlg.cpp
字號:
// UpdateDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Update.h"
#include "UpdateDlg.h"
#include <tlhelp32.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUpdateDlg dialog
CUpdateDlg::CUpdateDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUpdateDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUpdateDlg)
m_strStatus = _T("點擊下一步查看可用的更新");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
char buf[MAX_PATH];
::GetTempPath(sizeof(buf),buf);
m_strTempDir=buf;
m_dwHttpRequestFlags=HSR_DOWNLOAD | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT;
//獲得路徑
::GetModuleFileName(NULL,buf,sizeof(buf));
CString str=buf;
int m=str.ReverseFind('\\');
m_strAppPath=str.Left(m+1);
str.Empty();
}
void CUpdateDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUpdateDlg)
DDX_Control(pDX, IDOK, m_buOK);
DDX_Control(pDX, IDC_LIST_PRODUCT, m_lbProduct);
DDX_Control(pDX, IDC_PROGRESS1, m_prog);
DDX_Text(pDX, IDC_STATIC_STATUS, m_strStatus);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUpdateDlg, CDialog)
//{{AFX_MSG_MAP(CUpdateDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUpdateDlg message handlers
BOOL CUpdateDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5);
m_pHttp=m_cis.GetHttpConnection(m_strServer,m_dwPort);
m_lbProduct.AddString(m_strSoft+" "+m_strVersion);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CUpdateDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CUpdateDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CUpdateDlg::OnOK()
{
CString str;
m_buOK.GetWindowText(str);
if(str=="更新(&U)")
{
Update();
return;
}
else if(str=="完成(&O)")
{
CDialog::OnOK();
return;
}
m_strStatus="正在連接到服務器...";
UpdateData(FALSE);
CHttpFile *pFile = m_pHttp->OpenRequest(CHttpConnection::HTTP_VERB_GET,
m_strIniPath, NULL, 1, NULL, NULL, m_dwHttpRequestFlags);
if(!pFile->SendRequest())
{
m_strStatus="連接服務器失敗!";
UpdateData(FALSE);
pFile->Close();
return;
}
m_strStatus="讀取版本信息...";
UpdateData(FALSE);
if(pFile)
{
CStdioFile csf;
csf.Open(m_strTempDir+"\\update.ini",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
char buf[2048];
int n;
while((n=pFile->Read(buf,2048))>0)
csf.Write(buf,n);
csf.Close();
pFile->Close();
}
char buf[128];
::GetPrivateProfileString(m_strSoft,"VERSION","1.0",buf,sizeof(buf),m_strTempDir+"\\update.ini");
m_strNewVer=buf;
if(atof(m_strVersion)>=atof(buf))//現有版本大于
{
m_strStatus="您現在用的版本已是最新的!";
UpdateData(FALSE);
m_buOK.EnableWindow(FALSE);
return;
}
m_strStatus="發現新的版本:";
m_strStatus+=m_strSoft+" "+buf;
UpdateData(FALSE);
m_buOK.SetWindowText("更新(&U)");
}
BOOL CUpdateDlg::DestroyWindow()
{
m_pHttp->Close();
m_cis.Close();
return CDialog::DestroyWindow();
}
void CUpdateDlg::Update()
{
BeginWaitCursor();
DWORD id=FindAppProcessID();
if(id!=-1)
{
if(AfxMessageBox(m_strSoft+"已在運行!\n請先關閉程序才能進行更新!\n如果選擇確定將強行關閉程序!",MB_OKCANCEL)==IDOK)
{
HANDLE ProcessHandle=OpenProcess(PROCESS_ALL_ACCESS,FALSE,id);
if(ProcessHandle)TerminateProcess(ProcessHandle,0);
}
}
char buf[MAX_PATH];
for(int i=0;;i++)
{
CString str;
str.Format("FILE%d",i);
::GetPrivateProfileString(m_strSoft,str,"",buf,sizeof(buf),m_strTempDir+"\\update.ini");
if(strlen(buf)<1)break;//退出
if(!UpdateFile(buf))
{
m_buOK.SetWindowText("完成(&O)");
EndWaitCursor();
return;
}
}
m_lbProduct.DeleteString(0);
m_lbProduct.AddString(m_strSoft+" "+m_strNewVer);
m_buOK.SetWindowText("完成(&O)");
m_strStatus="完成更新!";
UpdateData(FALSE);
::DeleteFile(m_strTempDir+"\\update.ini");
EndWaitCursor();
}
DWORD CUpdateDlg::FindAppProcessID()
{
HANDLE handle=::CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
PROCESSENTRY32 Info;
Info.dwSize = sizeof(PROCESSENTRY32);
if(::Process32First(handle,&Info))
{
do{
CString ss=Info.szExeFile;
if(!ss.CompareNoCase(m_strSoft+".exe"))
{
::CloseHandle(handle);
return Info.th32ProcessID;
}
}
while(::Process32Next(handle,&Info));
::CloseHandle(handle);
}
return -1;
}
BOOL CUpdateDlg::UpdateFile(CString strFile)
{
CHttpFile *pFile =m_pHttp->OpenRequest(CHttpConnection::HTTP_VERB_GET,
strFile, NULL, 1, NULL, NULL, m_dwHttpRequestFlags);
// pFile->AddRequestHeaders(szHeaders);
try{
pFile->SendRequest();
}
catch(CInternetException* pEx)
{
TCHAR szError[1024];
pEx->GetErrorMessage(szError,1024);
AfxMessageBox(szError);
pFile->Close();
return FALSE;
}
m_strStatus="正在下載文件:"+strFile;
UpdateData(FALSE);
CString str;
if(pFile)
{
pFile->QueryInfo(HTTP_QUERY_STATUS_CODE,str);
if(str=="404")
{
m_strStatus=strFile+"不存在!";
UpdateData(FALSE);
pFile->Close();
return FALSE;
}
pFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,str);
DWORD dwLen=atol(str);
int n=strFile.ReverseFind('/');
str=m_strAppPath+strFile.Right(strFile.GetLength()-n-1);
CStdioFile csf;
if(!csf.Open(str+".upg",CFile::modeCreate|CFile::modeWrite | CFile::typeBinary | CFile::shareDenyWrite))
{//先為*.upg文件
AfxMessageBox("寫文件"+str+"錯誤!\n文件正在使用中,請先關閉程序!",MB_ICONSTOP);
pFile->Close();
return FALSE;
}
char buf[2048];
DWORD dwRead=0;
while((n=pFile->Read(buf,sizeof(buf)))>0)
{
dwRead+=n;
m_prog.SetPos(100*dwRead/dwLen);
MSG msg;
for(int i=0;i<10;i++)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
csf.Write(buf,n);
}
pFile->Close();
}
if(::DeleteFile(str)){
::rename(str+".upg",str);
m_strStatus=strFile+"完成更新!";
UpdateData(FALSE);
}
return TRUE;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -