Search code examples
c++com

Why are the methods in IUnknown class declared as __stdcall


I am new to COM. Why are the interface methods QueryInterface AddRef and Release are declared to have __stdcall and not any other call (__cdlecl, __thiscall, etc).

Is this to support calling form other languages?

I think this question is quite simple to those who known COM.


Solution

  • COM is a binary interface standard, allowing code written in one language to call functions written in another language. There have to be some minimum guarantees that such calls can come to a good end, languages have different standards for the way they implement their own function calls.

    There are too many calling conventions. There is __stdcall, __cdecl, __thiscall, __fastcall, __clrcall off the top of my head for ones that are common in 32-bit code. All different with different trade-offs between space, time, flexibility and safety. Language implementers tend to come up with their own, usually some variation of __fastcall.

    That won't do, the COM designers had to nail one to the wall to give code a shot to interop. They picked an obvious choice, doubtful that they spent a lot of time weighing the options, they used the calling convention that was also used to make operating system calls. Implicit in having a language runtime running on Windows is that it needs to know how to make OS calls. So they picked __stdcall.