The difference between C + + _stdcall and __stdcall __c++

Source: Internet
Author: User
Tags function definition win32
Today, when writing the thread function, we found that the definition of ThreadProc in MSDN is required: DWORD WINAPI ThreadProc (LPVOID lpparameter);

Puzzled why to use the WINAPI macro definition, after checking found the following definition. There is a need to distinguish between __stdcall and __cdecl; #define CALLBACK __stdcall
#define WINAPI __stdcall
#define WINAPIV __cdecl
#define APIENTRY WINAPI
#define Apiprivate __stdcall
#define PASCAL __stdcall
#define CDECL _cdecl
#ifndef CDECL
#define CDECL _cdecl
#endif

Almost every Windows API function we write is __stdcall type, first of all, we need to understand the difference between the two: Windows function calls need to use the stack (stack, a first-come out of the storage structure). When the function call is complete, the stack needs to be clear, and here is the key to the problem, how to clear it. If our function uses _cdecl, then the stack cleanup is done by the caller, in COM terms, the client. This poses a tricky problem, and different compilers generate stacks differently, so the caller can perform the cleanup work normally. The answer is no. If you use __stdcall, the above problem is solved, the function solves the cleanup work by itself. So we all use __stdcall (although sometimes WINAPI) in the call to the Cross (development) platform. So why do we need _cdecl? When we encounter such a function such as fprintf () its parameters are variable, indefinite length, the caller can not know the length of the parameters in advance, the removal of the subsequent work can not be normal, therefore, this situation we can only use _cdecl. Here we have a conclusion that if your program does not involve variable parameters, it is best to use the __stdcall keyword.

2. __cdecl,__stdcall is a declared function invocation protocol. It is mainly the difference between the reference and the stack. General C + + is used __cdecl,windows most of the __stdcall (API)

__CDECL is the calling convention that is used by default by the C + + and MFC programs, and can be manually specified when the function declaration is added with the __CDECL keyword. When the __CDECL convention is used, function parameters are stacked in the right to left order, and the Calling function pops the stack to clean the stack. Therefore, a function that implements a variable parameter can only use that calling convention. Because each function that uses the __CDECL convention contains the code to clean the stack, the resulting executable file size is larger. __cdecl can be written as _cdecl.
The __stdcall calling convention is used to invoke Win32 API functions. When using the __stdcall convention, function parameters are placed in the stack from right to left, and the called function clears the stack of transfer parameters before returning, and the number of function parameters is fixed. Because the function body itself knows the number of incoming parameters, the called function can clean the stack of passing arguments directly with a ret n instruction before returning. __stdcall can be written as _stdcall.
The __fastcall convention is used for situations where performance requirements are high. The __fastcall Convention places a function of two sizes of not more than 4 bytes (DWORD) from the left to the ECX and edx registers, and the remaining parameters are still transferred from the right to left stack, and the called function cleans up the stack of the transfer parameters before returning. __fastcall can be written as _fastcall

3. __stdcall:

The _stdcall calling convention is equivalent to the Pascal calling convention that is often used in 16-bit dynamic libraries.


Pascal calling convention is no longer supported in 32-bit vc++5.0 (in fact it has been defined as __stdcall.) In addition to __pascal, __fortran and __syscall are not supported, and instead are __stdcall calling conventions. The two are essentially consistent, that is, the parameters of the function are passed from right to left through the stack, and the called function cleans up the memory stack of the transfer parameters before returning, but the modified portion of the function name (the cosmetic part about 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.

_CDECL:

_CDECL c calling convention, press the parameters from right to left into the stack, by the caller to the parameter 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.

__fastcall:

The __fastcall calling convention is "human" 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.

ThisCall:

ThisCall applies only 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.

Naked Call:

When using the 1-4 calling convention, if necessary, the compiler produces code to hold the ESI,EDI,EBX,EBP register when it is entered, and the contents of those registers are generated when the function exits.

Naked call does not produce such a code. Naked call is not a type modifier and must be used in conjunction with _DECLSPEC.

Also attached:

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.

Name Modification conventions

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.

b, c + + compile-time Function name Modification Convention rules:

__stdcall calling convention:
1. To "?" Identifies the start of the function name, followed by the number of functions;
2, after the function name with "@ @YG" identifies the start 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,
....
The pa--represents a pointer, followed by a code indicating the pointer type, if the same type of pointer appears continuously, substituting "0", a "0" represents a repetition;
4, the first item of the parameter table is the return value type of the function, followed by
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.