calling conventions

Started by
5 comments, last by chollida1 18 years, 8 months ago
can you tell me when to use the various calling conventions (as shown by microsoft in their documentation as: __cdecl, __cdecl, and __fastcall). I am working on creating some COM objects and was curious which of these would be best for my COM objects. ~guyaton
~guyaton
Advertisement
You should use __stdcall for COM. If you look at all the standard COM methods (e.g. IUnknown) they're all declared with STDMETHODIMP which resolves down to __stdcall (among other things).
-Mike
what do they all do differently from each other?

~guyaton
~guyaton

Calling conventions work as follows:


__cdecl pushes parameters on the stack in reverse order (right to left) with the caller responsible for cleaning up the stack.

__stdcall pushes parameters on the stack in reverse order (right to left) with the callee responsible for cleaning up the stack.

__fastcall pushes parameters on the stack in reverse order (right to left) with the 2 left most parameters passed in registers. Callee is responsible for cleaning up the stack.

thiscall (which is not a keyword but an implicit calling method) pushes parameters onto the stack in reverse order (right to left) with the this parameter passed in ECX. Callee is responsible for cleaning up the stack.


For class members that accept a variable number of arguments the this pointer is pushed onto the stack instead of being passed in ECX and the call is implicitly impemented as a __cdecl type call. This is necessary since the callee has no explicit or implicit indication of the number or size of the parameters actually passed.

Each calling method also has it's own name decoration attributes in order to prevent interpretation of methods using an incorrect calling method. This differs depending on whether the code is compiled as C or C++.

More information can be found in the MSDN.
Quote:Original post by Helter Skelter

Calling conventions work as follows:


__cdecl pushes parameters on the stack in reverse order (right to left) with the caller responsible for cleaning up the stack.

__stdcall pushes parameters on the stack in reverse order (right to left) with the callee responsible for cleaning up the stack.

__fastcall pushes parameters on the stack in reverse order (right to left) with the 2 left most parameters passed in registers. Callee is responsible for cleaning up the stack.

thiscall (which is not a keyword but an implicit calling method) pushes parameters onto the stack in reverse order (right to left) with the this parameter passed in ECX. Callee is responsible for cleaning up the stack.


For class members that accept a variable number of arguments the this pointer is pushed onto the stack instead of being passed in ECX and the call is implicitly impemented as a __cdecl type call. This is necessary since the callee has no explicit or implicit indication of the number or size of the parameters actually passed.

Each calling method also has it's own name decoration attributes in order to prevent interpretation of methods using an incorrect calling method. This differs depending on whether the code is compiled as C or C++.

More information can be found in the MSDN.



It was my understanding taht you could only use __fastcall on methods with at most 2 parameters. Your post makes it sound like you can use it on mehtods with more than 2 parameters.

Is this what you inteded?? was I wrong in this assumption??

Cheers
Chris
CheersChris
__fastcall can be used on a function with more than two parameters.
Ahh never mind. MSDN says this on __fastcall

Argument-passing order The first two DWORD or smaller arguments are passed in ECX and EDX registers; all other arguments are passed right to left.

So essentially I was wrong:)

So should I mod myself up for answering my own question?? or mod down for not knowing the answer in the first place:)

Cheers
Chris
CheersChris

This topic is closed to new replies.

Advertisement