Пример #1
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;
}
Пример #2
0
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;
    }
}