Error Handling
Almost all COM functions return an HRESULT. If it’s other than S_OK, the question then becomes, how to handle it.
In the Win32, a system may return ’error’ (see docs). If you want additional info, call GetLastError to get a system error code, and then call FormatMessage
The COM equivalent is similar, but COM-based. (Things are a little different over IDispatch).
Server
The server implements ISupportErrorInfo to let clients know that it supports error info for a given interface. Then it uses ICreateErrorInfo to create a new IErrorInfo object. The it calls the Win32 SetErrorInfo.
Client side
The client calls ISupportErrorInfo::InterfaceSupportsErrorInfo to see if the server supports it, then calls GetErrorInfo.
In practice, the _com_error class wraps this. Here’s the pattern:
References
- GetLastError function (errhandlingapi.h) - Win32 apps | Microsoft Learn
- FormatMessage function (winbase.h) - Win32 apps | Microsoft Learn
- ISupportErrorInfo (oaidl.h) - Win32 apps | Microsoft Learn
- ICreateErrorInfo (oaidl.h) - Win32 apps | Microsoft Learn
- IErrorInfo (oaidl.h) - Win32 apps | Microsoft Learn
- SetErrorInfo function (oleauto.h) - Win32 apps | Microsoft Learn
- GetErrorInfo function (oleauto.h) - Win32 apps | Microsoft Learn
_com_errorClass | Microsoft Learn- Error Handling in COM (Get Started with Win32 and C++) - Win32 apps | Microsoft Learn
- Handling Errors in COM+ - Win32 apps | Microsoft Learn