Created
June 30, 2012 06:42
-
-
Save kvanbere/3022692 to your computer and use it in GitHub Desktop.
C++ Error Handling by TheNullz + Ghoul
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #include "GhoulTrainer.h" | |
#define AllocateBuffer(len) \ | |
new TCHAR[##len] | |
#ifndef Error | |
#define Error(func) \ | |
HandleError(TEXT(#func), TEXT(__FILE__), __LINE__) | |
#endif | |
// myCppFile.cpp | |
LPCTSTR lpFormatStr = "File: %s, Line: %d\n%s"; | |
VOID HandleError ( | |
__in LPCTSTR lpProcName, | |
__in LPCTSTR lpSourceFile, | |
__in INT iLine | |
) | |
{ | |
HWND hWnd = GetDesktopWindow(); | |
DWORD dwMessageIdentifier = GetLastError(); | |
SIZE_T nStrSize = 1024; | |
LPTSTR lpStrBuf = AllocateBuffer(nStrSize); | |
__try { | |
ZeroMemory(lpStrBuf, nStrSize); | |
if (dwMessageIdentifier != NO_ERROR) { | |
LPVOID lpvErrStrBuffer = NULL; | |
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwMessageIdentifier, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpvErrStrBuffer, 0, NULL) == 0) { | |
lpvErrStrBuffer = "An unknown error has occured."; | |
} | |
if (SUCCEEDED(sprintf_s(lpStrBuf, nStrSize, lpFormatStr, lpProcName, lpSourceFile, iLine, lpvErrStrBuffer))) { | |
MessageBox(hWnd, lpStrBuf, "Internal Error", MB_ICONERROR | MB_OK | MB_SETFOREGROUND | MB_TOPMOST); | |
} | |
if (lpvErrStrBuffer != NULL) { | |
LocalFree(reinterpret_cast<HLOCAL>(lpvErrStrBuffer)); | |
} | |
} else if (errno != NULL) { | |
LPTSTR lpvErrStrBuffer = AllocateBuffer(nStrSize); | |
ZeroMemory(lpvErrStrBuffer, nStrSize); | |
if (!_tcserror_s(lpvErrStrBuffer, nStrSize, errno)) { | |
if (SUCCEEDED(sprintf_s(lpStrBuf, nStrSize, lpFormatStr, lpProcName, lpSourceFile, iLine, lpvErrStrBuffer))) { | |
MessageBox(hWnd, lpStrBuf, "Internal Error", MB_ICONERROR | MB_OK | MB_SETFOREGROUND | MB_TOPMOST); | |
} | |
} | |
delete [] lpvErrStrBuffer; | |
} | |
} __finally { | |
delete [] lpStrBuf; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment