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