.386
.model flat, stdcall
option casemap:none
include windows.inc
include user32.inc
include kernel32.inc
includelib user32.lib
includelib kernel32.lib
.data
lpOldHandler dd ?
.const
szMsg db '异常发生位置: %08X, 异常代码: %08X, 标志: %08X', 0
szSafe db '回到安全地方!!!', 0
szCaption db '筛选器异常处理的例子!!!', 0
.code
_Handler proc _lpExceptionPoint
LOCAL @szBuffer[256]:byte
pushad
mov esi, _lpExceptionPoint
assume esi: ptr EXCEPTION_POINTERS
mov edi, [esi].ContextRecord
mov esi, [esi].pExceptionRecord
assume esi: ptr EXCEPTION_RECORD, edi: ptr CONTEXT
invoke wsprintf, addr @szBuffer, addr szMsg, [edi].regEip, [esi].ExceptionCode, [esi].ExceptionFlags
invoke MessageBox, NULL, addr @szBuffer, NULL, MB_OK
;设置跳到哪行代码去继续执行,
mov [edi].regEip, offset _SafePlace
assume esi: nothing, edi: nothing
popad
;返回值设置为继续执行代码
mov eax, EXCEPTION_CONTINUE_EXECUTION
ret
_Handler endp
start:
;设置异常回调函数
invoke SetUnhandledExceptionFilter, addr _Handler
;保存原先的异常回调函数
mov lpOldHandler, eax
xor eax, eax
;产生异常,就会跳到上边设置的异常回调函数去执行
mov dword ptr [eax], 0
_SafePlace:
invoke MessageBox, NULL, addr szSafe, addr szCaption, MB_OK
;设置回原先的异常回调函数
invoke SetUnhandledExceptionFilter, lpOldHandler
invoke ExitProcess, NULL
end start