;************************************************************************
;*项目名称:Thread SEH示例 *
;************************************************************************
;*代码名称:Thread SEH_2_4.asm *
;*代码功能:显示一些异常信息 *
;************************************************************************
;*代码作者:by lujue, 2006-10-30 *
;************************************************************************
;************************************************************************
;汇编模式定义
;************************************************************************
.386
.model flat,stdcall
option casemap:none
;************************************************************************
;头文件定义
;************************************************************************
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
;************************************************************************
;数据段定义
;************************************************************************
.const
szMsg db "We are in Thread Structured Exception Handler",0dh,0ah,0dh,0ah,/
"异常发生位置:%08X,异常代码:%08X,标志:%08X",0
szSafe db "回到了安全的地方!",0
szCaption db "Thread SEH示例",0
szText db "Thread SEH程序没有运行",0
;************************************************************************
;代码段
;************************************************************************
.code
;********************************************************************
; 函数功能:异常处理回调函数:修改 regEip 使其指向ExecuteHere处的位置
;********************************************************************
_Handler Proc _lpExceptionRecord,_lpSEH,_lpContext,_lpDispatcherContext
local @szBuffer[256]:byte
pushad
mov esi,_lpExceptionRecord
mov edi,_lpContext
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,addr szCaption,MB_OK
; 将 EIP 指向安全的位置并恢复堆栈
mov eax,_lpSEH
push [eax + 8]
pop [edi].regEip
push [eax + 0ch]
pop [edi].regEbp
push eax
pop [edi].regEsp
assume esi:nothing,edi:nothing
popad
mov eax,ExceptionContinueExecution
ret
_Handler Endp
;********************************************************************
;程序入口
;********************************************************************
Start:
;********************************************************************
; 在堆栈中构造一个 EXCEPTION_REGISTRATION_RECORD 结构
;********************************************************************
assume fs:nothing
push ebp
push offset _SafePlace
push offset _Handler
push fs:[0]
mov fs:[0],esp
;********************************************************************
; 会引发异常的指令
;********************************************************************
xor ebp,ebp
xor eax,eax
mov dword ptr [eax],0
WouldBeOmit:
invoke MessageBox,0,addr szText,addr szCaption,MB_OK ;这一句将无法被执行
;********************************************************************
; 异常处理完毕后,从这里开始执行
;********************************************************************
_SafePlace:
invoke MessageBox,NULL,addr szSafe,addr szCaption,MB_OK
;********************************************************************
; 恢复原来的 SEH 链
;********************************************************************
pop fs:[0]
add esp,0ch
invoke ExitProcess,NULL
End Start