Several concepts of function calls

Source: Internet
Author: User
Tags definition exit file size function definition win32

Left through the stack, the called function cleans up the memory stack of the transfer parameters before returning, but the modified portion of the function name (the cosmetic portion of the function name is described later in detail).

_stdcall is the default calling method of Pascal program, usually used in the Win32 API, the function uses a right to left stack way, its own on the exit empty stack. VC compiles the function with an underscore prefix in front of the function name, plus the "@" and the number of bytes in the parameter after the function name.

2, c calling convention (that is, with the __CDECL keyword description) press from right to left in the sequence of parameters into the stack, by the caller to the parameters pop-up stack. The memory stack for the transfer parameter is maintained by the caller (because of this, a function that implements a variable parameter can only use that calling convention). In addition, there are differences in function name modification conventions.

_CDECL is the default invocation 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 takes the form of a right to left pressure stack. VC compiles the function with an underscore prefix in front of the function name. Is the MFC default calling convention.

3, the __fastcall calling convention is "person" as its name, its main feature is that it is fast because it transmits parameters through registers (in fact, it transmits the first two double words (DWORD) or smaller arguments with ECX and edx, and the remaining parameters are still transferred from the right to left press stack, The called function cleans up the memory stack of the transfer parameters before returning it, and it differs from the previous two in terms of the Function name Modification Convention.

The function of _fastcall method adopts register pass parameter, VC will compile function to precede function name with "@" prefix, add "@" after function name and number of bytes of parameter.

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

5, Naked call using the 1-4 of the calling convention, if necessary, the compiler will produce code to save the ESI,EDI,EBX,EBP register, exit the function when the code to restore 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.

Keywords __stdcall, __cdecl, and __fastcall can be added directly to the function to be exported, or to the setting...\c/c++ \code generation item selection in the compilation environment. When the keyword before the output function differs from the selection in the compilation environment, the keyword directly preceding the output function is valid. Their corresponding command-line arguments are/GZ,/gd, and/gr respectively. The default state is/GD, or __cdecl.

To completely imitate the Pascal calling convention, you must first use the __stdcall calling convention, and the function name modification conventions can be imitated by other methods. Another noteworthy is the WINAPI macro, Windows.h that supports the macro, which translates the function into the appropriate calling convention, which in WIN32 is defined as __stdcall. Use the WINAPI macro to create your own APIs.

2) Name Modification Convention

1, decorated name (decoration name)

The "C" or "C + +" functions are identified internally (compiled and linked) by the decorated name. 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 a function, such as specifying output "C + +" overloaded functions, constructors, destructors in the module definition file, and so on, as in assembly code.

Decorated names are determined by function names, class names, calling conventions, return types, parameters, and so on.

2, the name Modification Convention Tiaogan with the Convention and the compilation type (C or C + +) different and changes. The function name modification conventions vary, depending on the type of compilation and the calling convention, as described below.

A, C compile-time Function name Modification 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 arguments, formatted as _functionname@number.

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 arguments, in the form of @functionname@number.

They do not change the character case in the output function name, which is different from the Pascal calling convention, and the function name of Pascal convention output is not decorated and all uppercase.

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.