Without:
- MFC
- ATL
How can I use FormatMessage()
to get the error text for a HRESULT
?
HRESULT hresult = application.CreateInstance("Excel.Application");
if (FAILED(hresult))
{
// what should i put here to obtain a human-readable
// description of the error?
exit (hresult);
}
Here's the proper way to get an error message back from the system for an
HRESULT
(named hresult in this case, or you can replace it withGetLastError()
):The key difference between this and David Hanak's answer is the use of the
FORMAT_MESSAGE_IGNORE_INSERTS
flag. MSDN is a bit unclear on how insertions should be used, but Raymond Chen notes that you should never use them when retrieving a system message, as you've no way of knowing which insertions the system expects.FWIW, if you're using Visual C++ you can make your life a bit easier by using the
_com_error
class:Not part of MFC or ATL directly as far as I'm aware.