1 | #include <stdio.h>
|
---|
2 | #include <windows.h>
|
---|
3 | #include <excpt.h>
|
---|
4 |
|
---|
5 | #ifndef _MSC_VER
|
---|
6 |
|
---|
7 | #include <odinlx.h>
|
---|
8 |
|
---|
9 | int _main();
|
---|
10 |
|
---|
11 | int WIN32API WinMain(HANDLE hInstance,
|
---|
12 | HANDLE hPrevInstance,
|
---|
13 | LPSTR lpCmdLine,
|
---|
14 | int nCmdShow)
|
---|
15 | {
|
---|
16 | return _main();
|
---|
17 | }
|
---|
18 |
|
---|
19 | int main()
|
---|
20 | {
|
---|
21 | EnableSEH();
|
---|
22 | RegisterLxExe(WinMain, NULL);
|
---|
23 | }
|
---|
24 |
|
---|
25 | #else
|
---|
26 | #define _main main
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | int exc_filter(DWORD code, DWORD filtercode, PEXCEPTION_RECORD pRec)
|
---|
30 | {
|
---|
31 | printf("Filter: code %08lx, filtercode %08lx\n", code, filtercode);
|
---|
32 | printf("ExceptionCode %p\n", pRec->ExceptionCode);
|
---|
33 | printf("ExceptionAddress %p\n", pRec->ExceptionAddress);
|
---|
34 | printf("NumberParameters %d\n", pRec->NumberParameters);
|
---|
35 |
|
---|
36 | // if (code == filtercode)
|
---|
37 | return EXCEPTION_EXECUTE_HANDLER;
|
---|
38 | // return EXCEPTION_CONTINUE_SEARCH;
|
---|
39 | // return EXCEPTION_CONTINUE_EXECUTION;
|
---|
40 | }
|
---|
41 |
|
---|
42 | void throw_EXCEPTION_INT_DIVIDE_BY_ZERO()
|
---|
43 | {
|
---|
44 | printf("Throwing EXCEPTION_INT_DIVIDE_BY_ZERO...\n");
|
---|
45 | int x = 0;
|
---|
46 | volatile int y = 4 / x;
|
---|
47 | }
|
---|
48 |
|
---|
49 | void throw_EXCEPTION_ACCESS_VIOLATION()
|
---|
50 | {
|
---|
51 | printf("Throwing EXCEPTION_ACCESS_VIOLATION...\n");
|
---|
52 | volatile int *x = 0;
|
---|
53 | *x = 0;
|
---|
54 | }
|
---|
55 |
|
---|
56 | int _main()
|
---|
57 | {
|
---|
58 | __try
|
---|
59 | {
|
---|
60 | #if 0
|
---|
61 | throw_EXCEPTION_INT_DIVIDE_BY_ZERO();
|
---|
62 | printf("FAILED: No outer exception!\n");
|
---|
63 | return 1;
|
---|
64 | #endif
|
---|
65 | __try
|
---|
66 | {
|
---|
67 | #if 1
|
---|
68 | throw_EXCEPTION_ACCESS_VIOLATION();
|
---|
69 | #else
|
---|
70 | throw_EXCEPTION_INT_DIVIDE_BY_ZERO();
|
---|
71 | #endif
|
---|
72 | printf("FAILED: No inner exception!\n");
|
---|
73 | return 1;
|
---|
74 | }
|
---|
75 | __except(exc_filter(GetExceptionCode(),
|
---|
76 | EXCEPTION_ACCESS_VIOLATION,
|
---|
77 | _exception_info()->ExceptionRecord))
|
---|
78 | {
|
---|
79 | // handle exception
|
---|
80 | printf("Inner exception handled.\n");
|
---|
81 | }
|
---|
82 | }
|
---|
83 | __except(exc_filter(GetExceptionCode(),
|
---|
84 | EXCEPTION_INT_DIVIDE_BY_ZERO,
|
---|
85 | _exception_info()->ExceptionRecord))
|
---|
86 | {
|
---|
87 | // handle exception
|
---|
88 | printf("Outer exception handled.\n");
|
---|
89 | }
|
---|
90 |
|
---|
91 | printf("Return.\n");
|
---|
92 |
|
---|
93 | return 0;
|
---|
94 | }
|
---|