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