1. EXE file Concept
EXE file: executable file, which is an executable file. It can be transplanted into the memory and executed by the operating system, it is an executable program that can be floating in the operating system storage space. For example, the notepad.exe program can be used to edit documents. For example, test. txt double-hitting the notepad.exe Notepad program for editing.
Ii. EXE file structure
The EXE file is divided into two parts: the EXE file header and the program body. The EXE file is complex and belongs to a multi-segment structure. It is one of the most successful and complex dos designs. Each EXE file contains a file header and an image of the relocated program. The file header contains information for the MS-DOS to load the program, such as the program size and initial register values. The file header also points to a relocation table, which contains a linked list of pointers pointing to the relocated segment addresses in the program image. The MS-DOS loads the EXE program by copying the image directly from the file to the memory, and then adjusts the relocated segment address described in the positioning table. The positioning table is a relocation pointer array, each pointing to the relocated segment address in the program image. For details about the principle and structure, click the structure and principle of the EXE file.
The preceding figure shows the EXE file header.
Iii. Open EXE files
Because the EXE file is special, the open method is also a bit special, you need to specify the format, see the following code:
#include<iostream>#include<string>#include<fstream>#include<ios>using namespace std;const int BUFFER_SIZE=1024;void update(ifstream& in) {if (!in) {return;}std::streamsize length;char buffer[BUFFER_SIZE];while (!in.eof()){in.read(buffer, BUFFER_SIZE);length = in.gcount();if (length > 0) {printf("%s",buffer);}}in.close();}int main(){update(ifstream("1.exe", ios_base::binary));return 0;}
Ios_base is a class in the C ++ standard library and is defined in the header file <IOS>. The ios_base class encapsulates basic information about data types that do not depend on read/write in the Stream Input and Output in the C ++ standard, such as formatting information, exception status, and Event Callback functions.