Jump to content
  • Advertisement
Sign in to follow this  
Taralieth

What is __stdcall?

This topic is 4777 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!