问一个关于FTP下载的问题,棘手!
代码如下:
#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!");
}
就可以下载下来,不知道怎么回事。哪位能帮帮忙啊!