main.cpp
资源名称:update.zip [点击查看]
上传用户:wangying89
上传日期:2007-01-07
资源大小:61k
文件大小:12k
源码类别:
Ftp客户端
开发平台:
Visual C++
- /* sample of file wzjf.ver:
- 134.104.9.165 /caijia/cai_ji/datas/data06/success
- N2.H01
- N4.H01
- NH.H01
- */
- #include <windows.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <ras.h>
- #include "resource.h"
- #include "tcp.h"
- #include "ftp.h"
- char hostname[20];
- #define UPDATE_PORT 7788
- #define P_VERSION_FILE "wzjf.ver"
- char app_name[100], host[20], path[128], version[20], new_version[20];
- char init_file[128];
- HANDLE hThread_app =NULL, hProcess_app =NULL;
- HWND g_hDlg =NULL;
- int g_fconnected =false;
- HRASCONN g_hRasConn =NULL;
- char new_file_name[100][128];
- int TransNewFiles(HWND hDlg);
- int CheckNewFiles(HWND hDlg);
- int sd, new_file_count =0;
- void log_msg(char *msg);
- int show_msg =0;
- int run_app();
- int term_app();
- int day_is_changed();
- int run_dial();
- void run_hangup();
- void SetState(char *msg);
- void WINAPI RasDialFunc(UINT msg, RASCONNSTATE state, DWORD dwError);
- time_t start_t =0;
- int f_auto_hide =0;
- int f_auto_hangup =0;
- BOOL CALLBACK MainDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- int x, y;
- RECT rc;
- char temp[100];
- static int f_first =1;
- switch(msg)
- {
- case WM_INITDIALOG:
- g_hDlg =hDlg;
- GetWindowRect(hDlg, &rc);
- x =GetSystemMetrics(SM_CXSCREEN);
- y =GetSystemMetrics(SM_CYSCREEN);
- x =x/2-(rc.right-rc.left)/2;
- y =y/2-(rc.bottom-rc.top)/2;
- if(GetPrivateProfileString("update", "app", NULL, app_name, sizeof(app_name), init_file) ==0)
- app_name[0] =0; // 不执行计费程序
- if(GetPrivateProfileInt("update", "autohide", 0, init_file) !=0)
- {
- f_auto_hide =1; // 做完后自动隐藏
- CheckDlgButton(hDlg, IDK_AUTOHIDE, BST_CHECKED);
- }
- if(GetPrivateProfileInt("update", "autohangup", 0, init_file) !=0)
- {
- f_auto_hangup =1; // 退出时自动断开
- CheckDlgButton(hDlg, IDK_AUTOHANGUP, BST_CHECKED);
- }
- SetWindowPos(hDlg, HWND_TOPMOST, x, y, 0, 0, SWP_NOSIZE);
- SetTimer(hDlg, 1, 0, NULL);
- return TRUE;
- case WM_KEYDOWN:
- switch(wParam)
- {
- case VK_ESCAPE:
- ShowWindow(hDlg, SW_MINIMIZE);
- break;
- case VK_F3:
- run_hangup();
- break;
- case VK_F1:
- WinExec("notepad update.log", SW_SHOWNORMAL);
- break;
- }
- break;
- case WM_COMMAND:
- switch(LOWORD(wParam))
- {
- case IDK_AUTOHIDE:
- if(HIWORD(wParam) ==BN_CLICKED)
- {
- if(SendMessage((HWND)lParam, BM_GETCHECK, 0, 0L) ==BST_CHECKED)
- f_auto_hide =1;
- else f_auto_hide =0;
- }
- break;
- case IDK_AUTOHANGUP:
- if(HIWORD(wParam) ==BN_CLICKED)
- {
- if(SendMessage((HWND)lParam, BM_GETCHECK, 0, 0L) ==BST_CHECKED)
- f_auto_hangup =1;
- else f_auto_hangup =0;
- }
- break;
- case ID_HIDE:
- ShowWindow(hDlg, SW_HIDE);
- break;
- case IDOK:
- ShowWindow(hDlg, SW_MINIMIZE);
- break;
- case ID_CLOSE:
- case IDCANCEL: // press ALT+F4
- if(MessageBox(hDlg, "您是否确定退出?", "update", MB_YESNO|MB_DEFBUTTON2)
- ==IDNO)
- break;
- term_app();
- if(!f_auto_hangup) run_hangup();
- EndDialog(hDlg, 0);
- break;
- }
- break;
- case WM_TIMER:
- if(f_first)
- {
- f_first =0;
- KillTimer(hDlg, 1);
- if(run_dial() <0) // 自动拨号
- {
- run_hangup();
- log_msg("拨号失败");
- }
- else ShowWindow(hDlg, SW_MINIMIZE);
- SetTimer(hDlg, 1, 10000, NULL); // 10秒钟检查日期
- tcp_init();
- /*if(run_app() <0)
- {
- sprintf(temp, "运行程序 %s 失败", app_name);
- ::MessageBox(hDlg, temp, "update", MB_OK);
- //EndDialog(hDlg, 0);
- break;
- }*/
- //break;
- }
- // 检查程序是否退出
- if(hProcess_app && WaitForSingleObject(hProcess_app, 0) ==WAIT_OBJECT_0)
- {
- log_msg("应用程序已经退出");
- hProcess_app =NULL;
- }
- if(f_auto_hide)
- ShowWindow(hDlg, SW_HIDE);
- if(day_is_changed() ==0)
- break; // 晚上24点过了?
- if(f_auto_hide)
- ShowWindow(hDlg, SW_SHOW);
- if(CheckNewFiles(hDlg) <=0) // 检查是否有新版本
- {
- if(f_auto_hide) ShowWindow(hDlg, SW_HIDE);
- break;
- }
- if(term_app() <0) // 中断计费程序
- {
- sprintf(temp, "term_app %s failed", app_name);
- log_msg(temp);
- if(f_auto_hide) ShowWindow(hDlg, SW_HIDE);
- break;
- }
- TransNewFiles(hDlg); // 传输新版本文件
- if(run_app() <0) // 运行计费程序
- {
- sprintf(temp, "run_app %s failed", app_name);
- log_msg(temp);
- }
- if(f_auto_hide) ShowWindow(hDlg, SW_HIDE);
- break;
- case WM_QUERYENDSESSION: // Windows退出
- if(!f_auto_hangup) run_hangup();
- return TRUE;
- }
- return FALSE;
- }
- int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPre, LPSTR lpCmdLine, int nCmdShow)
- {
- char temp[120];
- /*if(FindWindow(NULL, "正在检查是否更新系统...")) return 0;*/
- GetCurrentDirectory(sizeof(temp), temp);
- wsprintf(init_file, "%s\update.ini", temp);
- DialogBox(hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc);
- tcp_exit();
- return 0;
- }
- // 对SUNOS系统,由于它的ftp server不支持针对文件的SIZE和DATE命令,所以不能正常使用
- int CheckNewFiles(HWND hDlg)
- {
- FILE *fp =NULL;
- char temp[256];
- int ret =0;
- GetPrivateProfileString("update", "version", "1.0", version, sizeof(version), init_file);
- GetPrivateProfileString("update", "host", "134.104.69.168", host, sizeof(host), init_file);
- GetPrivateProfileString("update", "path", "./", path, sizeof(path), init_file);
- SetState("登录主机 ...");
- if((sd =ftp_login(host, "caijia", "caijia")) <0)
- {
- sprintf(temp, "ftp 登录失败: host=%s", host);
- if(show_msg) ::MessageBox(hDlg, temp, "update", MB_OK);
- else log_msg(temp);
- return -1;
- }
- if(ftp_chdir(sd, path) <0)
- {
- ftp_quit(sd);
- sprintf(temp, "ftp 目录失败: path=%s", path);
- if(show_msg) ::MessageBox(hDlg, temp, "update", MB_OK);
- else log_msg(temp);
- return -1;
- }
- SetState("读取升级文件 ...");
- if(ftp_get_file(sd, P_VERSION_FILE, P_VERSION_FILE, 0, 30) <=0)
- {
- ftp_quit(sd);
- sprintf(temp, "ftp 取升级信息文件失败: host =%s", host);
- if(show_msg) ::MessageBox(hDlg, temp, "update", MB_OK);
- else log_msg(temp);
- return -1;
- }
- if((fp =fopen(P_VERSION_FILE, "r")) ==NULL)
- {
- sprintf(temp, "fopen version file for read failed");
- if(show_msg) ::MessageBox(hDlg, temp, "update", MB_OK);
- else log_msg(temp);
- ftp_quit(sd);
- return -8;
- }
- new_file_count =0;
- if(fgets(temp, sizeof(temp), fp) ==NULL)
- {
- sprintf(temp, "read new.ver failed");
- if(show_msg) ::MessageBox(hDlg, temp, "ftp_ctl", MB_OK);
- else log_msg(temp);
- fclose(fp);
- ftp_quit(sd);
- return -9;
- }
- app_name[0] =0;
- if(sscanf(temp, "%s %s %s %s", new_version, host, path, app_name) <3)
- {
- fclose(fp);
- ftp_quit(sd);
- sprintf(temp, "读升级信息文件失败: %s, format error", P_VERSION_FILE);
- if(show_msg) ::MessageBox(hDlg, temp, "update", MB_OK);
- else log_msg(temp);
- return -1;
- }
- if(strcmp(new_version, version) >0)
- {
- while(!feof(fp) && fgets(new_file_name[new_file_count], 128, fp))
- {
- if(new_file_name[new_file_count][0] =='r' || new_file_name[new_file_count][0] =='n') continue;
- strtok(new_file_name[new_file_count], "r");
- strtok(new_file_name[new_file_count], "n");
- new_file_count++;
- }
- }
- fclose(fp);
- if(new_file_count ==0)
- ftp_quit(sd);
- return new_file_count;
- }
- int TransNewFiles(HWND hDlg)
- {
- int i, ret =0;
- char temp[200];
- ShowWindow(hDlg, SW_RESTORE);
- SetWindowPos(hDlg, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
- for(i =0; i<new_file_count; i++)
- {
- sprintf(temp, "更新系统:正在取文件 %s...", new_file_name[i]);
- SetDlgItemText(hDlg, IDC_MSG, temp);
- if(ftp_get_file(sd, new_file_name[i], new_file_name[i], 0, 300) <=0)
- {
- sprintf(temp, "ftp 取文件失败: file=%s", new_file_name[i]);
- if(show_msg) ::MessageBox(hDlg, temp, "update", MB_OK);
- else log_msg(temp);
- ret =-1;
- continue;
- }
- }
- ftp_quit(sd);
- if(ret ==0)
- {
- WritePrivateProfileString("update", "version", new_version, init_file);
- WritePrivateProfileString("update", "host", host, init_file);
- WritePrivateProfileString("update", "path", path, init_file);
- WritePrivateProfileString("update", "app", app_name, init_file);
- sprintf(temp, "更新系统成功. 新版本=%s", new_version);
- }
- else
- sprintf(temp, "更新系统失败. 系统需要更新为%s", new_version);
- log_msg(temp);
- return ret;
- }
- void log_msg(char *msg)
- {
- FILE *fp;
- time_t t;
- struct tm *ptm;
- char temp[300];
- time(&t);
- ptm =localtime(&t);
- SetDlgItemText(g_hDlg, IDC_MSG, msg);
- if((fp =fopen("update.log", "a")) !=NULL)
- {
- fprintf(fp, "%02d.%02d %02d.%02d errno:%d, %sn",
- ptm->tm_mon+1, ptm->tm_mday, ptm->tm_hour,
- ptm->tm_min, GetLastError(), msg);
- fclose(fp);
- if(strlen(msg) >200) *(msg+200) =0;
- wsprintf(temp, "%02d.%02d %02d.%02d errno:%d, %srn",
- ptm->tm_mon+1, ptm->tm_mday, ptm->tm_hour,
- ptm->tm_min, GetLastError(), msg);
- SendDlgItemMessage(g_hDlg, IDE_MSG, EM_REPLACESEL, 0, (LPARAM)(LPSTR)temp);
- }
- }
- int day_is_changed()
- {
- time_t t;
- struct tm *ptm;
- static int cur_day =-1;
- char temp[30];
- int tt;
- time(&t);
- if(start_t >0)
- {
- tt =t-start_t;
- wsprintf(temp, "%d:%02d:%02d:", tt/3600, tt%3600/60, tt%60);
- SetDlgItemText(g_hDlg, IDC_TIME, temp);
- }
- ptm =localtime(&t);
- if(cur_day ==ptm->tm_mday) return 0; /* first time is changed */
- cur_day =ptm->tm_mday;
- return 1;
- }
- int term_app()
- {
- if(f_auto_hide) WritePrivateProfileString("update", "autohide", "1", init_file);
- else WritePrivateProfileString("update", "autohide", "0", init_file);
- if(f_auto_hangup) WritePrivateProfileString("update", "autohangup", "1", init_file);
- else WritePrivateProfileString("update", "autohangup", "0", init_file);
- if(hProcess_app ==NULL) return 0;
- TerminateProcess(hProcess_app, 1);
- CloseHandle(hThread_app);
- CloseHandle(hProcess_app);
- return 0;
- }
- int run_app()
- {
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- char temp[100];
- if(app_name[0] ==0) return 0;
- memset(&si, 0, sizeof(si));
- if(CreateProcess(app_name, app_name, NULL, NULL, false, 0, NULL, NULL, &si, &pi) ==0)
- {
- sprintf(temp, "CreateProcess failed! errno:%d", GetLastError());
- log_msg(temp);
- return -1;
- }
- hThread_app =pi.hThread;
- hProcess_app =pi.hProcess;
- return 0;
- }
- int run_dial()
- {
- // rundll rnaui.dll,RnaDial ???
- char dial_name[100], dial_user[50], dial_pass[20], temp[200], err_buf[100];
- RASDIALPARAMS rdParams;
- DWORD dwRet;
- BOOL f_get_password;
- if(GetPrivateProfileString("update", "dialname", NULL, dial_name,
- sizeof(dial_name), init_file) ==0) return 0;
- dial_user[0] =dial_pass[0] =0;
- GetPrivateProfileString("update", "dialuser", NULL, dial_user,
- sizeof(dial_user), init_file);
- GetPrivateProfileString("update", "dialpass", NULL, dial_pass,
- sizeof(dial_pass), init_file);
- memset(&rdParams, 0, sizeof(rdParams));
- rdParams.dwSize=sizeof(RASDIALPARAMS);
- strcpy(rdParams.szEntryName,dial_name);
- //strcpy(rdParams.szPhoneNumber,"169");
- //strcpy(rdParams.szCallbackNumber,"*");
- if(dial_user[0] ==0 || dial_pass[0] ==0)
- {
- if(RasGetEntryDialParams(NULL, &rdParams, &f_get_password) !=0)
- {
- wsprintf(temp, "读取拨号网络%s的用户名和密码失败", dial_name);
- log_msg(temp);
- return -1;
- }
- if(f_get_password ==false)
- {
- wsprintf(temp, "读取拨号网络%s的密码失败,请先使用保存密码成功拨号一次", dial_name);
- log_msg(temp);
- return -1;
- }
- }
- else
- {
- strcpy(rdParams.szUserName, dial_user);
- strcpy(rdParams.szPassword, dial_pass);
- }
- //strcpy(rdParams.szDomain,"*");
- g_hRasConn=NULL;
- //if((dwRet=RasDial(NULL,NULL,&rdParams,0L, (RASDIALFUNC)RasDialFunc,&g_hRasConn)) !=0)
- wsprintf(temp, "正在拨号... name=%s", dial_name);
- log_msg(temp);
- if((dwRet=RasDial(NULL,NULL,&rdParams,0L, NULL, &g_hRasConn)) !=0)
- {
- RasGetErrorString(dwRet, err_buf, sizeof(err_buf));
- wsprintf(temp, "拨号失败, name=%s, ret=%d:%s", dial_name, dwRet, err_buf);
- log_msg(temp);
- return -1;
- }
- time(&start_t); // 开始计时
- wsprintf(temp, "拨号成功, name=%s", dial_name);
- log_msg(temp);
- return 0;
- }
- void run_hangup()
- {
- if(g_hRasConn) RasHangUp(g_hRasConn);
- g_hRasConn =NULL;
- }
- void WINAPI RasDialFunc(UINT msg, RASCONNSTATE state, DWORD dwError)
- {
- switch(state)
- {
- case RASCS_Connected:
- g_fconnected =true;
- break;
- case RASCS_Disconnected:
- g_fconnected =false;
- break;
- }
- }
- void SetState(char *msg)
- {
- SetDlgItemText(g_hDlg, IDC_MSG, msg);
- }