Jump to content
  • Advertisement
Sign in to follow this  
eben

Strange modifiers like "__stdcall"

This topic is 4887 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

There are an abundance of theses function modifiers in Windows code. How many odd modifiers like this are there? Should I bother learning them, or is it bad style to use them in your own code?

Share this post


Link to post
Share on other sites
Advertisement
__stdcall is a Visual-C++-specific keyword which specifies the calling convention of a function. It relates to the use of callbacks, and to run-time DLL bindings. When you need to use it, you'll know.

Share this post


Link to post
Share on other sites
Calling convention is one example of these modifiers, which relates to how the stack is setup and cleaned up before and after a function call respectively. This is important for particular functions so bad style it is not.

Share this post


Link to post
Share on other sites
__stdcall is the C style calling convention, in C++ it is __cdecl, those names differ from compiler to compiler.
If you're using the winapi you should use the defines, for Callback functions CALLBACK and for the WinMain WINAPI.
Currently all of them use __stdcall, but that could change in the future, so it's the best way to use the right one.

You don't have to write those calling conventions to every function, only when it's needed, for calling from outside (like WinMain, or callback functions).

In standard c++ you can write >>extern "C" void myFunction();<< that makes this function as a C-style function, that means it uses the C calling convention instead of the C++ calling convention.

Another calling convention is __fastcall, this calling convention passes the first two parameters in a register (thats much faster than passing arguments with the stack).

For C++ classes another calling convention is used: __thiscall


In general you don't have to deal with them, only if you have to communicate with code that uses other calling conventions.

Share this post


Link to post
Share on other sites
correction: __stdcall is not the C calling convention even under MSVC it's still __cdecl, under __stdcall the callee clears the stack making vararg functions impossible that's the beatuy of __cdecl since the caller clears the stack.

extern "C" affects name decoration and in practice nothing else although that's quite important when linking to or creating libraries callable from C code.

But as pointed out earlier when you need to use them you'll know.

Share this post


Link to post
Share on other sites
It's worth noting that since calling a function with the __cdecl calling convention means that the caller cleans up the stack, if you use __cdecl as the default calling convention for a project, you tend to get a slightly larger executable than if you used __stdcall as the default calling convention. Largely this doesn't matter much in release builds since inlining usually makes calling conventions moot. However, if you want to apply this program transformation to see if there is any difference, you need to do so manually, since an optimizer will not change calling conventions as it modifies semantics.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!