GetStrError(CString& strError, const DWORD& dwErr)
{
LPVOID lpMsgBuf;
//DWORD dw = GetLastError();
FormatMessage (
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwErr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
strError = (char *)lpMsgBuf;
LocalFree(lpMsgBuf);
}
个人写的:
GetStrError(CString &strError, const DWORD &dwErr)
{
LPVOID lpMsgBuf;
//DWORD dw = GetLastError();
FormatMessage (
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwErr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
strError = (char *)lpMsgBuf;
LocalFree(lpMsgBuf);
}
MyAfxMessageBox(CString strError)
{
CString str;
DWORD dwErr = GetLastError();
GetStrError(str, dwErr);
CString strTemp;
strTemp.Format(_T("程序运行错误!%s\r\n错误码:%lu\r\n错误内容:%s"), strError, dwErr, str);
AfxMessageBox(strTemp);
}
try
{
AfxMessageBox(strMsg, MB_ICONINFORMATION);
}
catch (CMemoryException *e)
{
MyAfxMessageBox(_T("CMemoryException"));
}
catch (CFileException *e)
{
MyAfxMessageBox(_T("CFileException"));
}
catch (CException *e)
{
MyAfxMessageBox(_T("CException"));
}
catch(...)
{
MyAfxMessageBox(_T("其他"));
}