C ++ call conventions and naming conventions

Source: Internet
Author: User

Http://www.cppblog.com/mzty/archive/2007/04/20/22349.html

C ++ call conventions and naming conventions

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 C and C ++Program. Every function that calls it containsCodeTherefore, the size of the executable file is larger than that of 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:
_ Stdcall indicates that an underline prefix is added before the output function name, followed by the "@" symbol and the number of bytes of the parameter. The format is_ Functionname @ numberFor 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. Conventions for modifying function names during C ++ Compilation:
__ stdcall call conventions:
1), "? "Start of the identification function name, followed by the function name;
2) after the function name, start with" @ YG "to identify the parameter table, followed by the parameter table;
3) parameter tables are represented in codes:
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, and the code behind it indicates the pointer type. If pointers of the same type appear consecutively, it is replaced by "0", and a "0" represents a duplicate.
4) the first item in the parameter table is the type of the return value of the function, followed by the Data Type of the parameter, pointer identification before the data type it refers to;
5) after the parameter table, the end of the entire name is marked with "@ Z". If the function has no parameter, end with the "Z" mark.
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 ++.

Note:
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

The default conventions for C ++ and C are _ cdecl.

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 inC LanguageCompile the compiler, and use it in the C ++ file, you must 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 overload 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
Afx_class_export
# Else
Afx_class_import

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 concurrently 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 ).

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.