⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 updatedlg.cpp

📁 这个是基于http的再线更新程序
💻 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 = CString(buf);
	m_strAppPath = str.Left(str.ReverseFind('\\') + 1);
}

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 == "更新")
	{
		Update();
		return;
	}
	else if (str == "完成")
	{
		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("更新");
}

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("完成");
			EndWaitCursor();
			return;
		}
	}

	m_lbProduct.DeleteString(0);
	m_lbProduct.AddString(m_strSoft + " " + m_strNewVer);
	m_buOK.SetWindowText("完成");
	m_strStatus = "完成更新!";
	UpdateData(FALSE);
	::DeleteFile(m_strTempDir + "\\update.ini");

	// 保存当前程序号
	WritePrivateProfileString(m_strSoft, "VERSION", m_strNewVer, m_strAppPath + "\\Version.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); 
	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, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary | CFile::shareDenyWrite))
		{
			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();
	}

	m_strStatus = strFile + "完成更新!";
	UpdateData(FALSE);
	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -