_ Cdecl _ fastcall and _ stdcall

Source: Internet
Author: User
_ Cdecl _ fastcall and _ stdcall

Call conventions:
_ Cdecl _ fastcall and _ stdcall are call conventions (calling convention), which determine the following content: 1) the order of function parameters in the pressure stack; 2) the parameter pop-up stack is made by the caller or the caller. 3) the method for generating the function modifier name.

1. _ stdcall call Convention: The function parameters are passed from right to left through the stack. The called function clears the memory stack of the transfer parameter before returning,

2. _ cdecl is the default call Method for C and C ++ programs. Every function that calls it contains the code to clear the stack. Therefore, the size of the executable file generated is larger than that of the call to the _ stdcall function. The function uses the stack pressure mode from right to left. Note: For Variable Parameter member functions, always use the _ cdecl conversion method.

3. _ fastcall: it transmits parameters through registers (in fact, it uses ECx and EDX to transmit the first two DWORD or smaller parameters, the remaining parameters are still transmitted from the right to the left pressure stack, and the called function clears the memory stack of the transfer parameter before returning ).

4. thiscall is only applied to "C ++" member functions. This pointer is stored in the Cx register and the parameter is pressed from right to left. Thiscall is not a keyword and cannot be specified by programmers.

5. Naked call uses 1-4 call timing. If necessary, the compiler will generate code to save the ESI, EDI, EBX, and EBP registers when entering the function, when you exit the function, the code is generated to restore the content of these registers. Naked call does not generate such code. The naked call is not a type modifier, so it must be used together with _ declspec.

Call conventions can be selected through the Project Settings: setting... "C/C ++" code generation item. The default status is _ cdecl.

Naming Conventions:

1. decoration name: "C" or "C ++" functions are internally (compiled and linked) identified by modifier names.
2. Agreed rules for function name modification during C Compilation:
The _ stdcall call Convention adds an underline prefix before the output function name, followed by a "@" symbol and the number of bytes of the parameter. The format is _ functionname @ number. For example: function (int A, int B), whose name is: _ FUNCTION @ 8
The _ cdecl call Convention only adds an underline prefix before the output function name in the format of _ functionname.
_ Fastcall: Add a "@" symbol before the output function name, followed by a "@" symbol and the number of bytes of the parameter. The format is @ functionname @ number.

3. Agreed rules for function name modification during C ++ Compilation:
_ Stdcall call conventions:
1) "? "Mark the start of the function name, followed by the function name;
2) The function name starts with "@ YG", followed by the parameter table;
3) The parameter table is represented in code:
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 -- indicates the pointer. The code behind the pointer indicates the pointer type. If a pointer of the same type appears consecutively, it is replaced by "0". A "0" indicates a repetition;
4) the first item of the parameter table is the type of the return value of the function, followed by the Data Type of the parameter, and the pointer ID is before the data type referred to by the function;
5) The end of the name is marked with "@ Z" after the parameter table. If this function has no parameter, it ends with "Z.
The format is "? Functionname @ YG ***** @ Z "or "? Functionname @ YG * xz ", for example
Int test1 (char * var1, unsigned long) ----- "? Test1 @ yghpadk @ Z"
Void Test2 () ----- "? Test2 @ ygxxz"

_ Cdecl:
The rules are the same as the _ stdcall call Convention above, except that the start mark of the parameter table is changed from "@ YG" to "@ ya ".
_ Fastcall:
The rules are the same as the _ stdcall call Convention above, except that the start identifier of the parameter table changes from "@ YG" to "@ Yi ".
The "_ cedcl" Declaration for the function can only be called by C/C ++.

1. _ beginthread requires the thread function address of _ cdecl, and _ stdcall for _ beginthreadex and createthread.

2. Generally, Win32 functions are _ stdcall. Windef. H has 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 modifier is written in the above Order.

4. Function of extern "C": If add (int A, int B) is compiled in the C language compiler and used in the C ++ file, you need to declare in the C ++ file: extern "C" add (int A, int B ), because the C compiler and C ++ compiler have different interpretations of function names (function parameters must be considered when the C ++ compiler interprets a function name, which facilitates function overloading, in C language, there is no function re-loading problem). The essence of using extern "C" is to tell the C ++ compiler that this function is a function in the C library. If you do not use extern "C", a link error occurs.
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 to export the class, afx_api_export to modify the function, and 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
# Else

6. dllmain is responsible for initialization and termination. Whenever a new process or a new thread of the Process accesses the DLL, or when every process or thread accessing the DLL no longer uses the DLL or ends, it will call dllmain. However, using terminateprocess or terminatethread to end a process or thread does not call dllmain.

7. One dll has only one instance in the memory.
The relationship between the DLL program and the program that calls its output function:
1) Relationship between DLL and process and thread
The DLL module is mapped to the virtual address space of the process that calls it.
The dll Memory is allocated from the virtual address space of the calling process and can only be accessed by the thread of the process.
The DLL handle can be used by the calling process, and the call Process Handle can be used by the DLL.
Dlldll can have its own data segment but does not have its own stack. It uses the stack of the calling process, which is the same as the stack mode of the application that calls it.

2) about shared data segments

The global variables defined by dll can be accessed by the calling process; The dll can access the global data of the calling process. Each process using the same dll has its own DLL global variable instance. If multiple threads access the same variable, you need to use the synchronization mechanism. For a DLL variable, If you want each thread that uses the DLL to have its own value, the local thread storage should be used (TLS, Thread Local strorage ).

_ Cdecl Standard C call manual stack clearance from right to left
Stdcall New Standard C/C ++ calls automatic stack clearing from right to left
Pascal style Automatic stack clearance from left to right


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.