What is __stdcall?

Started by
6 comments, last by Taralieth 19 years ago
I have a quick question for you guys. Like the topic says, what is this __stdcall? I believe that WINAPI and CALLBACK are also just defines for __stdcall. And microsoft seems to use this a lot. One example where its used is in the directx sample, CustumUI. For every callback function that they define, they use this CALLBACK keyword which is just a define for __stdcall. Same with the WinMain. It also has the keyword WINAPI which when I checked for the definition, it turns out to also be a define for __stdcall. So what does this __stdcall do?
Advertisement
It's a calling convention telling the compiler how the function should be called.
It specifies how arguments are passed (in this case right to left on the stack with the callee clearing the stack).

HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Linky from MSDN
But what is the purpose of using it? It seems rather pointless to me. Is there some advantage of passing the arguments from right to left? I'm assuming that it normally passes the argument left to right.
well the sneaky part is that the standard c-convention makes the caller clear the stack instead of the callee so there's a shift of responsibility there.

And the functions using the supplied callback are coded to assume that they use the stdcall convention.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Quote:Original post by Taralieth
But what is the purpose of using it?


If you look at that MSDN link, on the left panel with the other calling conventions, you will see some more reasons. For example:
Quote:__cdecl - This is the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions. The __cdecl calling convention creates larger executables than __stdcall, because it requires each function call to include stack cleanup code. The following list shows the implementation of this calling convention.


Also if you look at the Name-decoration convention, if you needed to do something with the name-decoration (how the compiler generates function names in the executable), then you could base it on that as well.

It's most of a concept of knowing that you need to change it for some specific reason. Everyday use does not really call (pun not intended) for changing this.
Quote:Original post by Taralieth
But what is the purpose of using it? It seems rather pointless to me. Is there some advantage of passing the arguments from right to left? I'm assuming that it normally passes the argument left to right.

Different languages use different calling convention, and if you call a function using an incorrect calling convention, everything breaks. Hard. Not only can arguments get assigned incorrectly, but the stack may fall apart. Either it'll get cleaned twice [resulting in losing memory you think is there], or it won't get cleaned at all [resulting in leaking memory]. Either way, thing'll stop working right away even though an explicit error might not be raised.

So it is pretty important that you have a way of specifying how you want functions called if using an external library. In this case, the windows API expects __stdcall, while c++ does not. So specifying a c++ function as using __stdcall ensures that when the compiler generates code for that call, it can interface with the API without problems.

CM
alright. Thanks for clearing that up.

This topic is closed to new replies.

Advertisement