Win32 API: Calling Convention - 2020
Bookmark and Share
ms_icon.png

Argument Passing and Naming Conventions supported by Visual C++

A calling convention is a scheme for how functions receive parameters from their caller and how they return a result. The calling conventions can differ in where parameters and return values are placed (in registers; on the call stack; a mix of both), the order they are placed.

Calling conventions describe the interface of called code: - wiki:

  • The order in which atomic (scalar) parameters, or individual parts of a complex parameter, are allocated
  • How parameters are passed (pushed on the stack, placed in registers, or a mix of both)
  • Which registers the callee must preserve for the caller
  • How the task of preparing the stack for, and restoring after, a function call is divided between the caller and the callee

Convention Stack cleanup Parameter passing Mangling Style
__cdecl Caller Pushes parameters on the stack, in reverse order (right to left) _MyFunc
__stdcall Callee Pushes parameters on the stack, in reverse order (right to left) @MyFunc@Byte
__fastcall Callee Stored in registers, then pushed on stack (right to left) _MyFunc@Byte

The fillowing pictures shows the result from three calling conventions when we use the following function - msdn

void    calltype MyFunc( char c, short s, int i, double f );
...
void    MyFunc( char c, short s, int i, double f ){}
...
MyFunc ('x', 12, 8192, 2.7183);

__cdecl : The C decorated function name is "_MyFunc."

cdecl.png

__stdcall : The C decorated name (__stdcall) is "_MyFunc@20." The C++ decorated name is proprietary.

stdcall.png

__fastcall : The C decorated name (__fastcall) is "@MyFunc@20." The C++ decorated name is proprietary.

fastcall.png