VC++出错处理

来源:互联网 发布:移动网络玩传奇霸业 编辑:程序博客网 时间:2024/06/06 21:39
出错处理:
LPVOID lpMsgBuf; //Windows will allocate 
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,0, 
GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //默认语言
(LPTSTR)&lpMsgBuf, 0, NULL );


//显示
::MessageBox(0, (LPCTSTR)lpMsgBuf, _T("GetLastError"), MB_OK|MB_ICONINFORMATION );
//lpMsgBuf中是你要的错误提示.


//释放内存
::LocalFree( lpMsgBuf );


可以用MessageBox,但是GetLastError返回值不是字符串,所以用FormatMessage转换。

原型:

DWORD FormatMessage(
DWORD dwFlags, // source and processing options
LPCVOID lpSource, // message source
DWORD dwMessageId, // message identifier
DWORD dwLanguageId, // language identifier
LPTSTR lpBuffer, // message buffer
DWORD nSize, // maximum size of message buffer
va_list *Arguments // array of message inserts
);
返回值为实际输出字符串的长度。

1 0