How to bundle multiple files into one Executable File

Source: Internet
Author: User
when multiple files are merged into one final executable file, it is equivalent to running multiple files before the merge. This Program is often used in Trojan program merging. Do you want to know how it is implemented by a program? The following is an example of Code of a file bundle I used vc6.0 to tell you that the interface after running the program is as follows:
Figure 1

basic components: in fact, the components are very simple. Create a new binary file, first write the data of your bundle and its file length, and then write the data of the first file to be bundled and its file length, and then directly write the data and file length of the second file you want to bind ......, Finally, you can directly write the data of the last file you want to bind (without the length of the file ). When the final file is executed, that is, the above method can be reversed. (The following example only shows you how to bind two files. For the bundling of multiple files, you only need to add a few changes. For more information, see the downloaded instance code .)
let's talk about how to implement it:
1. bind multiple files to an executable program.
get the file length of the bundle program and the file length of the first file to be bundled, enumerate whether the first file to be bundled has an icon. If yes, use it as the final file generation icon. Otherwise, use the default icon in the bundle to generate the file. Write the data of the bundle program and the file length in the new binary file, write the data of the first file to be bundled and the file length, and then directly write the data of the Second file.
bind the Han number code as follows:
// bind multiple files to an executable file

Bool cbindfiledlg: bind_files () {file * myself; // its own file * out; // The Final Synthesis file * In; // The Int bytesin file to be bound; int totalbytes = 0; struct _ stat st; unsigned int finder = 0x12345678; unsigned int I, K; int L = 1; // The status shows char buff [20]; // his_name = strfirstfilepath; // The first bound file name _ Stat (my_name, & St); modify_data.my_length = ST. st_size; If (modify_data.my_length = 0) {MessageBox ("when the file is bound, an error occurs when the file length is zero! "," Error "); Return false;} Buf = (byte *) malloc (modify_data.my_length); If (BUF = NULL) {MessageBox (" bind file, an error occurred while allocating the file length! "," Error "); Return false;} myself = fopen (my_name," rb "); // open your own file if (myself = NULL) {free (BUF ); messageBox ("An error occurred while opening the file bound to the file! "," Error "); Return false;} bytesin = fread (BUF, 1, modify_data.my_length, myself); fclose (myself); If (bytesin! = Modify_data.my_length) {free (BUF); MessageBox ("in the bound file, the file cannot be completely read. An error occurred! "," Error "); Return false;} for (I = 0; I <modify_data.my_length-sizeof (finder); I ++ = sizeof (finder )) {for (k = 0; k <sizeof (finder); k ++) {If (BUF [I + k]! = (Byte *) & finder) [k]) break;} If (k = sizeof (finder )) // locate and save the data file size {memcpy (BUF + I, & modify_data, sizeof (modify_data); break ;}} if (I> = modify_data.my_length-sizeof (finder) {free (BUF); MessageBox ("in the bound file, an error occurred while locating the file itself! "," Error "); Return false;} If (_ Stat (strfirstfilepath, & St )! = 0 | st. st_size = 0) {free (BUF); MessageBox ("in the bound file, an error occurred while reading the first file to be bound! "," Error "); Return false;} list_my_icons (); out = fopen (strfinalfilepath," WB "); // create the final merged file if (out = NULL) {free (BUF); MessageBox ("in the bound file, an error occurred while creating the merged file generated after the binding! "," Error "); Return false;} totalbytes + = fwrite (BUF, 1, bytesin, out); In = fopen (strfirstfilepath," rb "); // An error occurred while opening the first file to be bound if (in = NULL) {free (BUF); MessageBox ("in file binding, opening the first file to be bound! "," Error "); Return false;} // write the length of the first file to be bound to the merged file to totalbytes + = fwrite (& St. st_size, 1, sizeof (St. st_size), OUT); // mark (synchronous or asynchronous) updatedata (true) of the file execution method after the final decomposition of the write ); // pass the control value to the variable m_sync. totalbytes + = fwrite (& m_sync, 1, sizeof (INT), OUT); While (bytesin = fread (BUF, 1, modify_data.my_length, in) {totalbytes + = fwrite (BUF, 1, bytesin, out);} fclose (in ); // close the first bound file handle // set the progress bar to display m_progress.setrange (0,500); (INT m = 0; m <500; m ++) m_progress.setpos (m); m_parts = _ ltoa (L, buff, 10 ); m_parts + = _ T ("files already bound"); updatedata (false); L ++; In = fopen (strsecondfilepath, "rb "); // An error occurred while opening the second file to be bound if (in = NULL) {free (BUF); MessageBox ("binding file, opening the second file to be bound! "," Error "); Return false;} while (bytesin = fread (BUF, 1, modify_data.my_length, In) {totalbytes + = fwrite (BUF, 1, bytesin, out);} // set the progress bar to show m_progress.setrange (0,500); For (INT n = 0; n <500; n ++) m_progress.setpos (N ); m_parts = _ ltoa (L, buff, 10); m_parts + = _ T ("files already bound"); updatedata (false); L ++; fclose (in ); // close the second bound file handle fclose (out); // close the final merged file handle free (BUF); // release the buffer return true ;}
2. Release the final merged files and run them simultaneously.
open your own file and get the file length of your bundle. Then, you can locate the file pointer to the first bundled file and read its file length and data, write the read data to the first new file. Similarly, the length of the Self-bundled program file that has been read, the length of the first bundled file, and the number of bytes in which the two files are stored, you can accurately locate the second bound file, read its data, and write it to the second new file. At the same time, run the two files and then delete them.
the code for releasing the final merged files is as follows:
// break down the merged files and run them simultaneously
Void cbindfiledlg: unbind () {file * myself; // file * out of the current file; // int bytesin In the decomposed file; int totalbytes = 0; char temp_exe1 [] = "temp1.exe"; // The name of the file to be bound after decomposition is Char temp_exe2 [] = "temp2.exe"; // The Name Of The BIND file after decomposition is int syncflag; // The final execution flag of the file: Buf = (byte *) malloc (modify_data.my_length); myself = fopen (my_name, "rb "); // open the final merged file if (myself = NULL) {free (BUF); MessageBox ("detaching the file, an error occurred while opening the file itself! "," Error "); return;} Out = fopen (temp_exe1," WB "); // create the first bound file if (out = NULL) {free (BUF); MessageBox ("in the split file, an error occurred while creating the first bound file! "," Error "); return;} // locate the file pointer to fseek (myself, modify_data.my_length, seek_set) at the end of the bundle program length ); // read the length of the first bound file if (fread (& prog1_length, sizeof (prog1_length), 1, myself) = 0) {free (BUF ); messageBox ("An error occurred while reading the length of the first bound file in the detached file! "," Error "); return;} // read the final File Execution method (synchronous or asynchronous execution) if (fread (& syncflag, sizeof (INT), 1, myself) = 0) {free (BUF); MessageBox ("An error occurred while reading the length of the first bound file in the separated file! "," Error "); return;} // read the content of the first file and write while (bytesin = fread (BUF, 1, sizeof (BUF), myself )) {If (totalbytes + bytesin> prog0000length) bytesin = prog0000length-totalbytes; totalbytes + = fwrite (BUF, 1, bytesin, out);} fclose (out ); // close the first bound file handle # ifdef debug_print fprintf (stderr, "% d bytes copied! \ N ", totalbytes); # endif debug_print totalbytes = 0; out = fopen (temp_exe2," WB "); // create the second bound file if (out = NULL) {free (BUF); MessageBox ("in the split file, an error occurred while creating the second bound file! "," Error "); return;} // locate the file pointer to the second bound file header in the final merged file, offset = // (length of the bundle's own file + number of bytes for saving the first bound file + number of bytes for saving the execution mark of the final file + length of the first bound file) fseek (myself, modify_data.my_length + sizeof (modify_data.my_length) + sizeof (INT) + prog1_length, seek_set); // read the content of the second bound file and write the while (bytesin = fread (BUF, 1, sizeof (BUF), myself) {totalbytes + = fwrite (BUF, 1, bytesin, out);} fclose (out ); // close the second bound file handle # ifdef debug_print fprintf (stderr ," % D byte \ n ", totalbytes); # endif debug_print fclose (myself); // close the final merging file handle if (totalbytes = 0) {free (BUF ); messageBox ("there are no separated objects in the file itself! "," Error "); return;} Free (BUF); // release the buffer if (! Syncflag) // 0 -- synchronous execution, 1 -- asynchronous execution {// after being set to decomposition, create_process (temp_exe1, false); create_process (temp_exe2, false );} else {// after being decomposed, the asynchronous Execution Mode create_process (temp_exe1, true); create_process (temp_exe2, true );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.