Search code examples
c++winapiembedded-resourcecopying

Faster method for exporting embedded data


For some reasons, i'm using the method described here: Link

It starts off from the first byte of the embedded file and goes through 4.234.925 bytes one by one! It takes approximately 40 seconds to finish.

Is there any other methods for copying an embedded file to the hard-disk? (I maybe wrong here but i think the embedded file is read from the memory)

Thanks.


Solution

  • Once you know the location and size of the embedded exe , then you can do it in one write.

    LPBYTE pbExtract; // the pointer to the data to extract
    UINT   cbExtract; // the size of the data to extract.
    
    HANDLE hf;
    hf = CreateFile("filename.exe",          // file name
                    GENERIC_WRITE,           // open for writing 
                    0,                       // no share
                    NULL,                    // no security 
                    CREATE_ALWAYS,           // overwrite existing
                    FILE_ATTRIBUTE_NORMAL,   // normal file 
                    NULL);                   // no template 
    
    if (INVALID_HANDLE_VALUE != hf)
    {
       DWORD cbWrote;
       WriteFile(hf, pbExtract, cbExtract, &cbWrote, NULL);
       CloseHandle(hf);
    }