COM physical return value

All COM/DCOM functions written in IDL should return a result code as raw result value. This return is of type HRESULT. A HRESULT code of S_OK/zero means that the function returns a success. This is however not the actual return value of the function. This return value is called physical or raw return value. This is used to know if the fuction has encountered any error. This return value is not considered as actual return in client. Client can check error condition and process error handing. Below are some of very common result codes returned by IDL functions.

S_OKOperation successful0x00000000
E_ABORTOperation aborted0x80004004
E_ACCESSDENIEDGeneral access denied error0x80070005
E_FAILUnspecified failure0x80004005
E_HANDLEHandle that is not valid0x80070006
E_INVALIDARGOne or more arguments are not valid0x80070057
E_NOINTERFACENo such interface supported0x80004002
E_NOTIMPLNot implemented0x80004001
E_OUTOFMEMORYFailed to allocate necessary memory0x8007000E
E_POINTERPointer that is not valid0x80004003
E_UNEXPECTEDUnexpected failure0x8000FFFF

IDL functions and HRESULT

  interface ICalculate : IDispatch
    [propget, id(1), helpstring("Operand1 of calculator")]
    HRESULT Operand1([out, retval] long *pVal);
    [propput, id(1), helpstring("Operand1 of calculator")]
    HRESULT Operand1([in] long newVal);
    [propget, id(2), helpstring("Operand2 of calculator")]
    HRESULT Operand2([out, retval] long *pVal);
    [propput, id(2), helpstring("Operand2 of calculator")]
    HRESULT Operand2([in] long newVal);
    [id(3), helpstring("calculator main Operation method")]
    HRESULT DoOperation([in] char operation);
    [propget, id(4), helpstring("Result of calculator")]
    HRESULT Result([out, retval] long *pVal);

C++ functions and HRESULT

    virtual HRESULT __stdcall get_Operand1 (long * pVal ) = 0;
    virtual HRESULT __stdcall put_Operand1 (long pVal ) = 0;
    virtual HRESULT __stdcall get_Operand2 (long * pVal ) = 0;
    virtual HRESULT __stdcall put_Operand2 (long pVal ) = 0;
    virtual HRESULT __stdcall raw_DoOperation (char operation ) = 0;
    virtual HRESULT __stdcall get_Result (long * pVal ) = 0;

COM logical return value

COM function can optionally have a return value to the client. Return arguments are marked as [out,retval] in IDL. A function with void return type will not have any [out,retval]. These return arguments are called logical return to client. We have equivalent wrapped functions as below and we see the logical returns values of each "GET" functions. "PUT" functions being a void return will never have any logical return. Client should check the HRESULT code for S_OK / success before processing the logical return value.

Wrapped fucntions

long GetOperand1 ( );
void PutOperand1 (long pVal);
long GetOperand2 ( );
void PutOperand2 (long pVal);
HRESULT DoOperation (char operation);
long GetResult ( );

HRESULT and error handling

inline long ICalculate::GetOperand1 ( ) {
    long _result;
    HRESULT _hr = get_Operand1(&_result);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    return _result;

inline void ICalculate::PutOperand1 ( long pVal ) {
    HRESULT _hr = put_Operand1(pVal);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
inline long ICalculate::GetResult ( ) {
    long _result;
    HRESULT _hr = get_Result(&_result);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    return _result;

About our authors: Team EQA

You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.

Learn on Youtube