关闭

实现程序合并和分解

标签: 合并分解
227人阅读 评论(0) 收藏 举报
分类:

1.前言

公司需要实现我们的程序和客户的程序合并成一个,且要先运行我们的,客户的才能正确运行。所以需要做一个简单的类似于安装包一样的东东。

2.解决

有很多方法,这里提供两种:

2.1 可以将客户的程序作为我们程序的一个资源来实现合并和释放

合并代码片段

void merge()
{
    HANDLE hUpdateRes; 
    hUpdateRes = BeginUpdateResource(strDesPath.c_str(), FALSE);
    if (hUpdateRes != NULL)
    {
        HANDLE hOpenFile = (HANDLE)CreateFile(wstrloginpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
        if (hOpenFile != INVALID_HANDLE_VALUE)
        {
            DWORD fileSize = GetFileSize(hOpenFile, NULL);
            char *pBuffer = (char *) malloc(fileSize + 1);     //这里可能会失败
            DWORD RSize = 0;
            ReadFile(hOpenFile, pBuffer, fileSize, &RSize, NULL);

            BOOL result = UpdateResource(hUpdateRes,   
                TEXT("FILE"),                        
                TEXT("ALACLIENT"),       
                MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
                (char *)pBuffer,                        
                fileSize);  //fileSize + 5
            if (!result)
                writelog("failed UpdateResource exe in generatefunc");

            CloseHandle(hOpenFile);
            free(pBuffer);
        }
        EndUpdateResource(hUpdateRes, FALSE);
    }
}

分解代码片段

void splite()
{
    HRSRC hrSrc;
    HGLOBAL hGlobal;
    LPVOID lpExe;
    DWORD dwSize;
    HANDLE hFile;

    hrSrc = FindResource(NULL, TEXT("ALACLIENT"), TEXT("FILE"));
    hGlobal = LoadResource(NULL, hrSrc);
    lpExe = LockResource(hGlobal);
    dwSize = SizeofResource(NULL, hrSrc);
    hFile = CreateFile(
        strLoginPath.c_str(),
        GENERIC_ALL,
        0,
        NULL,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
    if(hFile != INVALID_HANDLE_VALUE)
    {
        DWORD dwBytesWritten = 0;
        if(WriteFile(hFile,lpExe, dwSize, &dwBytesWritten,NULL)==FALSE)
        {
            //这里进行下解密
        }
        CloseHandle(hFile);
    }
    UnlockResource(hGlobal);
    FreeResource(hGlobal);
}

2.2 可以将客户的程序合并到我们的程序后面

合并部分代码片段

void merge()
{
    DWORD dwSrcLen = 0;
    HANDLE hAppend = CreateFile(strDesPath.c_str(),  // open Two.txt
        FILE_APPEND_DATA,         // open for writing
        FILE_SHARE_READ,          // allow multiple readers
        NULL,                     // no security
        OPEN_ALWAYS,              // open or create
        FILE_ATTRIBUTE_NORMAL,    // normal file
        NULL);                    // no attr. template

    if (hAppend == INVALID_HANDLE_VALUE)
    {
        //printf("Could not open Two.txt.");
        writelog("failed open despath.");
        MessageBox(NULL, TEXT("生成失败,请重新封装"), TEXT("提示"), 0);
        CloseHandle(hAppend);
        hAppend = INVALID_HANDLE_VALUE;
        return;
    }
    else
        dwSrcLen = GetFileSize(hAppend, NULL);


    HANDLE hFileZip = CreateFile(wstrZipPath.c_str(),  // open One.txt
        GENERIC_READ,             // open for reading
        0,                        // do not share
        NULL,                     // no security
        OPEN_EXISTING,            // existing file only
        FILE_ATTRIBUTE_NORMAL,    // normal file
        NULL);                    // no attr. template

    if (hFileZip != INVALID_HANDLE_VALUE)
    {
        DWORD dwBytesRead = sizeof(DWORD), dwBytesWritten = 0;
        DWORD dwFileLen = GetFileSize(hFileZip, NULL);
        DWORD dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);
        BYTE   buff[4096] = {0};

        LockFile(hAppend, dwPos, 0, dwBytesRead, 0);
        WriteFile(hAppend, &dwFileLen, sizeof(DWORD), &dwBytesWritten, NULL);
        UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0);

        while (ReadFile(hFileZip, buff, sizeof(buff), &dwBytesRead, NULL)
            && dwBytesRead > 0)
        {
            dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);
            LockFile(hAppend, dwPos, 0, dwBytesRead, 0);
            WriteFile(hAppend, buff, dwBytesRead, &dwBytesWritten, NULL);
            UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0);
        }
        CloseHandle(hFileZip);
        hFileZip = INVALID_HANDLE_VALUE;


        dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);
        dwBytesRead = sizeof(DWORD);
        LockFile(hAppend, dwPos, 0, dwBytesRead, 0);
        WriteFile(hAppend, &dwSrcLen, sizeof(DWORD), &dwBytesWritten, NULL);
        UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0);
    }
    else
    {
        MessageBox(NULL, TEXT("生成失败,请重新进行生成."), TEXT("提示"), 0);
        CloseHandle(hAppend);
        hAppend = INVALID_HANDLE_VALUE;
        return;
    }
    CloseHandle(hAppend);
    hAppend = INVALID_HANDLE_VALUE;
}

