__stdcall,__cdecl,__fastcall The difference Tags: dll compiler pascalclassimportinitialization2009-12-09 15:07 10472 people read reviews (1) Favorite report categories:C + + (+)
The difference between __stdcall,__cdecl,__fastcall
One or three list of differences
|
__stdcall |
__cdecl |
__fastcall |
Parameter Passing method |
Right-to-left pressure stack |
Right-to-left pressure stack |
On the left, two parameters that are not greater than 4 bytes (DWORD) are placed in the ECX and edx registers, and the remaining parameters are still transferred from right to left. |
Clean Stack Side |
The called function cleans up (that is, the function cleans itself), and most of the data uses this |
Caller cleanup |
Called by the caller to clean up the stack |
Apply Occasion |
Win API |
MFC, C + +, default mode Variable parameters are used when |
Fast speed |
C Compile Adornment conventions (none of them change the case of characters in the output function name) |
The 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] Umber |
The Convention only adds an underscore prefix to the name of the output function, in the form _functionname |
The calling convention adds an "@" symbol to the output function name, followed by an "@" symbol and the number of bytes of its argument, in the form @[email protected] Mber. |
C++ Modified |
See the following "Four, name decoration Convention" |
Second, thiscall
ThisCall is only applied to the "C + +" member function. 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.
Naked Call:
With the 1-4 calling convention, if necessary, the compiler generates code to hold the ESI,EDI,EBX,EBP register when the function is entered, and when the function exits, it generates code to recover the contents of those registers.
Naked call does not produce such a code. Naked call is not a type modifier and must be used in conjunction with _DECLSPEC.
Third, compiler options
The keywords __stdcall, __cdecl, and __fastcall can be directly added to the function to be exported, or they can be selected in the Setting.../c/c++/code generation item of the compilation environment. When the keyword before the output function is different from the selection in the compilation environment, the keyword directly before the output function is valid. Their corresponding command-line arguments are/gz,/gd, and/gr, respectively. The default state is/GD, which is __cdecl.
Four, the name Decoration Convention
1. Decorated name (decoration name)
"C" or "C + +" functions are identified internally (compiled and linked) by decorated names. A decorated name is a string that the compiler generates when compiling a function definition or prototype. In some cases, it is necessary to use the decorated name of the function, such as specifying the output "C + +" overloaded functions, constructors, destructors in the module definition file, as well as calling "C" or "C + +" functions in the assembly code.
Decorated names are determined by the function name, class name, calling convention, return type, parameters, and so on.
2, the name adornment convention Tiaogan with the Convention and compiles the kind (c or C + +) the difference and changes. The function name Adornment convention differs depending on the type of compilation and the calling convention, as described below.
A, 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 format [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].
They do not change the case of the character in the output function name, which differs from the Pascal calling convention in that the function name of the Pascal contract output is not decorated and capitalized.
b, c + + compile-time function name adornment Convention rules:
__stdcall calling convention:
1, with "?" Identifies the beginning of the function name followed by the name of the letter;
2, the function name after the "@ @YG" to identify the beginning of the parameter table, followed by the parameter table;
3, the parameter table 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, followed by the code indicates the pointer type, if the same type of pointer appears consecutively, with "0" instead of 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, the parameter table after the "@z" to identify the end of the entire name, if the function has no parameters, the "Z" Mark to end.
The format is "[email protected] @YG *****@z" or "[email protected] @YG *xz", for example
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.
Details can be found at: http://blog.csdn.net/hxuan999/archive/2009/01/13/3767409.aspx
__stdcall,__cdecl,__fastcall the difference