3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/misc/except.c
6 * PURPOSE: Exception functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
8 * modified from WINE [ Onno Hovers, (onno@stack.urc.tue.nl) ]
13 #include <ddk/ntddk.h>
16 #include <kernel32/error.h>
18 typedef LONG (STDCALL *LPTOP_LEVEL_EXCEPTION_FILTER)(
19 struct _EXCEPTION_POINTERS *ExceptionInfo
23 LPTOP_LEVEL_EXCEPTION_FILTER GlobalTopLevelExceptionFilter;
25 UINT GetErrorMode(void);
29 UINT GetErrorMode(void)
36 SetErrorMode( UINT uMode )
38 UINT OldErrMode = GetErrorMode();
39 GlobalErrMode = uMode;
43 LPTOP_LEVEL_EXCEPTION_FILTER
45 SetUnhandledExceptionFilter(
46 LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter
49 LPTOP_LEVEL_EXCEPTION_FILTER OldTopLevelExceptionFilter =
50 GlobalTopLevelExceptionFilter;
51 lpTopLevelExceptionFilter = GlobalTopLevelExceptionFilter;
52 return OldTopLevelExceptionFilter;
58 UnhandledExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo)
64 if(ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION) {
65 // might check read only resource
66 // Is there a debugger running ?
67 errCode = NtQueryInformationProcess(NtCurrentProcess(),ProcessDebugPort,&DebugPort,sizeof(HANDLE),NULL);
68 if ( !NT_SUCCESS(errCode) ) {
69 SetLastErrorByStatus(errCode);
70 return EXCEPTION_EXECUTE_HANDLER;
73 //return EXCEPTION_CONTINUE_SEARCH;
75 if(GlobalTopLevelExceptionFilter != NULL) {
76 dbgRet = GlobalTopLevelExceptionFilter(ExceptionInfo);
77 if(dbgRet == EXCEPTION_EXECUTE_HANDLER)
78 return EXCEPTION_EXECUTE_HANDLER;
79 else if(dbgRet == EXCEPTION_CONTINUE_EXECUTION)
80 return EXCEPTION_CONTINUE_EXECUTION;
85 //if ( GetErrorMode() & SEM_NOGPFAULTERRORBOX == SEM_NOGPFAULTERRORBOX ) {
86 // produce a stack trace or pop a message
87 //sprintf( message, "Unhandled exception 0x%08lx at address 0x%08lx.",
88 // ExceptionInfo->ExceptionRecord->ExceptionCode,
89 // (DWORD)ExceptionInfo->ExceptionRecord->ExceptionAddress );
90 //MessageBoxA( 0, message, "Error", MB_OK | MB_ICONHAND );
93 // Returning EXCEPTION_EXECUTE_HANDLER means that the code in
94 // the __execept block will be executed. Normally this will end up in a
97 return EXCEPTION_EXECUTE_HANDLER;
104 DWORD dwExceptionCode,
105 DWORD dwExceptionFlags,
106 DWORD nNumberOfArguments,
107 CONST DWORD * lpArguments OPTIONAL
110 EXCEPTION_RECORD ExceptionRecord;
112 /* Do NOT normalize dwExceptionCode: it will be done in
113 * NTDLL.RtlRaiseException().
115 ExceptionRecord.ExceptionCode = dwExceptionCode;
116 ExceptionRecord.ExceptionRecord = NULL;
117 ExceptionRecord.ExceptionAddress = (PVOID) RaiseException;
119 * Normalize dwExceptionFlags.
121 ExceptionRecord.ExceptionFlags = (dwExceptionFlags & EXCEPTION_NONCONTINUABLE);
123 * Normalize nNumberOfArguments.
125 if (EXCEPTION_MAXIMUM_PARAMETERS < nNumberOfArguments)
127 nNumberOfArguments = EXCEPTION_MAXIMUM_PARAMETERS;
130 * If the exception has no argument,
131 * or it is a non-continuable exception,
132 * ignore nNumberOfArguments and lpArguments.
134 if ((NULL == lpArguments) || ExceptionRecord.ExceptionFlags)
136 ExceptionRecord.NumberParameters = 0;
140 ExceptionRecord.NumberParameters = nNumberOfArguments;
141 for ( nNumberOfArguments = 0;
142 (nNumberOfArguments < ExceptionRecord.NumberParameters);
143 nNumberOfArguments ++
146 ExceptionRecord.ExceptionInformation [nNumberOfArguments]
150 RtlRaiseException (& ExceptionRecord);