| 1 | #include <stdio.h>
|
|---|
| 2 | #include <windows.h>
|
|---|
| 3 | #include <excpt.h>
|
|---|
| 4 | #include <setjmp.h>
|
|---|
| 5 |
|
|---|
| 6 | #ifndef _MSC_VER
|
|---|
| 7 |
|
|---|
| 8 | #include <odinlx.h>
|
|---|
| 9 |
|
|---|
| 10 | int _main();
|
|---|
| 11 | int _argc;
|
|---|
| 12 | char **_argv;
|
|---|
| 13 |
|
|---|
| 14 | int WIN32API WinMain(HANDLE hInstance,
|
|---|
| 15 | HANDLE hPrevInstance,
|
|---|
| 16 | LPSTR lpCmdLine,
|
|---|
| 17 | int nCmdShow)
|
|---|
| 18 | {
|
|---|
| 19 | return _main(_argc, _argv);
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | int main(int argc, char **argv)
|
|---|
| 23 | {
|
|---|
| 24 | _argc = argc;
|
|---|
| 25 | _argv = argv;
|
|---|
| 26 | EnableSEH();
|
|---|
| 27 | RegisterLxExe(WinMain, NULL);
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | #else
|
|---|
| 31 | #define _main main
|
|---|
| 32 | #endif
|
|---|
| 33 |
|
|---|
| 34 | int exc_filter(DWORD code, PEXCEPTION_POINTERS pPtrs)
|
|---|
| 35 | {
|
|---|
| 36 | PEXCEPTION_RECORD pRec = pPtrs->ExceptionRecord;
|
|---|
| 37 |
|
|---|
| 38 | printf("Filter: code %08lx\n", code);
|
|---|
| 39 | printf("ExceptionCode %p\n", pRec->ExceptionCode);
|
|---|
| 40 | printf("ExceptionAddress %p\n", pRec->ExceptionAddress);
|
|---|
| 41 | printf("NumberParameters %d\n", pRec->NumberParameters);
|
|---|
| 42 |
|
|---|
| 43 | return EXCEPTION_EXECUTE_HANDLER;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | jmp_buf jmp_buffer;
|
|---|
| 47 |
|
|---|
| 48 | void foo()
|
|---|
| 49 | {
|
|---|
| 50 | printf("In foo.\n");
|
|---|
| 51 | longjmp(jmp_buffer, 1);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | int test_1()
|
|---|
| 55 | {
|
|---|
| 56 | __try
|
|---|
| 57 | {
|
|---|
| 58 | printf("__try begin.\n");
|
|---|
| 59 |
|
|---|
| 60 | if (!setjmp(jmp_buffer))
|
|---|
| 61 | {
|
|---|
| 62 | printf("After setjmp.\n");
|
|---|
| 63 | foo();
|
|---|
| 64 | printf("FAILED: After foo.\n");
|
|---|
| 65 | return 1;
|
|---|
| 66 | }
|
|---|
| 67 | else
|
|---|
| 68 | {
|
|---|
| 69 | printf("After longjmp.\n");
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | printf("__try end.\n");
|
|---|
| 73 | }
|
|---|
| 74 | __except(exc_filter(GetExceptionCode(), GetExceptionInformation()))
|
|---|
| 75 | {
|
|---|
| 76 | // handle exception
|
|---|
| 77 | printf("Exception handled.\n");
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | return 0;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | int test_2()
|
|---|
| 84 | {
|
|---|
| 85 | if (!setjmp(jmp_buffer))
|
|---|
| 86 | {
|
|---|
| 87 | printf("After setjmp.\n");
|
|---|
| 88 |
|
|---|
| 89 | __try
|
|---|
| 90 | {
|
|---|
| 91 | printf("__try begin.\n");
|
|---|
| 92 | foo();
|
|---|
| 93 | printf("FAILED: After foo.\n");
|
|---|
| 94 | return 1;
|
|---|
| 95 | }
|
|---|
| 96 | __except(exc_filter(GetExceptionCode(), GetExceptionInformation()))
|
|---|
| 97 | {
|
|---|
| 98 | // handle exception
|
|---|
| 99 | printf("Exception handled.\n");
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | else
|
|---|
| 103 | {
|
|---|
| 104 | printf("After longjmp.\n");
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | return 0;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | int _main(int argc, char **argv)
|
|---|
| 111 | {
|
|---|
| 112 | int code = argc > 1 ? atoi(argv[1]) : 0;
|
|---|
| 113 |
|
|---|
| 114 | int rc = 0;
|
|---|
| 115 |
|
|---|
| 116 | switch (code)
|
|---|
| 117 | {
|
|---|
| 118 | case 1:
|
|---|
| 119 | rc = test_1();
|
|---|
| 120 | break;
|
|---|
| 121 |
|
|---|
| 122 | // Note that since we disabled Win32 exception handler unwinding
|
|---|
| 123 | // support (see comments in OS2ExceptionHandler3ndLevel()), test_2()
|
|---|
| 124 | // will always crash now
|
|---|
| 125 | #if 0
|
|---|
| 126 | case 2:
|
|---|
| 127 | // note that test_2() will actually only work if the top exception
|
|---|
| 128 | // handler is our SEH handler (i.e. __try block). See #15 for more
|
|---|
| 129 | // details.
|
|---|
| 130 | rc = test_2();
|
|---|
| 131 | break;
|
|---|
| 132 | #endif
|
|---|
| 133 | default:
|
|---|
| 134 | break;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | printf("Return (%d).\n", rc);
|
|---|
| 138 |
|
|---|
| 139 | return rc;
|
|---|
| 140 | }
|
|---|