locked
Handling COM errors RRS feed

  • Question

  • I found that I can use GetErrorInfo to retrieve an IErrorInfo interface pointer, but when I try to do it with IStorage, for example, it returns 0. Is it correct to use only CODE portion of HRESULT with FormatMessage(), or there's another way to retrieve IErrorInfo which can be used to display error information?

    If I fall I will arise on my way to paradise

    Tuesday, June 21, 2016 8:25 AM

Answers

All replies

  • IErrorInfo is not necessarily supported on every COM object.  See the retrieving error information flowchart at https://msdn.microsoft.com/en-us/library/windows/desktop/ms221026(v=vs.85).aspx
    • Proposed as answer by RLWA32 Tuesday, June 21, 2016 1:26 PM
    Tuesday, June 21, 2016 10:12 AM
  • Yep, I've read this. But where from should I call QueryInterface if a pointer returned for StgCreateStorageEx is 0, and HRESULT is FAILED? I want to make a unique handler of HRESULT errors, and IErrorInfo could help me, but only if it was necessarily supported.

    That topic didn't give me clear understanding of how to handle COM errors correctly =(


    If I fall I will arise on my way to paradise

    Tuesday, June 21, 2016 11:53 AM
  • Yep, I've read this. But where from should I call QueryInterface if a pointer returned for StgCreateStorageEx is 0, and HRESULT is FAILED?

    You misunderstand. In the above case, IErrorInfo is not relevant.  Just refer to the HRESULT.

    If you already have an interface pointer and a method called through that pointer fails then you can see if the interface through which you made the failed call supports IErrorInfo.

    • Proposed as answer by RLWA32 Tuesday, June 21, 2016 1:26 PM
    Tuesday, June 21, 2016 12:19 PM
  • So when I have no interface pointer (and it's obvious that I can't call QueryInterface on nullptr), I need to extract error code from HRESULT and call FormatMessage(). If I have an interface pointer, I need to check if it supports IErrorInfo, and if no, I have to check HRESULT again, right?

    If I fall I will arise on my way to paradise

    Tuesday, June 21, 2016 12:41 PM
  •  I need to extract error code from HRESULT and call FormatMessage().

    FormatMessage will likely not help you with HRESULT error values.  There is no mapping from HRESULT errors to Win32 system error codes (assuming you intend to pass the FORMAT_MESSAGE_FROM_SYSTEM flag).

    If I have an interface pointer, I need to check if it supports IErrorInfo, and if no, I have to check HRESULT again, right
    Yes to check for IErrorInfo support, but there shouldn't be a need to check the HRESULT twice.

    • Proposed as answer by RLWA32 Tuesday, June 21, 2016 1:26 PM
    Tuesday, June 21, 2016 1:09 PM
  • Then what should I do if there is no IErrorInfo support, and I need a textual description? IErrorInfo or FormatMessage can provide system-language error description, but when dealing with manual HRESULT handling I'll need to store all descriptions in my program without any simple ability to extract them.

    I still cannot understand =(


    If I fall I will arise on my way to paradise

    Tuesday, June 21, 2016 1:13 PM
  • Then what should I do if there is no IErrorInfo support, and I need a textual description? IErrorInfo or FormatMessage can provide system-language error description, but when dealing with manual HRESULT handling I'll need to store all descriptions in my program without any simple ability to extract them.

    I still cannot understand =(

    You understand quite well but are not getting the answer that you desire.  There is no guaranteed mechanism to convert COM errors into text.

    • Edited by RLWA32 Tuesday, June 21, 2016 1:17 PM
    Tuesday, June 21, 2016 1:17 PM
  • So I have only 3 ways:

    0. Use FormatMessage() where possible (probably only Win32 errors)
    1. Check for IErrorInfo support and use it if possible
    2. Manually generate error messages based on HRESULT (e.g. for hr==STG_E_MEDIUMFULL I should write "There's no space left on a hard disk", for STG_E_FILENOTFOUND — "File wasn't found")

    Is it correct?


    If I fall I will arise on my way to paradise

    Tuesday, June 21, 2016 1:22 PM
  • So I have only 3 ways:

    0. Use FormatMessage() where possible (probably only Win32 errors)
    1. Check for IErrorInfo support and use it if possible
    2. Manually generate error messages based on HRESULT

    Yes.

    For more information about this topic read https://blogs.msdn.microsoft.com/oldnewthing/20061103-07/?p=29133/

    • Proposed as answer by RLWA32 Tuesday, June 21, 2016 1:25 PM
    • Marked as answer by apixosoft Tuesday, June 21, 2016 1:28 PM
    Tuesday, June 21, 2016 1:25 PM
  • Poor, but… thank you anyway for your help! ☺

    If I fall I will arise on my way to paradise

    Tuesday, June 21, 2016 1:28 PM