views:

917

answers:

6

I come across _stdcall a lot these days. And in my opinion 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 2.

Thanks

+1  A: 

That's a calling convention that WinAPI functions need to be called properly. A calling convention is a set of rules on how the parameters are passed into the function and how the return value is passed from the function.

If the caller and the called code use different conventions you run into undefined behaviour (like such a strange-looking crash).

C++ compilers don't use __stdcall by default - they use other conventions. So in order to call WinAPI functions from C++ you need to specify that they use __stdcall - this is usually done in Windoes SDK header files and you also do it when declaring function pointers.

sharptooth
+2  A: 

It specifies a calling convention for a function. A calling convention is a set of rules how parameters are passed to a function: in which order, per address or per copy, who is to clean up the parameters (caller or callee) etc.

sbi
+13  A: 

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
  • cdecl
  • clrcall
  • fastcall
  • thiscall

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

Raymond Chen also did a long series on the history of the various calling conventions (5 parts) starting here.

JaredPar
+2  A: 

Unfortunately, there is no easy answer for when to use it and when not.

__stdcall means that the arguments to a function are pushed onto the stack from the first to the last. This is as opposed to __cdecl, which means that the arguments are pushed from last to first, and __fastcall, which places the first four (I think) arguments in registers, and the rest go on the stack.

You just need to know what the callee expects, or if you are writing a library, what your callers are likely expect, and make sure you document your chosen convention.

Jonathan
+4  A: 

__stdcall is a calling convention: a way of determining how parameters are passed to a function (on the stack or in registers) and who is responsible for cleaning up after the function returns (the caller or the callee).

Raymond Chen wrote a blog about the major x86 calling conventions, and there's a nice CodeProject article too.

For the most part, you shouldn't have to worry about them. The only case in which you should is if you're calling a library function that uses something other than the default -- otherwise the compiler will generate the wrong code and your program will probably crash.

Nick Meyer
+1  A: 

__stdcall denotes a calling convention (see this PDF for some details). This means it specifies how function arguments are pushed and popped from the stack, and who is responsible.

__stdcall is just one of several calling conventions, and is used throughout the WINAPI. You must use it if you provide function pointers as callbacks for some of those functions. In general, you do not need to denote any specific calling convention in your code, but just use the compiler's default, except for the case noted above (providing callbacks to 3rd party code).

gimpf