Basic COM  «Prev  Next»
Lesson 5 COM method properties
Objective List the binary requirements of a COM method.

COM Method Properties and Binary Requirements

COM is a binary standard. This means that at its lowest level, COM methods and interfaces must follow a prescribed in-memory layout. COM also constrains the return type of a COM method. The following list summarizes the properties of a COM method.

Calling Conventions

COM methods are called using a hybrid of C and PASCAL calling conventions. In Visual C++ 5.0/6.0, the __stdcall attribute can be placed in front of a function to set up this calling convention.

COM's Calling Conventions

COM methods are called using C/C++ calling conventions. This means function parameters are pushed on to the stack in right-to-left order.
For example, the last parameter is pushed first. The caller must clean up the stack after the function call returns.

Return Types

COM methods return type HRESULT. An HRESULT is a 32-bit value that indicates the success or failure of a COM call. Core COM methods define specific error codes as HRESULTs. In general, COM methods do not return application-specific error codes as return values.
Two standard HRESULT return values are S_OK for no error and E_FAIL for a generic error. Two macros are defined to help process error codes: FAILED and SUCCEEDED. Each takes in an HRESULT return value and returns zero or non-zero. For example, passing in an HRESULT return value that indicates an error condition to FAILED will return non-zero, meaning the COM method call returned an error.
Two widely used COM methods, AddRef and Release of IUnknown, return unsigned long, or ULONG. This works because a ULONG is the same size as an HRESULT.

First parameter

The first parameter passed to a COM method must be a pointer to the COM interface containing the method being called. We will discuss this in more detail in the following lesson. For example, COM interface IMyComInterface contains methods Fx1 and Fx2:

interface IMyComInterface {
HRESULT __stdcall Fx1(CHAR *buf);
HRESULT __stdcall Fx2();

COM methods also have another requirement, they must be in a Vtable. The following lessons will discuss this.

COM Client-Server Interaction - Quiz

Click the Quiz link below to check your understanding of COM binary requirements.
COM Client-Server Interaction - Quiz

Essential COM