Code:
#include <errorx.h>
#include <wchar.h>
////////////////////////////////////////////////////////////////
// Error text lookup array
//
#pragma pack(1)
typedef struct MsgText
{ unsigned long code;
wchar_t msg[60]; }
MessageText;
const MessageText Message[] =
{ EXCEPTION_ACCESS_VIOLATION,
L"Access violation: protected system resources",
EXCEPTION_DATATYPE_MISALIGNMENT,
L"A data type misalignment has caused system errors",
EXCEPTION_BREAKPOINT,
L"A breakpoint has been reached while debugging",
EXCEPTION_SINGLE_STEP,
L"Single stepping mode while debugging",
EXCEPTION_ARRAY_BOUNDS_EXCEEDED,
L"Array access outside of allocated memory",
EXCEPTION_FLT_DENORMAL_OPERAND,
L"Floating point error, bad demoromal operand",
EXCEPTION_FLT_DIVIDE_BY_ZERO,
L"Floating point error, attempting to divide by zero",
EXCEPTION_FLT_INEXACT_RESULT,
L"Floating point error, inexact result",
EXCEPTION_FLT_INVALID_OPERATION,
L"Floating point error, invalid operation attempted",
EXCEPTION_FLT_OVERFLOW,
L"Floating point error, operation caused an overflow",
EXCEPTION_FLT_STACK_CHECK,
L"Floating point error, FPU stack corrupted",
EXCEPTION_FLT_UNDERFLOW,
L"Floating point error, operation caused an underflow",
EXCEPTION_INT_DIVIDE_BY_ZERO,
L"Integer math error, attempted to divide by zero",
EXCEPTION_INT_OVERFLOW,
L"Integer math error, operation caused an overflow",
EXCEPTION_PRIV_INSTRUCTION,
L"An attempt was made to execute priveledged instructions",
EXCEPTION_IN_PAGE_ERROR,
L"Memory page protection error",
EXCEPTION_ILLEGAL_INSTRUCTION,
L"An attempt was made to execute unknown CPU instructions",
EXCEPTION_NONCONTINUABLE_EXCEPTION,
L"Too many nested exceptions have occured",
EXCEPTION_STACK_OVERFLOW,
L"Stack overflow",
EXCEPTION_INVALID_DISPOSITION,
L"An invalid disposition error has occured",
EXCEPTION_GUARD_PAGE,
L"Invalid guard page",
EXCEPTION_INVALID_HANDLE,
L"Invalid system handle",
STATUS_NO_MEMORY,
L"Out of memory",
CONTROL_C_EXIT,
L"A keyboard exit has been requested",
0xFFFFFFFF,
L"No text description is available for this error" };
/////////////////////////////////////////////////////////////////////
// Fetch Windows error string
// 1. Searches MessageTable resources for error text matching errcode
// 2. Result is placed at errtext which must be at least MAX_PATH bytes.
// 2. width = right margin, 0 = no right margin
//
void WINAPI GetErrorTextW(unsigned long errcode, wchar_t *errtext, int width)
{ // fetch the current module's handle
HMODULE hmod = GetModuleHandle(NULL);
// locate the message in the local message table
if (FormatMessageW(FORMAT_MESSAGE_FROM_HMODULE | width,
hmod,errcode,0,errtext,256,NULL))
return;
// try the system list
if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | width,
NULL,errcode,0,errtext,256,NULL))
return;
// now the internal table
int x = 0;
while ((Message[x].code != errcode) &
(Message[x].code != 0xFFFFFFFF))
{ x++; }
FormatMessageW(FORMAT_MESSAGE_FROM_STRING | width,
Message[x].msg,0,0,errtext,256,NULL); }