1. Home
  2. Computing & Technology
  3. C / C++ / C#

LIst of Calling Conventions and Mangling Names in C++
Linking Safely

By , About.com Guide

List of Calling Conventions

The main ones are listed below. The parameters A,B and C referred to are in this function declaration :
function f(A, B,C)
  • cdecl This is the default for C and C++. Parameters are pushed from right to left, C then B then A. The called function gets the values of the parameters but doesn't alter the stack, so the compiler has to add an instruction after the call to increment the stack to make it all balance. There are variations between different platforms over register usage, and some languages e.g. Visual Basic cannot used cdecl. It does however support variadic functions such as printf().
  • stdcall This is probably the commonest, certainly if non C/C++ languages are involved. Unlike cdecl, the parameters are pushed from left to right ( a,b,c ) and the function itself must clean up the stack before returning.
  • winapi This is the default on Windows if none is specified. It defaults to stdcall on Windows and cdecl on Windows CE. It isn't the same as stdcall though so if you try to call a dll function marked winapi when expecting stdcall, Windows doesn't find it.
  • fastcall This uses registers for sped but it is implemented differently between different compilers so use it if you are sticking to compilers from one source and not being called from languages like Visual Basic or C#.
  • safecall Used by Borland for COM/OLE calling.
  • thiscall This is used in C# for calling functions in classes exported from unmanaged code.
Unless your dlls are only used by C or C++, I recommend you stick with winapi. This is the default, so doesn't actually need specifying!

Name Mangling in C++

Because C++ is a type safe language and fairly strict about types, it introduced name mangling. This means that the exported function includes extra characters derived from the type of parameters. This helped prevent functions being called with the incorrect number of parameters, something that can lead to mysterious crashes. However it can cause problems when non C++ code calls C++ functions. Eg say you have a C# application calling C+= code in a dll. With the functions names mangled in the dll, the functions can't be found. The operating system might be expecting a function called GetValue(int a, float b) but in the dll the name will be mangled and look something like GetValueif.

There's a simple fix. Any C++ dll must put this code before and after the declarations of any exported functions. You have something like this in the header file.

#define MYEXPORT __declspec(dllexport)

#ifdef __cplusplus
extern "C" {
#endif


MYEXPORT char * WINAPI GetBotName(void) ; // returns name of your Bot
...

#ifdef __cplusplus
}
#endif

The line
#define MYEXPORT __declspec(dllexport)
tells the compiler to add the __declspec bit to any function defined with MYEXPORT. This makes that function visible to any application that uses the dll.

The macro __cplusplus is only defined in Ansi 98 compatible C++ compilers so the macro is ignored in any header file that is included from a C file.

In the opposite case where you have C code that you wish to be linked from C++ then you wrap the declarations with a

extern "C++" {
around your declarations. This forces the compiler to mangle the function names. Full details of what exactly goes on in name mangling are here.

You can see examples of dll code for both C and C++ examples provided for the Ongoing Programming Challenge #1 (Rock Scissors and Paper). Both examples use the same header file.

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. Getting Started
  5. LIst of Calling Conventions and Mangling Names in C++>

©2009 About.com, a part of The New York Times Company.

All rights reserved.