Translate HRESULT to a readable message
Can anyone provide some information on how to programmatically translate HRESULT (http://en.wikipedia.org/wi开发者_开发百科ki/HRESULT) values to a human-readable message?
I know that the utility ERR.EXE does this, but my app needs to display some meaningful message to my users rather than a cryptic 0x800A03EC.
There's no built-in support for generating messages from an HRESULT with that value. The COM component needs to implement that itself, using the IErrorInfo interface.
The CLR interop support built into .NET already does that automatically, you should never need to help. It automatically throws an appropriate exception when it sees a COM interface method return a failure code. The Message property is initialized from IErrorInfo. If you get nothing decent as a message then the COM server just doesn't supply it.
Which is likely for 0x800a03ec, it is a grab-bag low-level Excel error with many error causes. You can find plenty of hits for it by googling "excel error 1004".
Have you tried FormatMessage function? You can use it from C# using PInvoke mechanism.
精彩评论