分解部分代码片段

void splite()
{
    hFile = CreateFile(ptExePath, // open One.txt
        GENERIC_READ,             // open for reading
        0,                        // do not share
        NULL,                     // no security
        OPEN_EXISTING,            // existing file only
        FILE_ATTRIBUTE_NORMAL,    // normal file
        NULL);                    // no attr. template
    if (hFile == INVALID_HANDLE_VALUE)
        break;
    SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
    dwFileLen = GetFileSize(hFile, NULL);

    //LONG lPos = -sizeof(DWORD);
    dwPos = SetFilePointer(hFile, -sizeof(DWORD), NULL, FILE_END);
    ReadFile(hFile, &snAlaPackLen, sizeof(DWORD), &dwBytesRead, NULL);

    //这里进行对数据提取cloud文件.
    if (snAlaPackLen < 100 || snAlaPackLen > dwFileLen)
        break;

    SetFilePointer(hFile, snAlaPackLen, NULL, FILE_BEGIN);
    if (snAlaPackLen + sizeof(DWORD) > dwFileLen)
        break;

    DWORD dwCloud = 0;
    ReadFile(hFile, &dwCloud, sizeof(DWORD), &dwBytesRead, NULL);
    if (snAlaPackLen + sizeof(DWORD) + dwCloud > dwFileLen)
        break;

    DWORD dwCount = dwCloud / sizeof(buff);
    DWORD dwValue = dwCloud % sizeof(buff);
    hFileZip = CreateFile(wstrTempZipPath.c_str(), // open Two.txt
        FILE_APPEND_DATA,         // open for writing
        FILE_SHARE_READ,          // allow multiple readers
        NULL,                     // no security
        OPEN_ALWAYS,              // open or create
        FILE_ATTRIBUTE_NORMAL,    // normal file
        NULL);                    // no attr. template
    if (hFileZip == INVALID_HANDLE_VALUE)
        break;

    for (DWORD dwIndex = 0; dwIndex < dwCount; ++dwIndex)
    {
        if (ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL) && dwBytesRead > 0)
        {
            dwPos = SetFilePointer(hFileZip, 0, NULL, FILE_END);
            LockFile(hFileZip, dwPos, 0, dwBytesRead, 0);
            WriteFile(hFileZip, buff, dwBytesRead, &dwBytesWritten, NULL);
            UnlockFile(hFileZip, dwPos, 0, dwBytesRead, 0);
        }
    }
    if (dwValue > 0)
    {
        if (ReadFile(hFile, buff, dwValue, &dwBytesRead, NULL) && dwBytesRead > 0)
        {
            dwPos = SetFilePointer(hFileZip, 0, NULL, FILE_END);
            LockFile(hFileZip, dwPos, 0, dwBytesRead, 0);
            WriteFile(hFileZip, buff, dwBytesRead, &dwBytesWritten, NULL);
            UnlockFile(hFileZip, dwPos, 0, dwBytesRead, 0);
        }
    }
    CloseHandle(hFileZip);
    hFileZip = INVALID_HANDLE_VALUE;
    CloseHandle(hFile);
    hFile = INVALID_HANDLE_VALUE;
}

3.备注

这里解释下第二种方法,因为无法将程序大小添加到文件头部,故将其添加到程序的尾部

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:57033次
    • 积分:1198
    • 等级:
    • 排名:千里之外
    • 原创:65篇
    • 转载:6篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论