Calling Convention includes defining how to pass parameters, receive calculation results, and maintainProgramContext and stack cleanup. The complete mode for writing a function should be:
Bool winapi isdebuggerpresent (void );
Among them, winapi is the name of the call agreement. If there is no call agreement, the compiler will use the default call agreement according to the language environment.
Common call agreements include: C call agreements, standard call agreements, quick call agreements, and this call agreements used by C ++ members.
The C call protocol is the default call protocol for common functions of C/C ++ programs. parameters are transmitted completely using stacks. Function callers (caller) before a call is initiated, press the parameter from right to left into the stack, and adjust the stack pointer to clear the parameters pushed into the stack after the function returns. The benefit of pressing parameters from right to left is that the offset of the first parameter to the top of the stack is always fixed for the called function, and the caller is responsible for clearing the parameters, therefore, the C call protocol supports variable parameters.
The standard call protocol is similar to the C call protocol. The stack is used to transmit parameters, and the transfer sequence is from right to left. However, the difference is that the standard call Protocol requires the called function to clear parameters in the stack. For a function that is frequently called, the advantage is that the size of the target program can be reduced, because the instructions for clearing the stack do not need to appear repeatedly in the call function, the keyword used by the standard call protocol is _ stdcall.
Most Windows functions and APIs use standard call protocols, but they are generally aliases using the _ stdcall keyword. From the windef. h file, we can find that the winapi and callback keywords are actually _ stdcall.
Most windows APIs and callback functions use the standard call protocol. All functions using the standard call protocol must have a function prototype.
The fast call protocol (fastcall) uses the Protocol to transmit parameters using registers, so it is faster than using the stack. For 32-bit programs, the quick call protocol uses ECx and EDX to transmit the first two (left-Start) parameters with a length of no more than 32 bits, other parameters are passed through stack (from right to left ). For example, if the first parameter (starting from left) is _ int64, the second parameter is int, and the third parameter is Char, the first parameter is passed through stack, the second parameter is passed through ECx, and the third parameter is passed through EDX.
The call protocol used by the member functions in the C ++ program by default is the this call protocol, the most important feature of this call protocol is that this pointer will be put into the ECX register and passed to the called method. This call Agreement also requires the called function to clean up the stack, so variable parameters are not supported. When a variable number of parameter member functions are defined in the C ++ class, the compiler (VC) automatically uses the C call protocol. When this method is called, the compiler pushes all parameters to the stack and then pushes this pointer to the stack.
The VC compiler before vc8 does not define keywords for this call protocol. Therefore, programmers cannot explicitly specify this call protocol. vc8 adds _ thiscall as the keyword of this call protocol, because the managed C ++ uses the newly defined CLR call protocol by default, if you want to use the traditional this call protocol, you need to add the _ thiscall keyword in the Declaration.
CLR Call Protocol: Before vc8, the compiler generates two entry points for each managed function. One is the managed entry point and the other is the local (native) entry point. In this way, when hostedCodeWhen you use a local entry point to call this function (the virtual function must always be called using a local entry point), the local entry point must forward the call to the hosted entry point, this causes unnecessary hosted/unmanaged context switching and Parameter/return value replication. This phenomenon is called double thunking ). Because dual conversions can cause performance loss, Visual Studio 2005 introduces a new call Convention keyword. If a managed function is not called by unmanaged code using pointers, when declaring this function, you can use the new _ clrcall modifier to prevent the compiler from generating two entries.
Because the traditional DLL output method (dllexport) is used to output a managed function, the compiler generates two entry points for it, in addition, any call to this function through the DLL method will use the local entry point (then transferred to the managed entry point ). To prevent double conversions in this case, we recommend that you use the. NET module reference method in the SDK documentation. Do not use the traditional DLL method.
X64 Call Protocol is applicable to 64-bit systems. Only one call protocol derived from quick call is used, and the first four parameters are transmitted using registers. Other parameters are transmitted using stacks.
For a function that does not specify the call protocol in the function declaration, the compiler will use the appropriate default call protocol based on the situation. You can also specify the call protocol through the compilation option.
/GD, which is set by default. The C call protocol (_ cdecl) is used as the default call protocol.
/GR, using the quick call protocol (_ fastcall) as the default Call Protocol
/GZ, using the standard call protocol (_ stdcall) as the default call protocol.