The Visual C/C++ compiler provides several ways to call internal and external functions. Understanding these different approaches can help you debug your program and link your code with assembly-language routines.
The following will explain the differences between the calling conventions, how arguments are passed, and how values are returned by functions.
__cdecl:
the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions. The __cdecl calling convention creates larger executables than __stdcall, because it requires each function call to include stack cleanup code.
Element | Implementation |
Argument-passing order | Right to left |
Stack-maintenance responsibility | Calling function pops the arguments from the stack |
Name-decoration convention | Underscore character (_) is prefixed to names |
Case-translation convention | No case translation performed |
__stdcall:
The __stdcall calling convention is used to call Win32 API functions. The callee cleans the stack, so the compiler makes vararg functions __cdecl. Functions that use this calling convention require a function prototype.
Element | Implementation |
Argument-passing order | Right to left. |
Argument-passing convention | By value, unless a pointer or reference type is passed. |
Stack-maintenance responsibility | Called function pops its own arguments from the stack. |
Name-decoration convention | An underscore (_) is prefixed to the name. The name is followed by the at sign (@) followed by the number of bytes (in decimal) in the argument list. Therefore, the function declared as int func( int a, double b ) is decorated as follows: _func@12 |
Case-translation convention | None |
__fastcall:
The __fastcall calling convention specifies that arguments to functions are to be passed in registers, when possible.
Element | Implementation |
Argument-passing order | The first two DWORD or smaller arguments are passed in ECX and EDX registers; all other arguments are passed right to left. |
Stack-maintenance responsibility | Called function pops the arguments from the stack. |
Name-decoration convention | At sign (@) is prefixed to names; an at sign followed by the number of bytes (in decimal) in the parameter list is suffixed to names. |
Case-translation convention | No case translation performed. |