#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;
}
SEH处理
最新推荐文章于 2021-04-12 14:45:09 发布