1 #include <windows.h>
2 #include <excpt.h>
3 #include <stdio.h>
4
5 /* The exception handler */
6 #ifndef _WIN64
_exception_filter(struct _EXCEPTION_RECORD * ed,void * v1,_CONTEXT * cont,void * v2)7 extern "C" EXCEPTION_DISPOSITION _exception_filter(struct _EXCEPTION_RECORD* ed,void *v1,_CONTEXT *cont,void* v2)
8 #else
9 extern "C" long _exception_filter (EXCEPTION_POINTERS *exception_data)
10 #endif
11 {
12 #ifdef _WIN64
13 struct _EXCEPTION_RECORD* ed = exception_data->ExceptionRecord;
14 #endif
15 char s[512];
16 sprintf (s, "0x%x\n", (unsigned int) ed->ExceptionCode);
17 MessageBox (NULL, s, "Exception code:", MB_OK);
18 /// RaiseException()
19 switch(ed->ExceptionCode)
20 {
21
22 case EXCEPTION_ACCESS_VIOLATION:
23 MessageBox(0,"Exception: Access vioalation","Exception",16);
24 break;
25 case EXCEPTION_BREAKPOINT:
26 MessageBox(0,"Exception: Breakpoint","Exception",16);
27 break;
28 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
29 MessageBox(0,"Exception: Array index out of bounds","Exception",16);
30 break;
31 case EXCEPTION_STACK_OVERFLOW:
32 MessageBox(0,"Exception: Stack overflow","Exception",16);
33 break;
34 case EXCEPTION_PRIV_INSTRUCTION:
35 MessageBox(0,"Exception: General Protection Fault","Exception",16);
36 break;
37 case EXCEPTION_ILLEGAL_INSTRUCTION:
38 MessageBox(0,"Exception: Illegal instruction in program","Exception",16);
39 break;
40 case EXCEPTION_INT_OVERFLOW:
41 MessageBox(0,"Exception: Integer overflow","Exception",16);
42 break;
43 case EXCEPTION_INT_DIVIDE_BY_ZERO:
44 MessageBox(0,"Exception: Integer division by zero","Exception",16);
45 break;
46 case EXCEPTION_FLT_UNDERFLOW :
47 MessageBox(0,"Exception: Floating point value underflow","Exception",16);
48 break;
49 default: MessageBox(0,"Unknown exception","Exception",16);
50 }
51
52
53 exit(666); // It's better to use ExitProcess()
54
55 }
56 /* Declare exception registration struct */
57 EXCEPTION_REGISTRATION *er;
58
59
60 /* This macro initializes SEH */
61 #ifndef _WIN64
62 #define init_seh() \
63 { \
64 er=new EXCEPTION_REGISTRATION; \
65 er->handler=_exception_filter; \
66 __try1(_exception_filter); \
67 }
68 #else
69 #define init_seh() \
70 { \
71 __try1(_exception_filter); \
72 }
73 #endif
74 /* This stops SEH */
75 #define stop_seh(void) \
76 { \
77 __except1; \
78 }
79
80 int zero = 0;
81 int val = 100;
82
main()83 int main()
84 {
85 init_seh()
86
87 /* let's crash our program */
88 int a=val/zero;
89 //short *ex=(short*)0xb800000;
90 //ex[0]='a';
91
92
93
94 stop_seh();
95 }
96