
How to show error message
Quote:
> catch(_com_error e)
> {
> CString eMsg;
> eMsg.Format("Can't connect to database, error is %s\
> ",e.Description);
> AfxMessageBox(eMsg);
> }
Personally, my favorite is:
catch(_com_error e)
{
CString eMsg;
eMsg.Format("Can't connect to database, error is %s\
",static_cast<LPCTSTR>(e.Description()));
AfxMessageBox(eMsg);
}
This compiles in unicode and ANSI builds. Unfortunately, it's also possible
for it to crash, as if _bstr_t contains a NULL BSTR, this returns a NULL for
the cast.
You'll have to add appropriate checks to make sure that there actually is a
description in there. I'll leave it to you to come up with some ingenious
code for that.
You might also want to consider the case where Description is empty, but
ErrorMessage has useful information.