What is the meaning and use of __stdcall?

These days I met __stdcall .
And, in my opinion, MSDN does not explain very clearly what this really means when and why it should be used, if at all.

I would appreciate someone providing an explanation, preferably with an example or two.

thank

+70
c ++ windows calling-convention
Aug 20 '09 at 14:03
source share
9 answers

All functions in C / C ++ have a special calling convention. The point of the calling agreement is to establish how data is transferred between the caller and the callee, and who is responsible for operations such as clearing the call stack.

Most popular window calling conventions

  • STDCALL
  • Cdecl
  • clrcall
  • bastard
  • thiscall

Adding this qualifier to a function declaration essentially tells the compiler that you want this particular function to have this particular calling convention.

Call conventions are described here.

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

+60
Aug 20 '09 at 14:07
source share

Traditionally, C-function calls are made with a call that calls some parameters on the stack, a function call, and then dropping the stack to clear these pressed arguments.

 /* example of __cdecl */ push arg1 push arg2 push arg3 call function add sp,12 // effectively "pop; pop; pop" 

Note. The default convention shown above is called __cdecl.

The other most popular convention is __stdcall. In it, the parameters are pressed again by the caller, but the stack is cleared by the called user. This is the standard convention for Win32 API functions (as defined by the WINAPI macro), and is also sometimes called the "Pascal" calling convention.

 /* example of __stdcall */ push arg1 push arg2 push arg3 call function // no stack cleanup - callee does this 

This looks like a minor technical detail, but if there is disagreement about how the stack is managed between the caller and the callee, the stack will be destroyed in a way that is unlikely to be restored. Since __stdcall performs a stack cleanup, the (very small) code to complete this task is only in one place, and is not duplicated in each calling device, as in __cdecl. This makes the code very slightly smaller, although size effects are only visible in large programs.

Variadic functions, such as printf (), are almost impossible to get with __stdcall, because only the caller really knows how many arguments were passed to clear them. A call can make some good guesses (say, looking at a format string), but clearing the stack should be determined by the actual logic of the function, and not by the call-convention mechanism itself. Therefore, only __cdecl supports variational functions so that the caller can perform the cleanup.

Linker conventions: As mentioned in the paragraph above, calling a function with an "incorrect" convention can be disastrous, so Microsoft has a mechanism to avoid this. It works well, although it can be insane if you don't know what the reason is. They decided to resolve this by encoding the calling convention into low-level function names with extra characters (often called "decorations"), and they are treated as unrelated names by the linker. The standard call is __cdecl, but can anyone be requested explicitly with / G? parameter to the compiler.

__ cdecl (cl / Gd ...)

All function names of this type have an underscore prefix, and the number of parameters does not matter much, since the caller is responsible for installing the stack and clearing the stack. It is possible for the caller and the callee to cause confusion over the number of parameters actually transferred, but at least the stack discipline is properly maintained.

__ stdcall (cl / Gz ...)

These function names have an underscore prefix and are added along with @ plus the number of bytes of the parameters passed. By this mechanism, it is impossible to call a function with the β€œwrong” type or even with the wrong number of parameters.

__ fastcall (cl / Gr ...)

These function names begin with the @ sign and suffixes with the @parameter parameter, like __stdcall.

Examples:

 Declaration -----------------------> decorated name void __cdecl foo(void); -----------------------> _foo void __cdecl foo(int a); -----------------------> _foo void __cdecl foo(int a, int b); -----------------------> _foo void __stdcall foo(void); -----------------------> _foo@0 void __stdcall foo(int a); -----------------------> _foo@4 void __stdcall foo(int a, int b); -----------------------> _foo@8 void __fastcall foo(void); -----------------------> @foo@0 void __fastcall foo(int a); -----------------------> @foo@4 void __fastcall foo(int a, int b); -----------------------> @foo@8 
+61
30 oct. '14 at 11:08
source share

__ 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 (caller or callee).

Raymond Chen has written a blog about basic x86 calling conventions , and there is a good CodeProject there .

For the most part, you do not have to worry about them. The only time you need to is to call a library function that uses something other than the default value - otherwise the compiler will generate the wrong code, and your program will probably crash.

+7
Aug 20 '09 at 14:08
source share

Unfortunately, there is no simple answer to the question of when to use it, and when not.

__ stdcall means that function arguments are pushed onto the stack from the first to the last. This is in contrast to __cdecl, which means that the arguments are carried over from the last to the first and __fastcall, which pushes the first four (I think) arguments into registers and the rest onto the stack.

You just need to know what the interlocutor expects, or if you are writing a library, what your callers expect, and make sure that you document the agreement you have chosen.

+6
Aug 20 '09 at 14:08
source share

It defines a calling convention for a function. The calling convention is a set of rules on how parameters are passed to the function: in what order, to the address or to the copy, who should clear the parameters (calling or called), etc.

+3
Aug 20 '09 at 14:07
source share

__ stdcall stands for the calling convention (see this PDF for some details). This means that it indicates how function arguments are popped and pushed from the stack, and who is responsible.

__ stdcall is just one of several calling conventions and is used throughout WINAPI. You should use it if you provide function pointers as callbacks for some of these functions. In general, you do not need to specify any specific calling convention in your code, but just use the default compiler option, except as noted above (providing callbacks for third-party code).

+2
Aug 20 '09 at 14:09
source share

This is the calling convention that WinAPI functions must be called properly. The calling convention is a set of rules about how parameters are passed to the function and how the return value from the function is passed.

If the calling and called codes use different conventions, you start undefined behavior (for example, such a strange occurrence of a failure ).

C ++ compilers do not use __stdcall by default - they use different conventions. Therefore, to call WinAPI functions from C ++, you need to indicate that they use __stdcall - this is usually done in the Windoes SDK header files, and you also do this when declaring function pointers.

+1
Aug 20 '09 at 14:07
source share

just added when the function is called, it is loaded onto the stack / register. __stdcall is one convention / way (right argument, then left argument ...), __decl is another convention that is used to load a function onto the stack or registers.

If you use them, you instruct the computer to use this method of loading / unloading the function during the binding, and therefore you will not get a mismatch / crash.

Otherwise, the called function and the calling function may use different conventions that cause the program to crash.

+1
Nov 30 '11 at 10:07
source share

__ stdcall is the calling convention used for this function. This tells the compiler the rules that are used to configure the stack, push arguments, and get the return value. There are a number of other calling conventions such as __ cdecl , __ thiscall , __ fastcall, and __ naked .

__ stdcall is the standard calling convention for Win32 system calls.

More information can be found on Wikipedia .

+1
Dec 07 '17 at 6:16
source share



All Articles