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;    

 

posted on 2015-01-23 16:32  星羽晨  阅读(598)  评论(0编辑  收藏  举报