Today, I started debugging windows core programming routines and it was very difficult to understand, because my c ++ skills were too weak.
First, a large number of macro problems are used in the Windows programming process.
# Pragma usage
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 instruction is a machine
Or the operating system proprietary, and for each compiler is different.
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 there is a positive
Indeed, we can use this command to check these macros during compilation. Suppose we want to determine
Have you 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 used in vc6
But considering the compatibility, It is not used too much.
(4) # pragma hdrstop indicates that the pre-compiled header file ends here, and the subsequent header files are not pre-compiled. BCB can be pre-configured
Compile the header file to speed up the link, but if all header files are pre-compiled, it may occupy too much disk space.
Use this option 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 # P
Ragma startup specifies the compilation priority. If # pragma package (smart_init) is used, BCB will compile according to the priority.
(5) # pragma resource/*. DFM/indicates adding the 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.
(8) · use # pragma pack (n) to change the byte alignment of the C Compiler
In C, the structure is a composite data type, and its components can be basic data types (such as int,
Long, float, and so on) variables can also be Composite data types (such as arrays, structures, joins, etc.)
Data Unit. In the structure, the compiler divides each member of the structure according to its natural division (alignment) condition.
Configuration space. Each member is stored in the memory in the declared order. The address and
The address of the entire structure is the same.
For example, the following structure shows the allocation of member spaces:
Struct Test
{
Char x1;
Short X2;
Float X3;
Char X4;
};
The first member of the structure X1, whose offset address is 0, occupies 1st bytes. The second member X2 is
Short type. The starting address must be 2 bytes in the peer interface. Therefore, the compiler fills
NULL bytes. The third member X3 and fourth member X4 of the structure happen to be on its natural peer address.
You do not need to fill in additional bytes. In the test structure, the X3 member requires a 4-byte perimeter.
The maximum peer unit required by all members. Therefore, the test structure naturally has four limitations.
Three NULL bytes are added after Member X4. The entire structure occupies 12 bytes of space. Change the C compiler's
Default byte alignment
By default, the C compiler assigns each variable or data unit based on its natural perimeter conditions.
Space. Generally, you can use the following method to change the default peer condition:
· With the pseudo command # pragma pack (n), the C compiler will align according to n Bytes.
· Use the pseudo command # pragma pack () to cancel the custom byte alignment.
In addition, the following method is provided:
· _ Attribute (aligned (N) to align the structure members to the natural boundary of n Bytes.
If the length of a member in the structure is greater than N, the maximum member length is used for alignment.
· _ Attribute _ (packed) to cancel structure optimization alignment during compilation.
The number of bytes occupied is aligned.
The first method above n = 1, 2, 4, 8, 16... is more common.
Application Instance
In network protocol programming, data packets of different protocols are often processed. One way is to use pointer offset
Method to obtain various information, but this method is not only complicated in programming, but also the program is modified once the Protocol changes.
It is also troublesome. After learning about the compiler's allocation principles for the structure space, we can use this
Define your own protocol structure and obtain various information by accessing the members of the structure. In this way,
It not only simplifies programming, but even if the protocol changes, we only need to modify the definition of the protocol structure,
Other programs do not need to be modified, saving time and effort. The following uses the TCP protocol header as an example to describe how to define the protocol structure.
The protocol structure is defined as follows:
# Pragma pack (1) // align in 1 byte mode
Struct tcpheader
{
Short srcport; // 16-bit source port number
Short dstport; // 16-bit destination port number
Int serialno; // 32-bit serial number
Int ackno; // 32-bit confirmation number
Unsigned char haderlen: 4; // 4-bit Header Length
Unsigned char reserved1: 4; // retain four of the six digits
Unsigned char reserved2: 2; // retain two of the six digits
Unsigned char URG: 1;
Unsigned char ack: 1;
Unsigned char PSH: 1;
Unsigned char rst: 1;
Unsigned char SYN: 1;
Unsigned char Fin: 1;
Short windowsize; // 16-bit window size
Short tcpchksum; // 16-bit TCP check
Short urgentpointer; // 16-bit emergency pointer
};
# Pragma pack () // cancel the 1-byte alignment
From: http://www.cnblogs.com/flying_bat/archive/2008/04/23/1167640.html