MFC ERROR GetLastError
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;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。