In previous articles, Jun Nakamura introduced using regular function pointers, C++ class member function pointers, and declaring pointers to the members of classes. In this article, he writes about calling conventions, callback functions, and begins to talk about using functors.
Function Pointers, part 3 - Windows Calling Conventions (Page 2 of 4 )
Alright, here is an overview of how different some Windows calling conventions can be (__pascal, __fortran and __syscall are no longer supported):
__cdecl Argument Passing: right to left Stack Maintenance: Calling function pops arguments from the stack Name Decoration (C only): _ prefixed to function names (e.g. _foo)
__stdcall Argument Passing: right to left Stack Maintenance: Called function pops its own arguments from the stack Name Decoration (C only): _ prefixed to function name, @ appended followed by the number of decimal bytes in the argument list. (e.g. _foo@10)
__fastcall (applies to Intel CPUs, this is the default calling convention for Borland) Argument Passing: First two DWORD arguments are passed in ECX and EDX, the rest is passed right to left Stack Maintenance: Called function pops its own arguments from the stack Name Decoration (C only): @ is prefixed to the name, @ appended followed by the number decimal bytes in the argument list. (e.g. @foo@10)
thiscall (used automatically by C++ code) Argument Passing: this pointer put in ECX, arguments passed right to left Stack Maintenance: Calling function pops arguments from the stack Name Decoration: None
You see that there are many ways leading to Rome, and this is just on a Windows machine. What is interesting for this article though, is how we declare a function pointer to the following function definition:
The principle remains the same whether it is a member function, a function argument, etc. Just bolt what you need into the declaration.
When your linker is giving you errors that it cannot resolve external symbols while you know that you are linking with the correct libraries; be sure to check which calling conventions are being used.