visual c++に基づくwindowsコアプログラミングコード分析(8)自動ダウンロード更新プログラム
Windowsプログラムを作成するには、常に自動更新が必要です.この機能はどのように実現されますか.コード実装と注釈の説明を見てみましょう.
#include "stdafx.h"
#include "AutoUpdate.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//
CWinApp theApp;
using namespace std;
#define MYVERSION 5 // 。 : , , !
DWORD WINAPI UpdateThreadProc(LPVOID lParam)
{
/*
* , GetTempPath() , 。 ,
* 。
*/
wchar_t *TempFileName= new wchar_t[255]; // , “AutoUpdate_.exe”
wmemset(TempFileName,0,255);
wsprintf(TempFileName,L"%s_.exe",AfxGetApp()->m_pszAppName); //wsprintf
URLDownloadToFile(NULL,_T("http://127.0.0.1/update.txt")/* , */, _T("Update.ini"),0,NULL);
FILE *pFile=_wfopen(L"update.ini",L"r");
if(!pFile)
return -1; // , 。
char UpdateInfo[10];// “update.ini”
memset(UpdateInfo,0,10);
fgets(UpdateInfo,10,pFile); //
fclose(pFile);
int UpdateVersion=atoi(UpdateInfo); //atoi (const char* _Str) int 。
if(UpdateVersion>MYVERSION) //
{
//MessageBox(NULL,_T(" "),_T(" "),MB_ICONINFORMATION);
URLDownloadToFile(NULL,_T("http://127.0.0.1/AutoUpdate.exe")/* , */,TempFileName,0,NULL);
PROCESS_INFORMATION PI;
STARTUPINFO SI;
memset(&SI, 0, sizeof(SI));
SI.cb = sizeof(SI);
if(CreateProcess(TempFileName,L"/install", NULL, NULL, FALSE,NORMAL_PRIORITY_CLASS, NULL, NULL, &SI, &PI))
//
ExitProcess(0);
}
DeleteFile(_T("update.ini"));
return 0;
}
DWORD WINAPI WorkThreadProc(LPVOID lParam)
{
MessageBox(NULL,L" 。",L" ",MB_ICONINFORMATION);
Sleep(INFINITE);
return 0;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// MFC
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO:
_tprintf(_T(" : MFC
"));
nRetCode = 1;
}
else
{
// TODO: 。
wchar_t StartInfo[255]; //
wmemset(StartInfo,0,255);
wcscat(StartInfo,AfxGetApp()->m_lpCmdLine);
if(!wcsicmp(StartInfo,_T("/install")))
{
//AfxMessageBox(L" ");
wchar_t *AppName = new wchar_t[255];
wchar_t *TempFileName = new wchar_t[255];// , “AutoUpdate_.exe”
wchar_t *FinalFileName = new wchar_t[255]; // , “AutoUpdate.exe”
wmemset(AppName,0,255);
wmemset(TempFileName,0,255);
wmemset(FinalFileName,0,255);
wcscat(AppName,AfxGetApp()->m_pszAppName);
if(AppName[wcslen(AppName)-1]=='_')
{
wsprintf(TempFileName,L"%s.exe",AppName); //wsprintf
wcscat(FinalFileName,AppName);
FinalFileName[wcslen(FinalFileName)-1]='\0';
wcscat(FinalFileName,_T(".exe"));
//MessageBox(NULL,TempFileName,L" ( )",MB_ICONINFORMATION);
//MessageBox(NULL,FinalFileName,L" ( )",MB_ICONINFORMATION);
CopyFile(TempFileName,FinalFileName,FALSE);
PROCESS_INFORMATION PI;
STARTUPINFO SI;
memset(&SI, 0, sizeof(SI));
SI.cb = sizeof(SI);
if(CreateProcess(FinalFileName,L"/finish", NULL, NULL, FALSE,NORMAL_PRIORITY_CLASS, NULL, NULL, &SI, &PI))
ExitProcess(0);
}
else
{
//MessageBox(NULL,L" ",L" ",MB_ICONERROR);
}
}
else if(!wcsicmp(StartInfo,_T("/finish")))
{
wchar_t *TempFileName = new wchar_t[255];
wmemset(TempFileName,0,255);
wsprintf(TempFileName,L"%s_.exe",AfxGetApp()->m_pszAppName); //wsprintf
//MessageBox(NULL,TempFileName,L" ... ",MB_ICONINFORMATION);
DeleteFile(TempFileName);
}
/* : */
wchar_t tmpMyVer[10];
wmemset(tmpMyVer,0,10);
_itow(MYVERSION,tmpMyVer,10/* 10 */);
MessageBox(NULL,tmpMyVer,_T(" :"),MB_ICONINFORMATION);
/*----------------------------*/
HANDLE hUpdateThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)UpdateThreadProc,NULL,0,0);
HANDLE hWorkThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WorkThreadProc,NULL,0,0);
WaitForSingleObject(hWorkThread,INFINITE);
}
return nRetCode;
}