How to bundle multiple files into an executable
Author: Xu Jing Zhou
Download sample program code (http://www.9cbs.net/cnshare/soft/10/10356.shtm)
Combining multiple files into a final executable file, after running this final synthesis file, it is equivalent to running multiple files before the merge. This program will often use in Trojan program, do you want to know how it is implemented? Let's take me the instance code of a file bundle made by VC6.0 to tell you that the interface after the instance program runs as follows:
legend
Basic composition: In fact, the composition of the file bundle is very simple, it can be divided into part of the merged file and decomposition release file. When the merge file: Create a new binary, write into your bundle of its own data and its file length, then write the data of the first file you want to bundle and its file length, then write you directly The data of the second file to be bundled and its file length ..., finally written directly to the last file you want to bundle (no longer need its file length). Decomposing the final synthetic file: That is, the above method thinks can be, open the final synthesis file, read the source bundle itself, after moving the file pointer to the bundled program, read the first The length of the bound file is read, and then a file data of the subsequent size is read and written to a new file 1, then read the length of the second binding file, then read a file after the size of the length. The data is written to the new file 2 ... until the end, you can directly read the data of the bound file and write it to the last new file. (The following instance code only tells you how to implement the bundle of two files, as for the bundle of multiple files, readers only need to be alive in the above, please review the downloaded instance code.)
Let me talk about the most core part of the file bundle, and how to implement it with code:
1. Bundle multiple files for an executable program
Idea: First get the file length of the bundle itself and the file length of the first bundled file, enumerate the first icon of the bundled file, if there is icon, use it as the icon that finally generates files, otherwise it will be used The bundle of the program itself is an icon for the final generated file. Then, create a binary, write the file data of the bundle itself, and its file length, then written to the first file length of the first bundled file and the running mode flag (synchronization or asynchronous run), Then, the file data of the first bundled file is then written, and the file data that is directly written directly to the second bundled file can be used.
l The specific code of the merger mandate is achieved:
// Bind two files as an executable
Bool cbindfiledlg :: bind_files ()
{
... (omit: This part of the code is used to define all members variables)
His_name = strfirstfilepath; // The first bound file name
_stat (my_name, & st); // Get your own bundled file information
Modify_data.my_length = st.st_size; // Get your own file length
BUF = (byte *) malloc (modify_data.my_length; // Assign a certain size buffer
myself = fopen (my_name, "rb"); // Open your own file
// read the bundled program own file data first
BYTESIN = FREAD (buf, 1, modify_data.my_length, myself);
Fclose (Myself);
... (omissions: This part of the code is used to get your own file and the first bundled file length, and get the icon for the final synthetic file)
OUT = FOPEN (StrfiTfilePath, "WB"); // Creating the final synthesis file // first writing the data of the own bundle of its own bundles in the final synthesis file
TotalBytes = fwrite (buf, 1, bytesin, out);
IN = fopen (strfirstfilepath, "rb"); // Open the first file to be bound
// Write the first length to bind files to the final synthesis file
TotalBytes = fwrite (& St.st_size, 1, sizeof (st.st_size), out);
// Write the flag bit (synchronous or asynchronous execution) of the file execution mode after the final decomposition
Updatedata (TRUE); / / transmission control value to variable m_sync
Totalbytes = fwrite (& M_Sync, 1, Sizeof (int), out);
// Write the first data to bind the file to the final synthesis file
While (Bytesin = FREAD (BUF, 1, Modify_Data.my_length, in))
{
TotalBytes = fwrite (buf, 1, bytesin, out);
}
Fclose (in); // Close the first binding file handle
IN = fopen (strSecondFilePath, "RB"); // Open the second file to be bound
/ / Write directly to the second data to bind files to the final synthesis file
While (Bytesin = FREAD (BUF, 1, Modify_Data.my_length, in))
{
TotalBytes = fwrite (buf, 1, bytesin, out);
}
... (omit: This part of the code is used to close the file handle)
}
2. Release the final synthesis file and run them simultaneously.
Idea: Open your own file, get the length of the file yourself, position the file pointer to the first bundled file after the bundled file, read the length of the file, the final synthesis of the files of the bundled file The operation mode flag (synchronous or asynchronous mode is performed) after the file is released, and then the file data of its first bundled file is read, and the read data is written directly into a newly built binary. Similarly, through the read bundlers own file length and the first file length of the first bundled file, plus the number of bytes of the two file length values and the event of the running flag after the final synthesis file release. Number, you can accurately locate the pointer position of the file data of the bundled file. After reading its file data, it is written directly to a new binary. Finally, according to the running flag released by the previously read files, determine the two files that are released in the way, if it is synchronous mode, the order is running, and the two generation files will be deleted, and the asynchronous mode will be simultaneously Run without removing these two generated files.
l The code that releases the final synthetic file is implemented as follows:
/ / Decompose the merged file while running them
void cbindfiledlg :: unbind ()
{
... (omitted: This part of the code is mainly used to define member variables)
Myself = fopen (my_name, "rb"); // Open the final synthesis file
OUT = fopen (Temp_exe1, "WB"); // Create the first release generation file
// Position the file pointer to the tail of the bundle program
FSeek (Myself, modify_data.my_length, seek_set);
// Read the length of the first bound file
FREAD (& PROG1_LENGTH, SIZEOF (PROG1_LENGTH), 1, Myself);
// Read the final file execution mode (synchronous or asynchronous execution) Fread (& syncflag, sizeof (int), 1, myself);
// Read the first file content and write into the newly built TEMP1.EXE file
While (bytesin = fread (buf, 1, sizeof (buf), myself)
{
IF (Totalbytes Bytesin> PROG1_LENGTH)
BYTESIN = PROG1_LENGTH - TOTALBYTES;
TotalBytes = fwrite (buf, 1, bytesin, out);
}
Fclose (out); // Close the first binding file handle
TotalBytes = 0; // Re-zero
OUT = FOPEN (Temp_exe2, "WB"); // Create a second release generation file
/ * Position the file pointer to the second binding file header in the final synthesis file, offset == (Bundlers own file length save the first binding file length account for the number save the final file Execute the number of bytes of the sign the first binding file length) * /
FSeek (myself, modify_data.my_length sizeof (modify_data.my_length) sizeof (int) prog1_length, seek_set);
/ / Read the second binding file content and write into the new TEMP2.EXE file
While (bytesin = fread (buf, 1, sizeof (buf), myself)
{
TotalBytes = fwrite (buf, 1, bytesin, out);
}
... (omissions: This part of the code is used to close the file handle and the decision to generate the function of the file)
}
3. Judging when to bundle the file while decomposing the final synthetic file.
Idea: Since this program uses the file header that is directly synthesized directly as the final synthetic file, the bound file data is added to generate the final synthesis file. So, as long as you know the length of the file yourself, depending on whether the length of the file is greater than the original file length, it can be judged whether or not the decomposition file is released. This judgment can be added in the initialization dialog box of ONINITDIALOG (), and know if it is the final synthesis file (or not to release the internal binding file). This example uses VC6.0 using the Release version generated by static connection, with a file length of 184K, greater than 184K (184 * 1024) bytes, can be judged to determine the release decomposition operation.
l If it is determined whether the code is bundled or the release file is implemented as follows:
Bool cbindfiledlg :: oninitdialog ()
{
... (omit: This part of the code is used to initialize some variables)
/ / Get your own file name to my_mane variable
:: getModuleFileName (0, my_name, sizeof (my_name));
Struct_stat st;
_stat (my_name, & st); // Get your own file information (length)
/ * The final size of the bundle program is added here to determine whether the file execution is a binding file or an exploded execution file When it is found that the own file size is greater than the original size 184K, in order to release internal synthesis files * /
IF (st.st_size> 184 * 1024)
{
Unbind (); // Separate files and run
Exit (0); / / Direct exit program, do not display the bundle screen
}
}
The details of the specific implementation of the above part of the code can be downloaded after downloading the instance code, and the source code can be carefully viewed (in detail).
Contact information:
Future Studio (Future Studio)