Ask Learn
Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Let's first start by looking at a small code snippet:
if (Marshal.GetHRForException(myException) == E_SOME_COM_ERROR)
DoSomething();
This looks perfectly fine, right? Not really. It turns out this API is actually poorly named, and it actually does more than just retrieving the HR from the exception object. Using this API incorrectly could give you some weird problems, such as throwing out an incorrect, old exception.
So what does the API actually do?
Besides returning you the HRESULT, this function also sets the current thread IErrorInfo object to be the exception object. Every thread has a assoicated IErrorInfo COM object, defaulting to NULL. This IErrorInfo object represents the failure from the last COM API call, which is sort of like the GetLastError() Win32 API, or errno C api. From the IErrorInfo object, you can get more information such as the error description, help file context, etc, that gives you more information about the error. By setting IErrorInfo object to the exception object (actually setting to the IErrorInfo implementation of the exception object, which is shared by all managed objects), you are essentially telling your COM callers, "hey, there is a failure, it's this exception object, and you should do something about it", which is obviously a bad idea if you only want to retrieve the HR.
In many cases, this could go unnoticed, if your COM caller/callee handles IErrorInfo correctly. By "correctly", I mean they follow the COM IErrorInfo protocol, which are basically:
1. The caller use IErrorInfo from the current thread when calling a COM interface that explicily says "I support IErrorInfo"
2. The callee (which implements the COM interface) that says "I support IErrorInfo", must clear/set IErrorInfo before returning. This is very similar to GetLastError()/errno, right?
If this is the case, the incorrectly set IErrorInfo in the thread, would typically either be overwritten/cleared by some interface implementation that supports IErrorInfo, or ignored.
However, because the protocol I described is actually not very well understood by every COM developer, I've often seen cases where the COM component just say "sure, I support IErrorInfo on every COM interface", and they don't set/clear IErrorInfo at all. And then, the caller of this poorly implmented COM interface, would end up using the IErrorInfo set by GetHRForException call earlier, and is lead to believe that there is an error. For example, let's consider the following scenario:
Now, you might be wondering: What is this API really intended for in the first place? It is actually used in places in COM interop, where you want to return the HRESULT and set IErrorInfo by yourself. This doesn't happen very often - most people would be happy enough to let COM interop takes care of that part by converting Exception to HR and IErrorInfo (which, happens to call Marshal.GetHRForException). So unless you really know what you are doing, my advice is to stay away from Marshal.GetHRForException.
What if you really, really want to just get the HRESULT for a specific Exception? You should use Exception.HResult property.
Please sign in to use this experience.
Sign in