| 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(PEXCEPTION_POINTERS pPtrs)
|
|---|
| 35 | {
|
|---|
| 36 | PEXCEPTION_RECORD pRec = pPtrs->ExceptionRecord;
|
|---|
| 37 |
|
|---|
| 38 | printf("\nexc_filter():\n");
|
|---|
| 39 | printf("pPtrs %p\n", pPtrs);
|
|---|
| 40 | printf("ExceptionCode %p\n", pRec->ExceptionCode);
|
|---|
| 41 | printf("ExceptionAddress %p\n", pRec->ExceptionAddress);
|
|---|
| 42 | printf("NumberParameters %d\n", pRec->NumberParameters);
|
|---|
| 43 | printf("Returning EXCEPTION_CONTINUE_SEARCH.\n");
|
|---|
| 44 |
|
|---|
| 45 | return EXCEPTION_CONTINUE_SEARCH;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | void throw_EXCEPTION_INT_DIVIDE_BY_ZERO()
|
|---|
| 49 | {
|
|---|
| 50 | printf("Throwing EXCEPTION_INT_DIVIDE_BY_ZERO...\n");
|
|---|
| 51 | int x = 0;
|
|---|
| 52 | volatile int y = 4 / x;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | int test_1()
|
|---|
| 56 | {
|
|---|
| 57 | printf("The program should now expectedly crash "
|
|---|
| 58 | "(but NO POPUPLOG.OS2 entry!)...\n");
|
|---|
| 59 | __try
|
|---|
| 60 | {
|
|---|
| 61 | printf("In outer try...\n");
|
|---|
| 62 |
|
|---|
| 63 | __try
|
|---|
| 64 | {
|
|---|
| 65 | printf("In inner try...\n");
|
|---|
| 66 |
|
|---|
| 67 | throw_EXCEPTION_INT_DIVIDE_BY_ZERO();
|
|---|
| 68 |
|
|---|
| 69 | printf("FAILED: No inner exception!\n");
|
|---|
| 70 | }
|
|---|
| 71 | __except(exc_filter(exception_info()))
|
|---|
| 72 | {
|
|---|
| 73 | printf("FAILED: Inner exception handled.\n");
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | printf("FAILED: No outer exception!\n");
|
|---|
| 77 | }
|
|---|
| 78 | __except(exc_filter(exception_info()))
|
|---|
| 79 | {
|
|---|
| 80 | printf("FAILED: Inner exception handled.\n");
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | printf("FAILED: No exception at all!\n");
|
|---|
| 84 | return 1;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | void foo(int code)
|
|---|
| 88 | {
|
|---|
| 89 | __try
|
|---|
| 90 | {
|
|---|
| 91 | printf("In foo(%d)...\n", code);
|
|---|
| 92 |
|
|---|
| 93 | if (code == 2)
|
|---|
| 94 | {
|
|---|
| 95 | throw_EXCEPTION_INT_DIVIDE_BY_ZERO();
|
|---|
| 96 | printf("FAILED: No exception!\n");
|
|---|
| 97 | }
|
|---|
| 98 | else
|
|---|
| 99 | {
|
|---|
| 100 | foo(code + 1);
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | __except(exc_filter(exception_info()))
|
|---|
| 104 | {
|
|---|
| 105 | printf("FAILED: foo(%d) exception handled.\n", code);
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | static
|
|---|
| 110 | DWORD WINAPI ThreadProc(LPVOID lpParameter)
|
|---|
| 111 | {
|
|---|
| 112 | foo(0);
|
|---|
| 113 | printf("FAILED: No exception at all!\n");
|
|---|
| 114 | return 0;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | int test_2()
|
|---|
| 118 | {
|
|---|
| 119 | printf("The program should now expectedly crash "
|
|---|
| 120 | "(but NO POPUPLOG.OS2 entry!)...\n");
|
|---|
| 121 |
|
|---|
| 122 | HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
|
|---|
| 123 | if (hThread == NULL)
|
|---|
| 124 | {
|
|---|
| 125 | printf("FAILED: CreateThread().");
|
|---|
| 126 | return 1;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | WaitForSingleObject(hThread, INFINITE);
|
|---|
| 130 |
|
|---|
| 131 | return 1;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | int _main(int argc, char **argv)
|
|---|
| 135 | {
|
|---|
| 136 | int rc = test_2();
|
|---|
| 137 |
|
|---|
| 138 | printf("Return (%d).\n", rc);
|
|---|
| 139 |
|
|---|
| 140 | return rc;
|
|---|
| 141 | }
|
|---|