Is there a way to get the string representation of HRESULT value using win API?

khkarens picture khkarens · Aug 10, 2011 · Viewed 53.1k times · Source

Is there a function in win API which can be used to extract the string representation of HRESULT value?

The problem is that not all return values are documented in MSDN, for example ExecuteInDefaultAppDomain() function is not documented to return "0x80070002 - The system cannot find the file specified.", however, it does! Therefore, I was wondering whether there is a function to be used in common case.

Answer

eran picture eran · Aug 10, 2011

You can use _com_error:

_com_error err(hr);
LPCTSTR errMsg = err.ErrorMessage();

If you don't want to use _com_error for whatever reason, you can still take a look at its source, and see how it's done.

Don't forget to include the header comdef.h