Translate HRESULT to a readable message

2019-06-22 07:41发布

Can anyone provide some information on how to programmatically translate HRESULT (http://en.wikipedia.org/wiki/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.

标签: c# excel com vsto
2条回答
成全新的幸福
2楼-- · 2019-06-22 08:02

Have you tried FormatMessage function? You can use it from C# using PInvoke mechanism.

查看更多
在下西门庆
3楼-- · 2019-06-22 08:17

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".

查看更多
登录 后发表回答