MFC中获取HRESULT错误消息内容

直接看代码:

CString GetComExceptionMessage(HRESULT hr)
{
	CString strMessage;
	switch (hr)
	{
	case S_OK:
	case S_FALSE:
		strMessage.Format(_T("%ld - %s"), hr, _T("无执行错误"));
		break;
		 	case E_FAIL:
		 		strMessage.Format(_T("%ld - %s"),hr,_T("语法错误"));
		 		break;
		 	case E_INVALIDARG:
		 		strMessage.Format(_T("%ld - %s"),hr,_T("一个或多个参数无效"));
		 		break;
		 	case E_NOINTERFACE:
		 		strMessage.Format(_T("%ld - %s"),hr,_T("不支持此接口"));
		 		break;
		 	case E_NOTIMPL:
		 		strMessage.Format(_T("%ld - %s"),hr,_T("未实现"));
		 		break;
		 	case E_OUTOFMEMORY:
		 		strMessage.Format(_T("%ld - %s"),hr,_T("未能分配所需的内存"));
		 		break;
		 	case E_POINTER:
		 		strMessage.Format(_T("%ld - %s"),hr,_T("无效的指针"));
		 		break;
		 	case E_UNEXPECTED:
		 		strMessage.Format(_T("%ld - %s"),hr,_T("未知错误"));
		 		break;
		 	case E_HANDLE:
		 		strMessage.Format(_T("%ld - %s"),hr,_T("无效句柄"));
		 		break;
		 	case E_ABORT:
		 		strMessage.Format(_T("%ld - %s"),hr,_T("执行被取消"));
		 		break;
		 	case E_ACCESSDENIED:
		 		strMessage.Format(_T("%ld - %s"),hr,_T("禁止访问"));
		 		break;
		 	case E_PENDING:
		 		strMessage.Format(_T("%ld - %s"),hr,_T("执行被挂起"));
		 		break;
	default:
	{
		IErrorInfoPtr ipErrorInfo;
		BSTR description(_bstr_t(_T("")));
		GetErrorInfo(NULL, &ipErrorInfo);
		if (ipErrorInfo)
		{
			ipErrorInfo->GetDescription(&description);
			strMessage = description;
			USES_CONVERSION;
		}
		strMessage.Format(_T("%ld - %s"), (long)hr, (CString)strMessage);
	}
	}
	return strMessage;
}


 

你可能感兴趣的:(MFC中获取HRESULT错误消息内容)