问一个关于FTP下载的问题,棘手!

zdrone 2008-05-23 12:51:44
代码如下:
#define WM_MYMESSAGE_EXCUTE (WM_USER+100)
class CAutoUpdateDlg : public CDialog
{
// 构造
public:
CAutoUpdateDlg(CWnd* pParent = NULL); // 标准构造函数
public:
void CloseRunApp(); //关闭应用程序
void ResumeRunApp(); //重新启动应用程序
void GetFtpFile(); //下载并更新文件
// 对话框数据
enum { IDD = IDD_AUTOUPDATE_DIALOG };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持


// 实现
protected:
HICON m_hIcon;

// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
afx_msg void OnNMCustomdrawProgress1(NMHDR *pNMHDR, LRESULT *pResult);
CProgressCtrl m_progress;
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg void OnBnClickedButton1();
};









BEGIN_MESSAGE_MAP(CAutoUpdateDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, &CAutoUpdateDlg::OnBnClickedOk)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_PROGRESS1, &CAutoUpdateDlg::OnNMCustomdrawProgress1)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON1, &CAutoUpdateDlg::OnBnClickedButton1)
ON_MESSAGE(WM_MYMESSAGE_EXCUTE,OnMyMessage)
END_MESSAGE_MAP()


// CAutoUpdateDlg 消息处理程序
VOID CALLBACK TimerProc( HWND hwnd,
UINT uMsg,
UINT_PTR idEvent,
DWORD dwTime
)
{
::KillTimer(hwnd,2);
::PostMessage(hwnd,WM_MYMESSAGE_EXCUTE,0,0);
}
BOOL CAutoUpdateDlg::OnInitDialog()
{
......

// TODO: 在此添加额外的初始化代码
m_progress.SetRange(0,100);
m_progress.SetStep(1);

::SetTimer(this->m_hWnd,1,100,NULL); //定时器1 负责进度条
this->SetTimer(2,1000,(TIMERPROC)TimerProc); //定时器2 负责发送消息调用OnMyMessage下载并更新文件

//OnOK();

return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}


void CAutoUpdateDlg::CloseRunApp()
{
//close rdd.exe or gov.exe
try{

HWND hWnd;
hWnd=::FindWindow("TApplication","统计新时空经贸数据无线申报系统");
//统计新时空经贸数据无线申报系统

DWORD dwProcessId;
// 得到该窗口的进程ID
GetWindowThreadProcessId(hWnd,&dwProcessId);

//HANDLE handle=OpenProcess(0,FALSE,dwProcessId);
HANDLE handle=OpenProcess(PROCESS_TERMINATE,FALSE,dwProcessId);
DWORD err;
err=::GetLastError();
CString strErr;
strErr.Format("%d",err);
TRACE(strErr);
// 强制终止进程
TerminateProcess(handle,0);
}
catch(CException *e)
{
e->ReportError();
}

}
void CAutoUpdateDlg::ResumeRunApp()
{
WinExec("RDD.exe",SW_SHOW);
}
void CAutoUpdateDlg::GetFtpFile()
{
CString strServer,strUser,strPwd,strFilename;
int intPort=21;
strServer.Format("192.168.1.220");
strUser.Format("zd");
strPwd.Format("123");

CInternetSession *iSession;
CFtpConnection *ftpConnection;

try
{
iSession=new CInternetSession(AfxGetAppName(),1);
ftpConnection=iSession->GetFtpConnection(strServer,strUser,strPwd,intPort);

ftpConnection->GetFile(_T("ReportTemplate.rar"),_T("ReportTemplate.rar"));

WinExec(_T("rar.exe e -y ReportTemplate.rar ReportTemplate\\"),SW_HIDE);
DeleteFile(_T("ReportTemplate.rar"));


ftpConnection->GetFile(_T("Login.rar"),_T("Login.rar"));
WinExec(_T("rar.exe e -y Login.rar"),SW_HIDE);
DeleteFile(_T("Login.rar"));
}
//catch(CException *e)
catch(CInternetException* e)
{
e->ReportError();
delete iSession;
delete ftpConnection;
iSession=NULL;
ftpConnection=NULL;
return;
}



//MessageBox(_T("download ok!"));
CloseRunApp();
delete iSession;
delete ftpConnection;
iSession=NULL;
ftpConnection=NULL;

}
void CAutoUpdateDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
this->CloseRunApp();
Sleep(2000);
this->GetFtpFile();
this->ResumeRunApp();
//OnOK();
}


