Fixed a few bugs, and improved the exception macros (based on
[wine/multimedia.git] / win32 / except.c
blobc455143cd7f05e45dcf5afb2fcb264977ca53044
1 /*
2 * Win32 exception functions
4 * Copyright (c) 1996 Onno Hovers, (onno@stack.urc.tue.nl)
5 * Copyright (c) 1999 Alexandre Julliard
7 * Notes:
8 * What really happens behind the scenes of those new
9 * __try{...}__except(..){....} and
10 * __try{...}__finally{...}
11 * statements is simply not documented by Microsoft. There could be different
12 * reasons for this:
13 * One reason could be that they try to hide the fact that exception
14 * handling in Win32 looks almost the same as in OS/2 2.x.
15 * Another reason could be that Microsoft does not want others to write
16 * binary compatible implementations of the Win32 API (like us).
18 * Whatever the reason, THIS SUCKS!! Ensuring portabilty or future
19 * compatability may be valid reasons to keep some things undocumented.
20 * But exception handling is so basic to Win32 that it should be
21 * documented!
25 #include <assert.h>
26 #include "winuser.h"
27 #include "winerror.h"
28 #include "ntddk.h"
29 #include "wine/exception.h"
30 #include "ldt.h"
31 #include "process.h"
32 #include "thread.h"
33 #include "stackframe.h"
34 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(seh)
39 /*******************************************************************
40 * RaiseException (KERNEL32.418)
42 void WINAPI RaiseException( DWORD code, DWORD flags, DWORD nbargs, const LPDWORD args )
44 EXCEPTION_RECORD record;
46 /* Compose an exception record */
48 record.ExceptionCode = code;
49 record.ExceptionFlags = flags & EH_NONCONTINUABLE;
50 record.ExceptionRecord = NULL;
51 record.ExceptionAddress = RaiseException;
52 if (nbargs && args)
54 if (nbargs > EXCEPTION_MAXIMUM_PARAMETERS) nbargs = EXCEPTION_MAXIMUM_PARAMETERS;
55 record.NumberParameters = nbargs;
56 memcpy( record.ExceptionInformation, args, nbargs * sizeof(*args) );
58 else record.NumberParameters = 0;
60 RtlRaiseException( &record );
64 /*******************************************************************
65 * UnhandledExceptionFilter (KERNEL32.537)
67 DWORD WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers)
69 char message[80];
70 PDB *pdb = PROCESS_Current();
72 /* FIXME: Should check if the process is being debugged */
74 if (pdb->top_filter)
76 DWORD ret = pdb->top_filter( epointers );
77 if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
80 /* FIXME: Should check the current error mode */
82 sprintf( message, "Unhandled exception 0x%08lx at address 0x%08lx.",
83 epointers->ExceptionRecord->ExceptionCode,
84 (DWORD)epointers->ExceptionRecord->ExceptionAddress );
85 MessageBoxA( 0, message, "Error", MB_OK | MB_ICONHAND );
86 return EXCEPTION_EXECUTE_HANDLER;
90 /*************************************************************
91 * SetUnhandledExceptionFilter (KERNEL32.516)
93 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
94 LPTOP_LEVEL_EXCEPTION_FILTER filter )
96 PDB *pdb = PROCESS_Current();
97 LPTOP_LEVEL_EXCEPTION_FILTER old = pdb->top_filter;
98 pdb->top_filter = filter;
99 return old;
103 /*************************************************************
104 * WINE_exception_handler
106 * Exception handler for exception blocks declared in Wine code.
108 DWORD WINAPI WINE_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
109 CONTEXT *context, LPVOID pdispatcher )
111 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
113 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
114 return ExceptionContinueSearch;
115 if (wine_frame->u.e.filter)
117 EXCEPTION_POINTERS ptrs;
118 __WINE_DUMMY_FRAME dummy;
120 ptrs.ExceptionRecord = record;
121 ptrs.ContextRecord = context;
122 dummy.u.e.code = record->ExceptionCode;
123 switch(wine_frame->u.e.filter( &ptrs, dummy, wine_frame->u.e.param ))
125 case EXCEPTION_CONTINUE_SEARCH:
126 return ExceptionContinueSearch;
127 case EXCEPTION_CONTINUE_EXECUTION:
128 return ExceptionContinueExecution;
129 case EXCEPTION_EXECUTE_HANDLER:
130 break;
131 default:
132 MESSAGE( "Invalid return value from exception filter\n" );
133 assert( FALSE );
136 RtlUnwind( frame, 0, record, 0 );
137 longjmp( wine_frame->u.e.jmp, 1 );
141 /*************************************************************
142 * WINE_finally_handler
144 * Exception handler for try/finally blocks declared in Wine code.
146 DWORD WINAPI WINE_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
147 CONTEXT *context, LPVOID pdispatcher )
149 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
151 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
152 return ExceptionContinueSearch;
153 wine_frame->u.f.finally_func( FALSE, wine_frame->u.f.param );
154 return ExceptionContinueSearch;