Navigation:  Tutorial >

Calling conventions

Previous pageReturn to chapter overviewNext page

A calling convention is a set of rules describing how to make a call to a function, the most important aspect of a calling convention are:

 

The order in which parameters are allocated.
Where to place the parameters
Which registers may be used by the function
Who is the responsible to unwind the stack on return
How to retrieve the result.

 

Each calling convention have also a different standard way to mangle the function names.

 

In windows programming the most used calling conventions for DLL exported functions are STDCALL and  CDECL.

 

The rest of the calling conventions may have differences across different compilers, so usually will not be used for DLL exported functions.

 

We will see some details on most commonly used called conventions in windows programming, but the most important here is to know the importance  of use the apropiate calling convention in each case. When we will try to use a function exported by a DLL we must seek in the package documentation  which calling convention must be used.

 

Using the wrong calling convention using DllCall() will result in a stack disaster, fortunatelly ot4xb DLL calling funcions and commands are detecting the calling convention on the fly without loose performance so using ot4xb DLL functions and commands you don't need to worry about the calling convention.

 

 

Xbase++  functions are internally managed as CDECL functions with one parameter that it's a 32 bit pointer to a internal structure that holds the info for the Xbase++ provided parameters. Xbase++ functions names are translated to uppercase.

 

This windows calling conventions have some common rules:

 

oParams are pushed onto the stack from right to left
oAll arguments are widened to 32 bits when they are passed
oReturn values are also widened to 32 bits and returned in the EAX register, except for 8-byte structures, which are returned in the EDX:EAX register pair. Larger structures are returned in the EAX register as pointers to hidden return structures.

 

STDCALL

oCalled function will restore the stack before return
oDefault Name-mangling: An underscore is prefixed to the name. The name is followed by the at sign (@) followed by the size in bytes of the parameters.
oFunction names are case sensitive.

 

CDECL

oCalling function must restore the stack after the called function return.
oDefault Name-mangling: An underscore is prefixed to the name
oFunction names are case sensitive.

 

The most WinAPI  DLL functions usually follows the STDCALL calling convention and are exported without the name mangling.