What does the ISupportErrorInfo
interface mean? I'm at a bit of a loss to understand it. From MSDN:
This interface ensures that error information can be propagated up the call chain correctly. Automation objects that use the error handling interfaces must implement ISupportErrorInfo.
This method indicates whether or not an interface supports the IErrorInfo interface.
HRESULT InterfaceSupportsErrorInfo(
REFIID riid
);
What does it mean to return S_OK in InterfaceSupportsErrorInfo
? Should you return S_OK for all interfaces? Just some?
-
My understanding of it (based on some related MSDN pages) is that by implementing
ISupportErrorInfo
, you are indicating that one or more interfaces on your class returns error information by callingSetErrorInfo
, as opposed to just returning a failureHRESULT
.To that end, your implementation of
ISuportErrorInfo::InterfaceSupportsErrorInfo
should returnS_OK
only for those interfaces on your class that actually useSetErrorInfo
to return error information to the caller, and only those interfaces.For example, say you have a class that implements an interface you wrote called
IFoo
that has aDoSomething
method. If someone else creates an instance of your class and callsIFoo::DoSomething
, they are supposed to do the following ifDoSomething
returns a failureHRESULT
(paraphrasing from various MSDN pages, but I started from here: http://msdn.microsoft.com/en-us/library/ms221510.aspx):Call
QueryInterface
on theIFoo
pointer to get theISupportErrorInfo
interface for the object that is implementingIFoo
If the called object doesn't implement
ISupportErrorInfo
, then the caller will have to handle the error based on theHRESULT
value, or pass it up the call stack.If the called object does implement
ISupportErrorInfo
, then the caller is supposed toQueryInterface
for aISupportErrorInfo
pointer and callISupportErrorInfo::InterfaceSupportsErrorInfo
, passing in aREFIID
for the interface that returned the error. In this case, theDoSomething
method of theIFoo
interface returned an error, so you would passREFIID_IFoo
(assuming it's defined) toInterfaceSupportsErrorInfo
.If
InterfaceSupportsErrorInfo
returnsS_OK
, then the caller knows at this point that it can retrieve more detailed information about the error by callingGetErrorInfo
. IfInterfaceSupportsErrorInfo
returnsS_FALSE
, the caller can assume the called interface doesn't supply detailed error information, and will have to rely on the returned HRESULT to figure out what happened.
The reason for this somewhat confusing/convoluted error-handling API seems to be for flexibility (as far I as I can tell anyway. This is COM after all ;). With this design, a class can support multiple interfaces, but not every interface is required to use
SetErrorInfo
to return error information from its methods. You can have certain, select interfaces on your class return detailed error information viaSetErrorInfo
, while other interfaces can continue to use normalHRESULT
s to indicate errors.In summary, the
ISupportErrorInfo
interface is a way to inform the calling code that at least one of the interfaces your class implements can return detailed error information, and theInterfaceSupportsErrorInfo
method tells the caller whether a given interface is one of those interfaces. If so, then the caller can retrieve the detailed error information by callingGetErrorInfo
.From Mike Spross
0 comments:
Post a Comment