Calling Conventions (Cdecl, and StdCall) (C/C++)

What is Calling Conventions?
When a function is called, the arguments are typically passed to it, and the return value is retrieved. A calling convention describes how the arguments are passed and values returned by functions. It also specifies how the function names are decorated. Examples of calling conventions for functions are __cdecl and __stdcall.
 
What is Cdecl?
Older, unmanaged code that was developed by using the standard C libraries can implement functions that take variable numbers of parameters by using a technique that is known as varargs. Only the code that calls these functions knows for certain what it has placed on the stack, so it is the responsibility of the calling code to clean up the stack when the function completes. This is referred to as the Cdecl calling convention. It is the mechanism that unmanaged code that runs on Windows Embedded CE uses (you can use this as a point to select between Cdecl and StdCall nn your code).
 
What is StdCall?
Later implementations of the C libraries have removed the ability to provide variable numbers. In unmanaged code that uses these libraries, the responsibility for cleaning the stack lies with the called function, not the calling code. This is referred to as the StdCall
calling convention. It is the mechanism that unmanaged Win32 Windows API functions use (you can use this as a point to select between Cdecl and StdCall nn your code).
 
Example
Consider the following function (assuming we are using C):
 
int subtract(int a, int b)
{
    return a – b;
}
 
The call to this function will look like this:
 
int c = subtract(2, 3);
 
C calling convention (__cdecl)
We can declare a function to use the __cdecl convention like this:
 
int __cdecl subtract(int a, int b);
 
The main characteristics of __cdecl calling convention are:
1.Arguments are passed from right to left, and placed on the stack.
2.Stack cleanup is performed by the caller.
3.Function name is decorated by prefixing it with an underscore character ‘_’ .
 
 
Standard calling convention (__stdcall)
We can declare a function to use the __stdcall convention like this:
 
int __stdcall subtract(int a, int b);
 
The main characteristics of __stdcall calling convention are:
1.Arguments are passed from right to left, and placed on the stack.
2.Stack cleanup is performed by the called function.
3.Function name is decorated by prepending an underscore character and appending a ‘@’ character and the number of bytes of stack space required.
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: