I am trying to understand how to check for a COM error when I am using a C++ COM client. I realise that HRESULT can be checked but what confuses me is exposed methods that also specify a parameter of type [retval,out].

For a COM object that returns eg. a BSTR via [retval, out] the call would be as follows. And I don't see how can also get an HRESULT returned?
BSTR b = ComObject->Foo();

ie. how do I check for COM failure of the above line? Where is the HRESULT?

All the examples I see of implemented COM methods seem to ALWAYS return S_OK. As if the methods never fail?

Not all COM methods return an HRESULT although most do. For the ones that don't, the object documentation should tell you what to check for to detect an error condition. With a BSTR return, it should be NULL on error (since it's a pointer).

The COM object I worked with return HRESULT and the BSTR was a parameter

BSTR b = 0;
HRESULT hr = ComObject->Foo(&b);

But you have to check exposed function prototypes in the COM object's header file.

OK you can return BSTR as an [out] parameter and still get the HRESULT value returned too. But in that case what is the difference between [out] and [retval, out]?

BSTR outparam;
BSTR retparam;
HRESULT hr = ComObject->Foo(&outparam, &retparam);

In the above outparam in the idl is specified as [out] while retparam is specified as [retval, out].

My understanding is that this can be written in the COM client as:

BSTR retparam = ComObject->Foo(&outparam);

In which case we have no HRESULT?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.