⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 win32updatemanager.cpp

📁 FreeAMP(MP3播放)程序源代码-用来研究MP3解码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*____________________________________________________________________________
	
	FreeAmp - The Free MP3 Player

	Portions Copyright (C) 1998-1999 EMusic.com

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
	
	$Id: win32updatemanager.cpp,v 1.10 2000/05/22 13:50:19 elrod Exp $
____________________________________________________________________________*/

// The debugger can't handle symbols more than 255 characters long.
// STL often creates symbols longer than that.
// When symbols are longer than 255 characters, the warning is disabled.
#ifdef WIN32
#pragma warning(disable:4786)
#define STRICT
#endif


#include <assert.h>
#include <windows.h>
#include <io.h>
#include <direct.h>
#include <time.h>
#include <errno.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <iomanip>

using namespace std;

#include "config.h"
#include "facontext.h"

#include "errors.h"
#include "win32updatemanager.h"
#include "utility.h"
#include "event.h"
#include "eventdata.h"
#include "zlib.h"


Win32UpdateManager::Win32UpdateManager(FAContext* context):
UpdateManager(context)
{
    m_context = context;
}

Win32UpdateManager::~Win32UpdateManager()
{
  
}

Error Win32UpdateManager::DetermineLocalVersions()
{
    Error result = kError_AlreadyUpdating;

    if(m_mutex.Acquire(0))
    {
        char appPath[MAX_PATH];
        uint32 length = sizeof(appPath);
        m_context->prefs->GetPrefString(kInstallDirPref, appPath, &length);

        // paths i need to ignore
        length = sizeof(m_musicPath);
        m_context->prefs->GetPrefString(kSaveMusicDirPref, m_musicPath, &length);
   
        strcpy(m_updatePath, appPath);
        strcat(m_updatePath, "\\update");

        // analyze all the files in our install dir
        result = GetFileVersions(appPath);

        // what about the system files this version depends on?
        if(IsntError(result))
            result = GetSystemFileVersions();
        
        m_mutex.Release();
    }

    return result;
}

Error Win32UpdateManager::UpdateComponents(UMCallBackFunction function,
                                           void* cookie)
{
    Error result;
    
    result = UpdateManager::UpdateComponents(function, cookie);

    if(IsntError(result))
    {
        int32 response;

        response = MessageBox(NULL, 
                              The_BRANDING" needs to close down and restart in order to replace components\r\n"
                              "which are being used. If you do not wish to quit the application you\r\n"
                              "can choose \"Cancel\" and update again at a later time.",
                              "Restart "the_BRANDING"?", 
                              MB_OKCANCEL|MB_ICONQUESTION|MB_SETFOREGROUND);

        if(response == IDOK)
        {
            char appPath[MAX_PATH];
            char updatePath[MAX_PATH];
            uint32 length = sizeof(appPath);
            m_context->prefs->GetPrefString(kInstallDirPref, appPath, &length);
            
            strcpy(updatePath, appPath);

            strcat(appPath, "\\update.exe");
            strcat(updatePath, "\\update\\update.exe");

            // if the update program has been updated we need to 
            // move it before execing it...
            HANDLE findFileHandle = NULL;
            WIN32_FIND_DATA findData;

            findFileHandle = FindFirstFile(updatePath, &findData);

            if(findFileHandle != INVALID_HANDLE_VALUE)
            {
                int32 failureCount;
                BOOL success;

                failureCount = 0;
                success = TRUE;

                do
                {
                    // remove old file
                    success = DeleteFile(appPath);

                    if(!success)
                        Sleep(1000);

                }while(!success && failureCount++ < 3);

                
                if(success)
                {
                    failureCount = 0;

                    do
                    {
                       // actually move the file
                        success = MoveFile(updatePath, appPath);

                        if(!success)
                            Sleep(1000);

                    }while(!success && failureCount++ < 3);
                }
                
                if(!success)
                {
                    LPVOID lpMessageBuffer;

		            FormatMessage(
		              FORMAT_MESSAGE_ALLOCATE_BUFFER |
		              FORMAT_MESSAGE_FROM_SYSTEM,
		              NULL,
		              GetLastError(),
		              MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		              (LPTSTR) &lpMessageBuffer,
		              0,
		              NULL );

		            // now display this string
 		            MessageBox(NULL, (char*)lpMessageBuffer, appPath, MB_OK);

		            // Free the buffer allocated by the system
		            LocalFree( lpMessageBuffer );
                }

                FindClose(findFileHandle);
            }

            m_context->target->AcceptEvent(new Event(CMD_QuitPlayer));

            WinExec(appPath, SW_NORMAL);
        }
    }
    
    return result;
}

Error Win32UpdateManager::GetFileVersions(const char* path)
{
    Error result = kError_NoErr;
    
    HANDLE findFileHandle = NULL;
    WIN32_FIND_DATA findData;  
    char filePath[MAX_PATH];
    char* fp;

    strcpy(filePath, path);
    strcat(filePath, "\\*.*");
    fp = strrchr(filePath, '\\') + 1;
    
    findFileHandle = FindFirstFile(filePath, &findData);

    //cout << "searching in "<< path << ":" << endl;

    if(findFileHandle != INVALID_HANDLE_VALUE)
    {
        do
        {
            strcpy(fp, findData.cFileName);

            // skip these two special entries && music dir
            if( strcmp(findData.cFileName, ".") && 
                strcmp(findData.cFileName, "..") &&
                strcmp(filePath, m_updatePath) &&
                strcmp(filePath, m_musicPath))
            {
                if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
                {
                    // call ourselves on that directory
                    GetFileVersions(filePath);      
                }
                else 
                {
                    DWORD versionSize;
                    DWORD dummyHandle;
                    void* data;

                    versionSize = GetFileVersionInfoSize(filePath, &dummyHandle);
                    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -