What is the meaning and usage of __stdcall?
c++, calling-convention, windows
Solution
All functions in C/C++ have a particular calling convention. The point of a calling convention is to establish how data is passed between the caller and callee and who is responsible for operations such as cleaning out the call stack.
The most popular calling conventions on windows are
- `__stdcall`, Pushes parameters on the stack, in reverse order (right to left)
- `__cdecl`, Pushes parameters on the stack, in reverse order (right to left)
- `__clrcall`, Load parameters onto CLR expression stack in order (left to right).
- `__fastcall`, Stored in registers, then pushed on stack
- `__thiscall`, Pushed on stack; this pointer stored in ECX
Adding this specifier to the function declaration essentially tells the compiler that you want this particular function to have this particular calling convention.
The calling conventions are documented here
- https://learn.microsoft.com/en-us/cpp/cpp/calling-conventions
Raymond Chen also did a long series on the history of the various calling conventions (5 parts) starting here.
- https://devblogs.microsoft.com/oldnewthing/20040102-00/?p=41213
Problem
I've come across `__stdcall` a lot these days. MSDN doesn't explain very clearly what it really means, when and why should it be used, if at all. I would appreciate if someone would provide an explanation, preferably with an example or two.