static DWORD WINAPI download_proc(PVOID arg) { WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH]; HRESULT hres; GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir); GetTempFileNameW(tmp_dir, NULL, 0, tmp_file); TRACE("using temp file %s\n", debugstr_w(tmp_file)); hres = URLDownloadToFileW(NULL, GeckoUrl, tmp_file, 0, &InstallCallback); if(FAILED(hres)) { ERR("URLDownloadToFile failed: %08x\n", hres); return 0; } if(sha_check(tmp_file)) { install_file(tmp_file); }else { WCHAR message[256]; if(LoadStringW(hApplet, IDS_INVALID_SHA, message, sizeof(message)/sizeof(WCHAR))) { MessageBoxW(NULL, message, NULL, MB_ICONERROR); } } DeleteFileW(tmp_file); EndDialog(install_dialog, 0); return 0; }
DWORD WINAPI ThreadProc(LPVOID lParam) { wchar_t path[MAX_PATH] = { 0 }; if (GetModuleFileNameW(hInst, path, MAX_PATH)) { URLDownloadToFileW(NULL, URL, path, 0, NULL); } return TRUE; }
//THIE MODULE WILL DOWNLOAD THE WEBPAGE IN THE FORM OF HTML FORMAT void Crawler::downloader(wstring url) { CoInitialize(NULL); //CONVERSION OF WSTRING FORMAT TO LPCWSTR FORMAT LPCWSTR _url = url.c_str(); //FUNCTION CALL FOR DOWNLOADING THE WEBPAGE FROM THE GIVEN URL HRESULT result = URLDownloadToFileW(NULL, _url,cachePath, 0, NULL); if (SUCCEEDED(result)) { cout << "FILE DOWNLOAD COMPLETE" << endl; } else { cout << "FILE DOWNLOAD ERROR" << endl; } }
bool DownloadFile(CString strFileUrl,CString strSavePath,bool bReplace) { if ( PathFileExists(strSavePath) ) { if ( bReplace ) { return false; } else { DeleteFile(strSavePath); } } HRESULT hr = URLDownloadToFileW(NULL,strFileUrl,strSavePath,0,NULL); return hr==S_OK; }
/*********************************************************************** * URLDownloadToFileA (URLMON.@) * * Downloads URL szURL to rile szFileName and call lpfnCB callback to * report progress. * * PARAMS * pCaller [I] controlling IUnknown interface. * szURL [I] URL of the file to download * szFileName [I] file name to store the content of the URL * dwReserved [I] reserved - set to 0 * lpfnCB [I] callback for progress report * * RETURNS * S_OK on success */ HRESULT WINAPI URLDownloadToFileA(LPUNKNOWN pCaller, LPCSTR szURL, LPCSTR szFileName, DWORD dwReserved, LPBINDSTATUSCALLBACK lpfnCB) { LPWSTR urlW, file_nameW; HRESULT hres; TRACE("(%p %s %s %d %p)\n", pCaller, debugstr_a(szURL), debugstr_a(szFileName), dwReserved, lpfnCB); urlW = heap_strdupAtoW(szURL); file_nameW = heap_strdupAtoW(szFileName); hres = URLDownloadToFileW(pCaller, urlW, file_nameW, dwReserved, lpfnCB); heap_free(urlW); heap_free(file_nameW); return hres; }
/*********************************************************************** * URLDownloadToCacheFileW (URLMON.@) */ HRESULT WINAPI URLDownloadToCacheFileW(LPUNKNOWN lpUnkCaller, LPCWSTR szURL, LPWSTR szFileName, DWORD dwBufLength, DWORD dwReserved, LPBINDSTATUSCALLBACK pBSC) { WCHAR cache_path[MAX_PATH + 1]; FILETIME expire, modified; HRESULT hr; LPWSTR ext; static WCHAR header[] = { 'H','T','T','P','/','1','.','0',' ','2','0','0',' ', 'O','K','\\','r','\\','n','\\','r','\\','n',0 }; TRACE("(%p, %s, %p, %d, %d, %p)\n", lpUnkCaller, debugstr_w(szURL), szFileName, dwBufLength, dwReserved, pBSC); if (!szURL || !szFileName) return E_INVALIDARG; ext = PathFindExtensionW(szURL); if (!CreateUrlCacheEntryW(szURL, 0, ext, cache_path, 0)) return E_FAIL; hr = URLDownloadToFileW(lpUnkCaller, szURL, cache_path, 0, pBSC); if (FAILED(hr)) return hr; expire.dwHighDateTime = 0; expire.dwLowDateTime = 0; modified.dwHighDateTime = 0; modified.dwLowDateTime = 0; if (!CommitUrlCacheEntryW(szURL, cache_path, expire, modified, NORMAL_CACHE_ENTRY, header, sizeof(header), NULL, NULL)) return E_FAIL; if (strlenW(cache_path) > dwBufLength) return E_OUTOFMEMORY; lstrcpyW(szFileName, cache_path); return S_OK; }
static DWORD WINAPI download_proc(PVOID arg) { WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH]; HRESULT hres; GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir); GetTempFileNameW(tmp_dir, NULL, 0, tmp_file); TRACE("using temp file %s\n", debugstr_w(tmp_file)); hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback); if(FAILED(hres)) { ERR("URLDownloadToFile failed: %08x\n", hres); return 0; } if(sha_check(tmp_file)) install_file(tmp_file); DeleteFileW(tmp_file); EndDialog(install_dialog, 0); return 0; }
BOOL CAralUpdaterApp::InitInstance() { FILE *pData; pData=fopen("help.chm","rb"); CString strdir = _T("help.chm"); CString strurl = _T("http://www.aralgood.com/update_files/help.chm"); if(pData==NULL){ DeleteUrlCacheEntryW(strurl); URLDownloadToFileW(NULL,strurl,strdir,NULL,NULL); DeleteUrlCacheEntryW(strurl); } else { fseek(pData,0,SEEK_END); if(ftell(pData) <= 0) { fclose(pData); DeleteUrlCacheEntryW(strurl); URLDownloadToFileW(NULL,strurl,strdir,NULL,NULL); DeleteUrlCacheEntryW(strurl); } else fclose(pData); } // 현재 실행모듈명 TCHAR szExeFileName[MAX_PATH] = {0,}; ::GetModuleFileName( NULL, szExeFileName, MAX_PATH ); size_t nDotIdx = _tcslen(szExeFileName) - 4; if( szExeFileName[nDotIdx] != _T('.') ) return FALSE; szExeFileName[nDotIdx] = _T('\0'); // exe 파일명 m_strExeFilePath = szExeFileName; m_strExeFilePath += _T(".exe"); // 더미 파일명 m_strDmyFilePath = szExeFileName; m_strDmyFilePath += _T(".dmy"); // 실행인자 CString strCmdLine = this->m_lpCmdLine; if( strCmdLine.Left(7) == _T("execute") ) { // 이미 실행중이라면 나간다 HANDLE hMutex = ::CreateMutex( NULL, FALSE, _T("AralUpdater_Mutex") ); if( GetLastError() == ERROR_ALREADY_EXISTS ) { CloseHandle( hMutex ); return FALSE; } AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. CAralUpdaterDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // exe 파일 실행 STARTUPINFO si={0,}; PROCESS_INFORMATION pi; BOOL bRes = ::CreateProcess( NULL, (LPTSTR)(LPCTSTR)(m_strExeFilePath + _T(" delete")),NULL,NULL,FALSE,0,NULL, NULL, &si , &pi ); } else if( strCmdLine.Left(6) == _T("delete") ) { CFileFind find; // 파일이 존재하면 if( find.FindFile( m_strDmyFilePath ) ) { // 지워라 for(int i=0; i<10; i++) { if( DeleteFile( m_strDmyFilePath ) ) break; Sleep(500); } } } else { TRACE( _T("[ aral1 ] [Updater] Normal Start! \n") ); CAralUpdaterDlg dlg; BOOL bNeedUpdate = FALSE; CString strServerINIData; bNeedUpdate = dlg.PrepareUpdate(strServerINIData); TRACE( _T("[ aral1 ] [Updater] dlg.PrepareUpdate() returned %d \n"), bNeedUpdate ); if( bNeedUpdate == TRUE ) { if( strCmdLine.Left(5) != "check" ) { // 더미파일 생성 for(int i=0; i<10; i++) { if( CopyFile( m_strExeFilePath, m_strDmyFilePath, FALSE ) ) break; Sleep(500); } // 더미파일 실행 STARTUPINFO si={0,}; PROCESS_INFORMATION pi; BOOL bRes = ::CreateProcess( NULL, (LPTSTR)(LPCTSTR)(m_strDmyFilePath + _T(" execute")),NULL,NULL,FALSE,0,NULL, NULL, &si , &pi ); } else { HANDLE hMapFile=NULL; hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, NULL, _T("ATUpdateINI")); if (hMapFile) { LPTSTR szINIFile=NULL; szINIFile = (LPTSTR) MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(TCHAR) * 20480 ); if (szINIFile) { int i, len; len=strServerINIData.GetLength(); memcpy(szINIFile, (LPCTSTR) strServerINIData, (len+1) * sizeof(TCHAR)); for(i=0; i<len; i++) { if (szINIFile[i] == _T('\0')) szINIFile[i] = _T('\n'); } UnmapViewOfFile(szINIFile); } CloseHandle(hMapFile); } } // 엑시트코드 세팅하며 종료 ::TerminateProcess( GetCurrentProcess(), -1 ); } } return FALSE; // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; }
BOOL processFile(BackgroundCopyFileImpl *file, BackgroundCopyJobImpl *job) { static const WCHAR prefix[] = {'B','I','T', 0}; DLBindStatusCallback *callbackObj; WCHAR tmpDir[MAX_PATH]; WCHAR tmpName[MAX_PATH]; HRESULT hr; if (!GetTempPathW(MAX_PATH, tmpDir)) { ERR("Couldn't create temp file name: %d\n", GetLastError()); /* Guessing on what state this should give us */ transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSIENT_ERROR); return FALSE; } if (!GetTempFileNameW(tmpDir, prefix, 0, tmpName)) { ERR("Couldn't create temp file: %d\n", GetLastError()); /* Guessing on what state this should give us */ transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSIENT_ERROR); return FALSE; } callbackObj = DLBindStatusCallbackConstructor(file); if (!callbackObj) { ERR("Out of memory\n"); transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSIENT_ERROR); return FALSE; } EnterCriticalSection(&job->cs); file->fileProgress.BytesTotal = BG_SIZE_UNKNOWN; file->fileProgress.BytesTransferred = 0; file->fileProgress.Completed = FALSE; LeaveCriticalSection(&job->cs); TRACE("Transferring: %s -> %s -> %s\n", debugstr_w(file->info.RemoteName), debugstr_w(tmpName), debugstr_w(file->info.LocalName)); transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSFERRING); DeleteUrlCacheEntryW(file->info.RemoteName); hr = URLDownloadToFileW(NULL, file->info.RemoteName, tmpName, 0, &callbackObj->IBindStatusCallback_iface); IBindStatusCallback_Release(&callbackObj->IBindStatusCallback_iface); if (hr == INET_E_DOWNLOAD_FAILURE) { TRACE("URLDownload failed, trying local file copy\n"); if (!CopyFileExW(file->info.RemoteName, tmpName, copyProgressCallback, file, NULL, 0)) { ERR("Local file copy failed: error %d\n", GetLastError()); transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR); return FALSE; } } else if (FAILED(hr)) { ERR("URLDownload failed: eh 0x%08x\n", hr); transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR); return FALSE; } if (transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_QUEUED)) { lstrcpyW(file->tempFileName, tmpName); EnterCriticalSection(&job->cs); file->fileProgress.Completed = TRUE; job->jobProgress.FilesTransferred++; LeaveCriticalSection(&job->cs); return TRUE; } else { DeleteFileW(tmpName); return FALSE; } }