SEH处理

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <Dbghelp.h>
#pragma comment(lib,"Dbghelp.lib")

typedef struct _RUNTIME_FUNCTION {
	ULONG BeginAddress;
	ULONG EndAddress;
	ULONG UnwindData;
} RUNTIME_FUNCTION, *PRUNTIME_FUNCTION;

typedef struct _DISPATCHER_CONTEXT {
	ULONG64 ControlPc;
	ULONG64 ImageBase;
	PRUNTIME_FUNCTION FunctionEntry;
	ULONG64 EstablisherFrame;
	ULONG64 TargetIp;
	PCONTEXT ContextRecord;
	PEXCEPTION_ROUTINE LanguageHandler;
	PVOID HandlerData;
} DISPATCHER_CONTEXT, *PDISPATCHER_CONTEXT;


/**************************************
* Usage of MiniDump
* Use SetUnhandledExceptionFilter first
***************************************/
long WINAPI Excep_Handler(_EXCEPTION_POINTERS *excepInfo)
{
	MessageBox(NULL, _T("Crush"), _T("Info"), MB_OK);
	HANDLE hFile = ::CreateFile(_T("crush.dmp"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		std::cout << "Write dump file failed" << std::endl;
		return -1;
	}
	MINIDUMP_EXCEPTION_INFORMATION minidmpInfo;
	minidmpInfo.ThreadId = ::GetCurrentThreadId();
	minidmpInfo.ExceptionPointers = excepInfo;
	minidmpInfo.ClientPointers = FALSE;
	MiniDumpWriteDump(::GetCurrentProcess(), ::GetCurrentProcessId(), hFile, MiniDumpNormal, &minidmpInfo, NULL, NULL);
	std::cout << "Write dump file successfullly" << std::endl;
	return 0;
}


DWORD dwRet;
EXCEPTION_DISPOSITION __cdecl my_excep_handler(
	IN PEXCEPTION_RECORD pExceptionRecord,
	IN void * pEstablisherFrame,
	IN OUT PCONTEXT pContextRecord,
	IN OUT PDISPATCHER_CONTEXT pDispatcherContext
	)
{
	std::cout << "reach my exception" << std::endl;
	pContextRecord->Eax = (DWORD)&dwRet;
//	return ExceptionContinueExecution;
	return ExceptionContinueExecution;
}

int _tmain(int argc, _TCHAR* argv[])
{
//	SetUnhandledExceptionFilter(Excep_Handler);
	int *p = NULL;
	//setup a new exception handler
	__asm
	{
		push my_excep_handler
		push fs : [0]
		mov fs : [0], esp
	}

	//triger an exception
	__asm
	{
		xor eax,eax
		mov dword ptr[eax],0x123
	}
	printf("After writing! scratch=0x%08x\n", dwRet);
	//uninstall the exception handler
	__asm
	{
		pop dword ptr fs : [0]
		add esp,4
	}
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值