Get Error Message from HRESULT

Need to look up the error string for an HRESULT code? Here’s some code to do so.

#include <comdef.h>

CString GetMsgForHresult(HRESULT hr)
{
CString cs;
CString msg = _com_error(hr).ErrorMessage();
cs.Format(_T("Error 0x%08x: %s"), hr, msg);
return cs;
}

This entry was posted in tech. Bookmark the permalink.

2 Responses to Get Error Message from HRESULT

  1. It looks like _com_error::ErrorMessage() uses FormatMessage internally.

  2. Pingback: Generate diagnostic message for HRESULT codes? | The Largest Forum Archive

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>