📄 upgradeatx.cpp
字号:
// UpgradeAtx.cpp : Implementation of CUpgradeAtx
#include "stdafx.h"
#include "UpgradeAtv.h"
#include "UpgradeAtx.h"
#include <afxinet.h> //for WinInet
#include <comdef.h> //for _com_raise_error() function
#include "UpgradeSession.h" //for CUpgradeSession
#include <atlbase.h> //for CRegKey
/////////////////////////////////////////////////////////////////////////////
// CUpgradeAtx
//##ModelId=3E5F16D903A8
STDMETHODIMP CUpgradeAtx::Download(VARIANT_BOOL *ret)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
*ret = VARIANT_FALSE;
//初始化刻度
m_uFullScale = 0;
m_uCurrentScale = 0;
//0. 取得文件的长度
try
{
m_uFullScale = GetFileLength();
}
catch(_com_error &e)
{
return e.Error();
}
//1. 设置状态为正在连接。
//2.开始下载
CUpgradeSession session("FTP Session");
session.EnableStatusCallback(TRUE);
CFtpConnection* pConn = NULL;
CInternetFile* pFtpFile = NULL;
try
{
//取得FTP连接
pConn = session.GetFtpConnection(m_strServerName, "hacker", "free");
//得到文件
pFtpFile = pConn->OpenFile(m_strRemoteFile, GENERIC_READ);
if (NULL == pFtpFile)
{
//display an error
delete pConn;
AfxMessageBox("GetFile Error!");
}
//打开本地文件。
CFile file;
file.Open(m_strLocalFile, CFile::modeCreate
|CFile::modeWrite|CFile::typeBinary);
//分块下载
char buf[1024] = {0};
UINT uCount = 0;
do
{
uCount = pFtpFile->Read(buf, sizeof(buf));
file.Write(buf, uCount);
Proceed(uCount);
//TRACE("%u\n", uCount);
}while(uCount >= sizeof(buf));
pFtpFile->Close();
delete pFtpFile;
file.Close();
pConn->Close();
delete pConn;
session.Close();
}
/**
* 关于CException和它的派生类
*
* CException对象不能用new来进行分配。
* 如果想要在 heap 上生成一个CException对象,那么使用构造函数
* CException::CException(TRUE);如果想要在 stack 或者是 global
* 变量区生成一个 CException对象,使用构造函数 CException(FALSE)。
*
* 因为不是用 new 生成的,所以在 catch 中也不能使用 delete 来进行销毁。
* 对于一个在 catch(CException &e) 中获得的对象,使用 e->Delete() 来
* 来进行销毁。
*/
catch(CInternetException *e)
{
delete pConn;
session.Close();
AfxMessageBox("CInternetException Error!");
e->Delete();
}
catch(CFileException *e)
{
delete pConn;
e->ReportError();
e->Delete();
}
catch(...)
{
delete pConn;
session.Close();
}
//下载结束。
return S_OK;
}
//##ModelId=3E5F16D9039E
STDMETHODIMP CUpgradeAtx::IsAppExisting(VARIANT_BOOL *ret)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
// TODO: Add your implementation code here
*ret = VARIANT_FALSE;
CRegKey reg;
DWORD dwRet = 0;
do
{
dwRet = reg.Create(HKEY_LOCAL_MACHINE, _T("Software\\VCBook\\WMWB"));
if (dwRet != ERROR_SUCCESS)
{
*ret = VARIANT_FALSE;
break;
}
CString strValue('\x0', 255);
DWORD dwLen = strValue.GetLength();
dwRet = reg.QueryValue(strValue.GetBuffer(255), "Version", &dwLen);
if (dwRet != ERROR_SUCCESS)
{
*ret = VARIANT_FALSE;
break;
}
*ret = VARIANT_TRUE;
}while(false);
return S_OK;
}
//##ModelId=3E5F16DA0054
bool CUpgradeAtx::GetParameter()
{
return true;
}
//##ModelId=3E5F16DA004A
DWORD CUpgradeAtx::GetFileLength()
{
//1. 创建查找文件长度的Session
CInternetSession session("FTPFileLength Session");
//2. FTP FileFinder connection
CFtpConnection* pConn = NULL;
BOOL bRet = FALSE;
DWORD dwLength = 0;
try
{
//3.取得FTP 连接
pConn = session.GetFtpConnection(m_strServerName, "hacker", "free");
//4.使用CFtpFileFind为取得文件长度做准备
CFtpFileFind finder(pConn);
do
{
bRet = finder.FindFile(m_strRemoteFile);
if (!bRet)
break;
finder.FindNextFile();
//5. 取得文件的长度。
dwLength = finder.GetLength();
}while(false);
}
catch (CInternetException* pEx)
{
TCHAR sz[1024];
pEx->GetErrorMessage(sz, 1024);
pEx->Delete();
}
//6. 销毁对象,关闭连接。
if (NULL != pConn)
pConn->Close();
delete pConn;
//7. 返回结果,或者抛出异常。
if (bRet)
{
return dwLength;
}
else
{
_com_issue_error(E_FAIL);
return 0;
}
}
//##ModelId=3E623B220141
void CUpgradeAtx::Proceed(UINT scale)
{
//TRACE("Proceed\n");
m_uCurrentScale += scale;
CWnd *pWnd = CWnd::FromHandle(m_hWnd);
pWnd->Invalidate(TRUE);
MSG Msg;
if(PeekMessage(&Msg, pWnd->m_hWnd,WM_PAINT,WM_PAINT,PM_REMOVE))
{
DispatchMessage(&Msg);
}
}
//##ModelId=3E623B210032
STDMETHODIMP CUpgradeAtx::DoSetup()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
WinExec(m_strLocalFile, SW_SHOW);
return S_OK;
}
//##ModelId=3E623B21001E
HRESULT STDMETHODCALLTYPE CUpgradeAtx::SetClientSite(IOleClientSite *pClientSite)
{
if (pClientSite)
{
RECT rc = {0,0,0,0};
// Don't have access to the container's window so just use the
// desktop. Window will be resized correctly during in-place
// activation.
HWND hWnd = CreateControlWindow(::GetDesktopWindow(), rc);
_ASSERT (hWnd);
}
return IOleObjectImpl<CUpgradeAtx>::SetClientSite (pClientSite);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -