Moved LoadImage and related functions to cursoricon.c.
[wine/multimedia.git] / win32 / except.c
blobf642481a47e091ca5e8a0648c0962ef87aa4dac2
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 <stdio.h>
27 #include "windef.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winerror.h"
31 #include "ntddk.h"
32 #include "wine/exception.h"
33 #include "ldt.h"
34 #include "process.h"
35 #include "thread.h"
36 #include "stackframe.h"
37 #include "server.h"
38 #include "debugtools.h"
40 DEFAULT_DEBUG_CHANNEL(seh);
43 /*******************************************************************
44 * RaiseException (KERNEL32.418)
46 void WINAPI RaiseException( DWORD code, DWORD flags, DWORD nbargs, const LPDWORD args )
48 EXCEPTION_RECORD record;
50 /* Compose an exception record */
52 record.ExceptionCode = code;
53 record.ExceptionFlags = flags & EH_NONCONTINUABLE;
54 record.ExceptionRecord = NULL;
55 record.ExceptionAddress = RaiseException;
56 if (nbargs && args)
58 if (nbargs > EXCEPTION_MAXIMUM_PARAMETERS) nbargs = EXCEPTION_MAXIMUM_PARAMETERS;
59 record.NumberParameters = nbargs;
60 memcpy( record.ExceptionInformation, args, nbargs * sizeof(*args) );
62 else record.NumberParameters = 0;
64 RtlRaiseException( &record );
68 /*******************************************************************
69 * UnhandledExceptionFilter (KERNEL32.537)
71 DWORD WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS epointers)
73 struct exception_event_request *req = get_req_buffer();
74 char message[80];
75 PDB *pdb = PROCESS_Current();
77 /* send a last chance event to the debugger */
78 req->record = *epointers->ExceptionRecord;
79 req->first = 0;
80 req->context = *epointers->ContextRecord;
81 if (!server_call_noerr( REQ_EXCEPTION_EVENT )) *epointers->ContextRecord = req->context;
82 if (req->status == DBG_CONTINUE) return EXCEPTION_CONTINUE_EXECUTION;
84 if (pdb->top_filter)
86 DWORD ret = pdb->top_filter( epointers );
87 if (ret != EXCEPTION_CONTINUE_SEARCH) return ret;
90 /* FIXME: Should check the current error mode */
92 sprintf( message, "Unhandled exception 0x%08lx at address 0x%08lx.",
93 epointers->ExceptionRecord->ExceptionCode,
94 (DWORD)epointers->ExceptionRecord->ExceptionAddress );
95 MessageBoxA( 0, message, "Error", MB_OK | MB_ICONHAND );
96 return EXCEPTION_EXECUTE_HANDLER;
100 /*************************************************************
101 * SetUnhandledExceptionFilter (KERNEL32.516)
103 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter(
104 LPTOP_LEVEL_EXCEPTION_FILTER filter )
106 PDB *pdb = PROCESS_Current();
107 LPTOP_LEVEL_EXCEPTION_FILTER old = pdb->top_filter;
108 pdb->top_filter = filter;
109 return old;
113 /*************************************************************
114 * WINE_exception_handler
116 * Exception handler for exception blocks declared in Wine code.
118 DWORD WINE_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
119 CONTEXT *context, LPVOID pdispatcher )
121 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
123 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
124 return ExceptionContinueSearch;
125 if (wine_frame->u.filter)
127 EXCEPTION_POINTERS ptrs;
128 ptrs.ExceptionRecord = record;
129 ptrs.ContextRecord = context;
130 switch(wine_frame->u.filter( &ptrs ))
132 case EXCEPTION_CONTINUE_SEARCH:
133 return ExceptionContinueSearch;
134 case EXCEPTION_CONTINUE_EXECUTION:
135 return ExceptionContinueExecution;
136 case EXCEPTION_EXECUTE_HANDLER:
137 break;
138 default:
139 MESSAGE( "Invalid return value from exception filter\n" );
140 assert( FALSE );
143 /* hack to make GetExceptionCode() work in handler */
144 wine_frame->ExceptionCode = record->ExceptionCode;
145 wine_frame->ExceptionRecord = wine_frame;
147 RtlUnwind( frame, 0, record, 0 );
148 EXC_pop_frame( frame );
149 longjmp( wine_frame->jmp, 1 );
153 /*************************************************************
154 * WINE_finally_handler
156 * Exception handler for try/finally blocks declared in Wine code.
158 DWORD WINE_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
159 CONTEXT *context, LPVOID pdispatcher )
161 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
163 if (!(record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
164 return ExceptionContinueSearch;
165 wine_frame->u.finally_func( FALSE );
166 return ExceptionContinueSearch;