Re: calling convention stdcalll and cdecl call
* Igor Tandetnik:
"George" <George@discussions.microsoft.com> wrote in message
Both stdcall and cdecl calling convention could support variable input
parameters? Is that correct?
No. Variadic functions must use cdecl calling convention.
(I think stdcall is using RET N and cdecl is using ESP - N, so both
are capable to handle variable number of input parameter, like
I have no idea what "RET N" or "ESP - N" is supposed to mean.
With stdcall, the function is responsible for removing its parameters
from the stack. To do this, it must know how many parameters there are,
and thus cannot take variable number of parameters.
I'm sorry, that's incorrect (counter-example below).
It's only a tool and library limitation.
With cdecl, the caller is responsible for removing parameters after the
called function returns. The caller of course knows how many parameters
it has just pushed on the stack, so there's no problem removing them.
I now see that I remembered incorrectly about pushing order in my reply earlier
in this thread. Happily, as I recall I wrote a disclaimer "as I recall". ;-)
extern "C" void __stdcall display( int x )
std::cout << x << std::endl;
void _stdcall knurre( int );
void _stdcall knurre( int, int );
void _stdcall knurre( int, int, int );
void _stdcall knurre( int, int, int, int );
knurre( 1, 101 );
knurre( 2, 201, 202 );
knurre( 3, 301, 302, 303 );
display EQU _display@4
PUBLIC ?knurre@@YGXH@Z ; (int)
PUBLIC ?knurre@@YGXHH@Z ; (int, int)
PUBLIC ?knurre@@YGXHHH@Z ; (int, int, int)
PUBLIC ?knurre@@YGXHHHH@Z ; (int, int, int, int)
_knurre PROC NEAR
mov ebp, esp
mov ecx, [esp+8]
and ecx, ecx
mov eax, [esp+4*ecx+8]
mov eax, 1234
mov esp, ebp
pop ebx ; Return address
shl ecx, 2
add esp, ecx
As you can see at the assembly language level (apologies for novice style code,
it's been a long time!) there's no problem. The main problem with the above was
to get Visual C++ to accept the stdcall vararg routine, not changing the calling
convention to cdecl, which it does when it sees "...". For that, had to declare
the function with all actually used signatures -- so it's just a tool problem,
and for that matter, also library problem, that there's no support.
Cheers, & hth.,
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
Q: What is the most annoying thing on usenet and in e-mail?