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::InterfaceSupportsErrorInfoshould returnS_OKonly for those interfaces on your class that actually useSetErrorInfoto return error information to the caller, and only those interfaces.For example, say you have a class that implements an interface you wrote called
IFoothat has aDoSomethingmethod. If someone else creates an instance of your class and callsIFoo::DoSomething, they are supposed to do the following ifDoSomethingreturns a failureHRESULT(paraphrasing from various MSDN pages, but I started from here: http://msdn.microsoft.com/en-us/library/ms221510.aspx):Call
QueryInterfaceon theIFoopointer to get theISupportErrorInfointerface for the object that is implementingIFooIf the called object doesn't implement
ISupportErrorInfo, then the caller will have to handle the error based on theHRESULTvalue, or pass it up the call stack.If the called object does implement
ISupportErrorInfo, then the caller is supposed toQueryInterfacefor aISupportErrorInfopointer and callISupportErrorInfo::InterfaceSupportsErrorInfo, passing in aREFIIDfor the interface that returned the error. In this case, theDoSomethingmethod of theIFoointerface returned an error, so you would passREFIID_IFoo(assuming it's defined) toInterfaceSupportsErrorInfo.If
InterfaceSupportsErrorInforeturnsS_OK, then the caller knows at this point that it can retrieve more detailed information about the error by callingGetErrorInfo. IfInterfaceSupportsErrorInforeturnsS_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
SetErrorInfoto 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 normalHRESULTs to indicate errors.In summary, the
ISupportErrorInfointerface is a way to inform the calling code that at least one of the interfaces your class implements can return detailed error information, and theInterfaceSupportsErrorInfomethod 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