Among all the pre-processing commands, the # pragma command may be the most complex. It is used to set the compiler status or to instruct the compiler to complete some specific actions. # The Pragma command provides a method for each compiler to provide the unique features of the host or operating system while maintaining full compatibility with C and C ++ languages. According to the definition, the compilation instructions are proprietary to machines or operating systems and are different for each compiler.
The format is generally: # pragma para
Here, para is a parameter. Below are some common parameters.
(1) Message parameter. The message parameter is one of my favorite parameters. It can be used in the compilation information output window.
Output the corresponding information, which is very important for controlling the source code information. The usage is as follows:
# Pragma message ("message text ")
When the compiler encounters this instruction, it prints the message text in the compilation output window.
When we define many Macros in the program to control the source code version, we may forget whether these macros are correctly set, in this case, we can use this command to check it during compilation. Suppose we want to determine whether we have defined the _ x86 macro in the source code. The following method can be used:
# Ifdef _ x86
# Pragma message ("_ x86 macro activated !")
# Endif
After we define the _ x86 macro, the application will display "_
X86 macro activated !". We won't be scratching our heads because we don't remember some specific macros we defined.
.
(2) The other Pragma parameter that is used more frequently is code_seg. Format:
# Pragma code_seg (["section-name" [, "section-class"])
It can set the code segment where function code is stored in the program. It is used when we develop the driver.
(3) # pragma once (commonly used)
You only need to add this command at the beginning of the header file to ensure that the header file is compiled once. This command is actually available in vc6, but it is not widely used in consideration of compatibility.
(4) # pragma hdrstop indicates that the pre-compiled header file ends here, and the subsequent header files are not pre-compiled. BCB can pre-compile the header file to speed up the link, but if all header files are pre-compiled, it may occupy too much disk space. Therefore, this option is used to exclude some header files.
Sometimes there is a dependency between units. For example, unit a depends on unit B. Therefore, Unit B must be compiled before unit. You can use # pragma startup to specify the compilation priority. If # pragma package (smart_init) is used, BCB will be compiled based on the priority.
(5) # pragma resource "*. DFM" indicates adding resources in the *. DFM file to the project. * DFM includes the form
The definition of the appearance.
(6) # pragma warning (Disable: 4507 34; once: 4385; error: 164)
It is equivalent:
# Pragma warning (Disable: 4507 34) // do not display the 4507 and 34 Warnings
# Pragma warning (once: 4385) // only one warning message is reported once
# Pragma warning (error: 164) // the error message 164 is used as an error.
This pragma warning also supports the following formats:
# Pragma warning (push [, N])
# Pragma warning (POP)
Here N represents a warning level (1---4 ).
# Pragma warning (push) saves the existing warning status of all warning information.
# Pragma warning (push, n) saves the existing warning status of all warning information, and sets global warning
The level is set to n.
# Pragma warning (POP) pops up the last warning message to the stack.
All changes are canceled. For example:
# Pragma warning (push)
# Pragma warning (Disable: 4705)
# Pragma warning (Disable: 4706)
# Pragma warning (Disable: 4707)
//.......
# Pragma warning (POP)
At the end of the Code, save all warning information (including 4707, and ).
(7) Pragma comment (...)
This command puts a comment record into an object file or executable file.
Common lib keywords can help us to connect to a library file.
# Pragma pack (1)
It is mainly used to set the byte alignment mode of the structure definition, such as single-byte alignment and double-byte alignment. For example, if it is double-byte alignment, the address of the member variable of the structure must be an integer multiple of 2, which leads to the completion of byte, but increases the access speed. A single byte is not completed. The address of the member variable is continuous, and so on. It is generally. It is usually used for data transmission over the network, especially when the entire structure is transmitted, the single-byte alignment must be adopted so that the structure address and structure length can be directly sent as the send parameter to the entire structure, otherwise, only the members of the structure can be sent in sequence, or the difference in structure interpretation may occur.
In addition, you can set the structure alignment in project-> setting-> C/C ++-> code generation-> struct member.
The transfer structure is irrelevant to the pack. As long as the structure defined by the Recv end is the same as that defined by the sender, it will be okay.
Pack is mostly used for Hook programs, such as hook api technology. Because hard encoding is required, the structure must be
Compress and complete the content!
For example:
Asm_struct {
Byte BJMP;
DWORD dwdes;
};
If pack is not used, the compilation is:
A. BJMP = 0xeb; // JMP Encoding
A. dwdes = 0x00410123; // JMP 0x00410123
If no pack is needed, the memory content is 0xeb xx 23 01 41 00 // a total of 8 bytes
XX is an indefinite value. After pack is used, 0xeb 23 01 41 00 // a total of 5 bytes
In this way, when running these commands in the hook, the # parama pack (1) // 1 byte method must be used for alignment.
If the structure address and length are directly sent to the entire structure as the send parameter, Do you not need to pack it? Consult verybigbug ()
No.
Send (struct, sizeof (struct); then, if neither program has pack,
The same struct is no problem when receiving data. Ensure that the packages on both sides are the same.
Pack is never used when I write the socket program (SDK mode.
Pack (1) is used only when writing hook APIs ).
# What does Pragma comment (Lib, "ws2_32") mean ???
. You include ws2_32.lib in the project. In this way, you can use the DLL interface function.
# What does Pragma data_seg mean ??
Use # pragma data_seg to create a new data segment and define shared data. The specific format is:
# Pragma data_seg ("shareddata ")
Hwnd sharedwnd = "null"; // share data
# Pragma data_seg () // The core programming ideology of Windows is used in many places.