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:
| o | Params are pushed onto the stack from right to left |
| o | All arguments are widened to 32 bits when they are passed |
| o | Return 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
| o | Called function will restore the stack before return |
| o | Default 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. |
| o | Function names are case sensitive. |
CDECL
| o | Calling function must restore the stack after the called function return. |
| o | Default Name-mangling: An underscore is prefixed to the name |
| o | Function names are case sensitive. |
The most WinAPI DLL functions usually follows the STDCALL calling convention and are exported without the name mangling.