Jump to content
  • Advertisement
Sign in to follow this  
guyaton

calling conventions

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

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

Share this post


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

Share this post


Link to post
Share on other sites

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.

Share this post


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

Share this post


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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!