Marshal gethrforexception


Marshal gethrforexception


I have some IO code that reads a stream within a try.catch. It catches IOException and calls System.Runtime.InteropServices.Marshal.GetHRForException()within the catch, in an attempt to take different actions based on the HResult. The System.Exception.HResult property is protected. It turns out this API is actually poorly named, and it actually does more than just retrieving marshal gethrforexception HR from the exception object.

Every thread has a assoicated Marshal gethrforexception 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. June 13, 2003 at 8:14 pmThe rGetHRForException also sets up an IErrorInfo interface for the exception that can be obtained by calling the COM GetErrorInfoNEEDGUID function.

You can use this function to return an HRESULT value on a managed class implementation of a COM interface where you apply the PreserveSigAttribute attribute. Have the attributed method catch all exceptions and use the GetHRForException method to return the appropriate HRESULT value. Allowing an exception to propagate outside the method produces incorrect behavior. (In fact, the common language runtime fails to pass an exception to a COM client that calls such a method through a v-table.)Note that the GetHRForException method sets the IErrorInfo interface of the current thread.

This can cause unexpected results for methods like the ThrowExceptionForHR methods that default to using the IErrorInfo of the current thread if it is set. marshal gethrforexception GetHRForException also sets up an IErrorInfo interface for the exception that can be obtained by calling the GetErrorInfo COM API method. You can use this method to return an HRESULT value on a managed class implemention of a COM interface where you apply the System.Runtime.InteropServices.PreserveSigAttribute.

Have the attributed method catch all exceptions and use GetHRForException to return the appropriate HRESULT value.




Marshal gethrforexception

Marshal gethrforexception

Marshal gethrforexception



Add a comment

Your e-mail will not be published. Required fields are marked *