Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

Way is the resource getting bigger when uploaded to exe

$
0
0
First of let me start by saying hello everyone as this is my first post on MSDN.

Ok so I have been having a hard time figuring out a problem.
When I split the file buffer and upload the split parts to resources of a file, the file get bigger than the actual file buffer.

What i mean is that if i split in 4 parts I get the correct size in the output file.
But the higer the split count the bigger the file gets. I have writen several desk tests of this and the buffers in `splitBlocks` is working as I can set it back together and output a copy of the file.

Do anyone have a clue on way file is getting bigger and corrupt when splitting it up like this. As far as I have managed to figure out is 4 is the last "safe" split count after that it gets unstable and corrupt.
    #include "stdafx.h"    #include <stdio.h>    #include <Windows.h>    #include <iostream>    #include <string>    #include <vector>        // Just a function i use to check my buffer is working.    void outputFile(char* cBuffer, int fileSize)    {        DWORD BytesRead = 0;        HANDLE hTest = NULL;        hTest = CreateFile("test.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, hTest);        WriteFile(hTest, cBuffer, fileSize, &BytesRead, FALSE);        CloseHandle(hTest);    }        int main(int argc, char* argv[])    {        HANDLE // file HANDLE            // User input            // Builder output HANDLE        // Update handle for output            hFileIn = NULL,            hFileOut = NULL,                hUpdate = NULL;        DWORD fileSize = 0;            // FileSize of user input            char x; // Trashe veriable for std::cin >>            cout << "Enter File Name: " << endl;        string fileName;        cin >> fileName;        cout << "\n\n[OK]\tFIle name set to: " << fileName << endl;            // Open file: string fileName;        hFileIn = CreateFile(fileName.c_str(), GENERIC_READ, 0, NULL, OPEN_ALWAYS, 0, hFileIn);        if (hFileIn == NULL )        {            cout << "Create File failed!.." << endl;            cout << "error code: " << GetLastError() << endl;            CloseHandle(hFileIn);            cin >> x;            return 0;        } else {            cout << "[OK]\tOpening file " << fileName << endl;        }            fileSize = GetFileSize(hFileIn, 0); // file size of fileName        DWORD BytesRead = 0;            // Bytes read in ReadFile()            vector<char>sBuffer;        // File buffer for user input file        // Read fileName and fill cBuffer whit contetnts.        sBuffer.resize(fileSize);        BOOL bRes;        do {            bRes = ReadFile(hFileIn, &sBuffer[0], sBuffer.size(), &BytesRead, NULL);            cout << "dwBytesRead = " << BytesRead << endl;                cout << "dwBytesRead = " << BytesRead << endl;            if(BytesRead < sBuffer.size())                cout << "!buffer not fully filled!" << endl;            } while ( bRes && BytesRead > 0 );            CloseHandle(hFileIn);        cout << "[OK]\tClosing " << fileName << " done loading to memory." << endl;            cout << "File Size: " << fileSize << endl;        cout << "V Size: " << sBuffer.size() << endl;            int blockCount;        cout << "Enter Splitt Count: ";        cin >> blockCount;            cout << "Splitt Count SET: " << blockCount << endl;            vector<vector<char> >splittBlocks; // Container for the splited file        splittBlocks.resize(blockCount);            unsigned estBlockSize = sBuffer.size() / blockCount;        cout << "Estemated Block Size: " << estBlockSize << endl;            unsigned totalSplitSize = 0;        for (int i = 0; i < blockCount; i++)        {            totalSplitSize += estBlockSize; // Estemate total size        }             cout << "Check Size: " << totalSplitSize << endl; // Displaying in console for convinience            int nDifrence = 0;        if (totalSplitSize < sBuffer.size())        {            nDifrence = sBuffer.size() - totalSplitSize; // Make shure we got the correct size            totalSplitSize += nDifrence;            } else if (totalSplitSize > sBuffer.size() )        {            nDifrence = totalSplitSize - sBuffer.size(); // Make shure we got the correct size            totalSplitSize -= nDifrence;            } else {            cout << "Moving on.... " << endl;        }            cout << "Check Size: " << totalSplitSize << endl; // Displaying in console for convinience            for (int i = 0; i < blockCount; i++)        {            if(i == (blockCount - 1))            {                splittBlocks[i].resize(estBlockSize + nDifrence);    // Allocates the space missing so totalSplitSize == fileSize            } else {                splittBlocks[i].resize( estBlockSize);                // Allocate space for the splited file with size estBlockSize.            }        }            int check2nd = 0;            for (int i = 0; i < blockCount; i++)        {            check2nd += splittBlocks[i].size();        } // Giving the user output telling that the file size is of the splitted buffer in total.        cout << "FInal Block Size: " << check2nd << endl;                int buffX = 0;        // THe blockCount           // Number of blocks in vector        for (unsigned blockX = 0; blockX < splittBlocks.size(); blockX++)        {                                    // Size of splittBlocks[blockX].size()            for ( unsigned tmpX = 0; tmpX < splittBlocks[blockX].size(); tmpX++)            {                splittBlocks[blockX][tmpX] = sBuffer[buffX]; // Copy each part from sBuffer to splitBlocks[blockCount][blockSize]                buffX++;            }        }                /* THIS I JUST USED TO TEST THE splitBlocks TO MAKE SURE IT'S NOT CORRUPT. */        char* cTest;        cTest = new char[check2nd];        buffX = 0;        for (unsigned blockX = 0; blockX < splittBlocks.size(); blockX++)        {            for ( unsigned tmpX = 0; tmpX < splittBlocks[blockX].size() ; tmpX++)            {                cTest[buffX] = splittBlocks[blockX][tmpX];                buffX++;            }        }        outputFile(cTest, buffX);            // Creates a copy of the input file And it works        /* :END: THIS I JUST USED TO TEST THE splitBlocks TO MAKE SURE IT'S NOT CORRUPT. :END: */            hUpdate = BeginUpdateResource("thegame.exe", FALSE); // Gets the handle to the thegame.exe file so we can update resources        int keepCount = 0;        for (unsigned blockX = 0; blockX < splittBlocks.size() && keepCount != totalSplitSize; blockX++)        {            keepCount +=  splittBlocks[blockX].size(); // Just to keep count.            cout << "Uploading Part of Size: " << splittBlocks[blockX].size() << endl;            cout << "Total Uploaded: " << keepCount << endl;              // 0 is reserved for system stuff so we skip it.            UpdateResource(hUpdate, RT_STRING, MAKEINTRESOURCE((blockX + 1)), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),                &splittBlocks[blockX][0], splittBlocks[blockX].size());            if (keepCount > totalSplitSize)                cout << "Uploaded to much " << endl;            Sleep(1); // This was just an attempt to see if the speed of the loop had anything to do with it.        }        EndUpdateResource(hUpdate, FALSE);            cin >> x;        return 0;    }


I know this code is not perfect but i tried to comment to the best of my abilities.
I do not understand way I upload the strings in a txt file. I need to place difrent parts of the file with different resource ID.

When I do this with split count of 4 its is ok, higer the number the harder the game.
I have tried with everything from the intended .txt file. To an .exe file.
And I get the same results, the code above is my 3rd attempt this time using vectors.

Does anyone have any ideas on why this problem is happening.
As `cTest` is put together from `splitBlocks[][]` and writen to disk its not the copy method buffer.
I have spent the last week on MSDN \ Google and have come up with nothing.

So if anyone have some insight on this problem it would much appreciated



Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>