首页 > 编程语言 > 详细

MFC ERROR GetLastError

时间:2015-01-23 17:55:24      阅读:323      评论:0      收藏:0      [点我收藏+]
 1 void CStockServerDlg::ErrorExit(LPTSTR lpszFunction)    //错误函数
 2 {
 3     // Retrieve the system error message for the last-error code
 4 
 5     LPVOID lpMsgBuf;
 6     LPVOID lpDisplayBuf;
 7     DWORD dw = GetLastError(); 
 8     
 9     FormatMessage(
10         FORMAT_MESSAGE_ALLOCATE_BUFFER | 
11         FORMAT_MESSAGE_FROM_SYSTEM |
12         FORMAT_MESSAGE_IGNORE_INSERTS,
13         NULL,
14         dw,
15         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
16         (LPTSTR) &lpMsgBuf,
17         0, NULL );
18 
19     // Display the error message and exit the process
20 
21     lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
22         (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); 
23     StringCchPrintf((LPTSTR)lpDisplayBuf, 
24         LocalSize(lpDisplayBuf),
25         TEXT("%s failed with error %d: %s"), 
26         lpszFunction, dw, lpMsgBuf); 
27     ::MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 
28     LocalFree(lpMsgBuf);
29     LocalFree(lpDisplayBuf);
30     ExitProcess(dw);
31 }
32 
33 //调用它
34         CStockServerDlg* pError =new CStockServerDlg();
35         pError->ErrorExit(TEXT("Create"));
36         delete pError;    //避免其成为"野指针"
37         pError = NULL;    

 

MFC ERROR GetLastError

原文:http://www.cnblogs.com/royaldesigner/p/4244547.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!