- /************************************************************************/
- /*函数名:ShowLastError
- /*功 能:封装GetLastError,输出错误信息
- /*返回值:无
- /************************************************************************/
- void Utility::ShowLastError(
- LPTSTR lpszFunction//自定义信息,可以是出错的函数名等提示信息
- )
- {
- // Retrieve the system error message for the last-error code
- LPVOID lpMsgBuf;
- LPVOID lpDisplayBuf;
- DWORD dw = GetLastError();
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- dw,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &lpMsgBuf,
- 0, NULL );
- // Display the error message
- lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
- (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
- StringCchPrintf((LPTSTR)lpDisplayBuf,
- LocalSize(lpDisplayBuf),
- TEXT(\"%s failed with error %d: %s\"),
- lpszFunction, dw, lpMsgBuf);
- ::MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT(\"Error\"), MB_ICONERROR);
- LocalFree(lpMsgBuf);
- LocalFree(lpDisplayBuf);
- }
- //该片段来自于http://www.codesnippet.cn/detail/13112012307.html
来源: http://www.codesnippet.cn/detail/13112012307.html