C + + compile-time Function name Decoration Convention rules (very specific), MFC provides macros, the role of extern "C"

Source: Internet
Author: User

calling convention:
__cdecl __fastcall and __stdcall, all three are calling conventions (calling convention), which determine the following: 1) the stack order of the function arguments, 2) The arguments are popped by the caller or by the caller, 3) and the method that produces the function decorated name.

1. __stdcall calling convention: The function parameter is passed from right to left through the stack, and the called function cleans the memory stack of the transfer parameters before returning.

2. _CDECL is the default calling method for C and C + + programs. Each function that calls it contains the code that empties the stack, so the resulting executable file size is larger than the call to the _stdcall function. The function uses a right-to-left compression stack. Note: For variable parameter member functions, always use the __cdecl conversion method.

3. __fastcall calling convention: it transmits the parameters via registers (in fact, it transmits the first two double words (DWORD) or smaller parameters with ECX and edx, the remaining parameters are still transferred from right to left, and the called function cleans up the memory stack of the transfer parameters before returning.

4. ThisCall applies only to "C + +" member functions. This pointer is stored in the CX register and the parameters are pressed from right to left. ThisCall is not a keyword and therefore cannot be specified by the programmer.

5. When naked call uses 1-4 of the calling convention, if necessary, the compiler generates code to hold the ESI,EDI,EBX,EBP register when it enters the function, and the Exit function generates code to recover the contents of these registers. Naked call does not produce such a code. Naked call is not a type modifier and must be used in conjunction with _DECLSPEC.

The calling convention can be selected from the project setup: Setting...\c/c++ \code generation, with the default status of __cdecl.

Name Decoration Conventions:

1, decorated name (decoration name): "C" or "C + +" functions are internally (compiled and linked) by decorated name recognition
2. C Compile-time function name adornment Convention rules:
The __stdcall calling convention adds an underscore prefix to the output function name, followed by an "@" symbol and the number of bytes of its argument, in the form [email protected], for example: function (int a, int b), with the decorated name: [email Protected
The __cdecl calling convention only adds an underscore prefix to the output function name, in the form of _functionname.

The __fastcall calling convention adds an "@" symbol to the output function name, followed by an "@" symbol and the number of bytes of its parameter, in the format @[email protected].

3, C + + compile-time Function name Adornment Convention rule:  
__stdcall calling convention:  
1), with "?" The beginning of the identification function name, followed by the  
2), the function name followed by "@ @YG" to identify the beginning of the parameter table, followed by the parameter table;  
3), the parameter list is indicated by the code name:  
x–void , 
d–char, 
e–unsigned char, 
f–short, 
h–int, 
i–unsigned int, 
j–long, 
k–unsigned long, 
m–float, 
n–double, 
_n–bool, 
PA– Represents the pointer, the following code indicates the pointer type, if the same type of pointer appears consecutively, with "0" instead, a "0" represents a repetition;  
4), the first item of the parameter table is the return value type of the function, followed by the data type of the parameter, and the pointer is identified before the data type it refers to;  
5), after the parameter table, "@z" identifies the end of the entire name, and if the function has no arguments, the "Z" Mark ends.  
is formatted as "[email protected] @YG *****@z" or "[email protected] @YG *xz", such as  
Int test1 ( Char *var1,unsigned long)-"[email protected]@[email protected]"  
Void Test2 ( )  -– "[email protected] @YGXXZ"  

__cdecl calling convention:
The rule is the same as the _stdcall calling convention above, except that the start identifier of the parameter table is changed from "@ @YG" to "@ @YA".
__fastcall calling convention:
The rule is the same as the _stdcall calling convention above, except that the start identifier of the parameter table is changed from "@ @YG" to "@ @YI".
VC + + On the function of the default declaration is "__CEDCL", will only be called by C + +.

Attention:
1, _beginthread need __cdecl thread function address, _beginthreadex and createthread need __stdcall thread function address.

2, the general WIN32 function is __stdcall. And in Windef.h, there are the following definitions:
#define CALLBACK __stdcall
#define WINAPI __stdcall

3, extern "C" _declspec (dllexport) int __cdecl Add (int a, int b);
typedef int (__cdecl*funpointer) (int a, int b);
The modifiers are written in the order shown above.

4, extern "C" function: if ADD (int a, int b) is compiled in the C language compiler, and in C + + files, you need to declare in C + + file: extern "C" Add (int a, int b), because C compiler and C + + The compiler's interpretation of the function name is different (the C + + compiler interprets the function name to consider the function parameters, which is convenient function overloading, and in C language does not have the problem of function overloading), using the extern "C", the essence is to tell the C + + compiler, the function is the C library inside the function. If you do not use extern "C" then a link error will occur.
It is generally used as follows:
#ifdef _cplusplus
#define Extern_c EXTERN "C"
#else
#define Extern_c EXTERN
#endif

#ifdef _cplusplus
extern "C" {
#endif
Extern_c int func (int a, int b);
#ifdef _cplusplus
}
#endif

5, MFC provides some macros, you can use Afx_ext_class to replace __declspec (DLLexport), and modify the class name, thereby exporting the class, afx_api_export to modify the function, Afx_data_export to modify the variable
Afx_class_import:__declspec (DLLexport)
Afx_api_import:__declspec (DLLexport)
Afx_data_import:__declspec (DLLexport)
Afx_class_export:__declspec (DLLexport)
Afx_api_export:__declspec (DLLexport)
Afx_data_export:__declspec (DLLexport)
Afx_ext_class: #ifdef _afxext
Afx_class_export
#else
Afx_class_import

6. DllMain is responsible for initializing (initialization) and ending (termination) work, whenever a new process or new thread of the process accesses the DLL, or every process or thread that accesses the DLL no longer uses the DLL or ends, Will call DllMain. However, using terminateprocess or TerminateThread to end a process or thread does not call DllMain.

7. A DLL has only one instance in memory
The relationship between a DLL program and a program that calls its output function:
1), the relationship between DLL and process, thread
The DLL module is mapped to the virtual address space of the process that called it.
The memory used by the DLL is allocated from the virtual address space of the calling process and can only be accessed by the thread of the process.
The handle to the DLL can be used by the calling process, and the handle to the calling process can be used by the DLL.
Dlldll can have its own data segment, but not its own stack, using the same stack mode as the application calling it's stack, with the calling process.

2), about shared data segments
The global variables defined by the DLL can be accessed by the calling process, and the DLL can access the global data of the calling process. Each process that uses the same DLL has its own DLL global variable instance. If multiple threads access the same variable concurrently, you need to use the synchronization mechanism; For a DLL variable, you should use thread-local storage (Tls,thread local strorage) If you want each thread that uses the DLL to have its own value.

Http://www.x86pro.com/article/calling-convention

C + + compile-time Function name Decoration Convention rules (very specific), MFC provides macros, the role of extern "C"

Related Article

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.