void CAutoUpdateDlg::OnTimer(UINT_PTR nIDEvent)
{

// TODO: 在此添加消息处理程序代码和/或调用默认值
int intLower,intUpper;
m_progress.GetRange(intLower,intUpper);
if( nIDEvent==1)
{
if(m_progress.GetPos()==intUpper)
m_progress.SetPos(0);
m_progress.SetPos(m_progress.GetPos()+1);

}

CDialog::OnTimer(nIDEvent);
}

void CAutoUpdateDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CString strServer,strUser,strPwd,strFilename;
int intPort=21;
strServer.Format("192.168.1.220");
strUser.Format("zd");
strPwd.Format("123");
CInternetSession *iSession;
CFtpConnection *ftpConnection;

iSession=new CInternetSession(AfxGetAppName(),1);
ftpConnection=iSession->GetFtpConnection(strServer,strUser,strPwd,intPort);

ftpConnection->GetFile(_T("ReportTemplate.rar"),_T("ReportTemplate.rar"));

MessageBox("download OK!");


}
LRESULT CAutoUpdateDlg::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
Sleep(1000);
SetDlgItemText(IDC_STATIC1,"关闭应用程序");

CloseRunApp();

Sleep(1000);
SetDlgItemText(IDC_STATIC1,"下载并更新网络文件");
GetFtpFile();
Sleep(1000);
::KillTimer(this->m_hWnd,1);
m_progress.SetPos(100);

if(MessageBox("更新完成!","提示",MB_OK|MB_ICONINFORMATION)==IDOK)
{
SetDlgItemText(IDC_STATIC1,"重新启动应用程序");
ResumeRunApp();
SetDlgItemText(IDC_STATIC1,"更新完成");
EndDialog(IDOK);
}
return true;
}

//OnInitDialog中执行
//this->SetTimer(2,1000,(TIMERPROC)TimerProc);
//然后下面这个函数PostMessage WM_MYMESSAGE_EXCUTE
//VOID CALLBACK TimerProc( HWND hwnd,
// UINT uMsg,
// UINT_PTR idEvent,
// DWORD dwTime
//)
//{
// ::KillTimer(hwnd,2);
// ::PostMessage(hwnd,WM_MYMESSAGE_EXCUTE,0,0);
//}
//然后使用OnMyMessage接受后调用OnMyMessage函数
//CloseRunApp();关闭现有程序
//GetFtpFile();下载并更新文件
//ResumeRunApp();重新启动文件
//可是在GetFtpFile()的时候始终下不下来文件,如果单步执行就可以下载,一切正常。
//如果直接使用这样调用
void CAutoUpdateDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CString strServer,strUser,strPwd,strFilename;
int intPort=21;
strServer.Format("192.168.1.220");
strUser.Format("zd");
strPwd.Format("123");
CInternetSession *iSession;
CFtpConnection *ftpConnection;

iSession=new CInternetSession(AfxGetAppName(),1);
ftpConnection=iSession->GetFtpConnection(strServer,strUser,strPwd,intPort);

ftpConnection->GetFile(_T("ReportTemplate.rar"),_T("ReportTemplate.rar"));

MessageBox("download OK!");


}
就可以下载下来,不知道怎么回事。哪位能帮帮忙啊!



...全文
54 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
izardMan 2008-05-23
  • 打赏
  • 举报
回复
关注

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