__except 捕获崩溃异常

__except 捕获崩溃异常

  • 一:概括
  • 二:MyTryExcept.h
  • 三:MyTryExcept.cpp
  • 三:main例子
  • 四:异常地址定位错误地址

一:概括

MyTryExcept.h、MyTryExcept.cpp、main例子。
wince环境也可用

二:MyTryExcept.h

#ifndef __HHH_TRY_EXCEPT__
#define __HHH_TRY_EXCEPT__
#include
#include
#include
#include

extern EXCEPTION_RECORD record;
extern CONTEXT context;
void writeErr();
void writeErrA();

#define my_try __try {
#define my_except }__except (record = *((GetExceptionInformation())->ExceptionRecord),\
	context = *((GetExceptionInformation())->ContextRecord),\
	EXCEPTION_EXECUTE_HANDLER) {\
	writeErrA();\
}


#endif   //#__HHH_TRY_EXCEPT__

三:MyTryExcept.cpp

#include "stdafx.h"
#include "MyTryExcept.h"
#include 

EXCEPTION_RECORD record;
CONTEXT context;
void writeErr()
{
	printf("AA\r\n");
	std::ofstream fout;
	fout.open("error.txt",std::ios::out);
	char err[128]={0};
	sprintf(err,"%p",record.ExceptionAddress);
	printf("BB\r\n");
	fout <<err<<std::endl;
    fout.close();
	printf("CC\r\n");
}
void writeErrA()
{
	WCHAR  err[128]={0};
	wprintf(err,L"%p",record.ExceptionAddress);
	//MessageBox(hWnd,err,NULL,MB_OK);
	printf("AA1\r\n");
	FILE* f;
    f = fopen("file.txt", "w");
    if (f != NULL)
    {
		printf("BB1\r\n");
		char  err1[128]={0};
		printf(err1,L"%p",record.ExceptionAddress);
        fputs(err1, f);
        fclose(f);
        f=NULL;
		printf("CC1\r\n");
    }
}

三:main例子

#include   
#include "MyTryExcept.h"
using namespace std;

int main()
{
my_try
	int ag=0;
	int bg=2;
	int cg =bg/0;
	std::cout<<cg<<std::endl;
my_except
    return 0;
}

record.ExceptionAddress为异常地址

四:异常地址定位错误地址

wince Remote Process Explorer定位崩溃地址
https://blog.csdn.net/cxd1314520/article/details/134751695?spm=1001.2014.3001.5502
使用map、cod文件定位崩溃地址
https://blog.csdn.net/dan452819043/article/details/108440921?spm=1001.2014.3001.5506
全面了解windows异常
https://zhuanlan.zhihu.com/p/562311363
wince程序崩溃应用分析法
https://www.cnblogs.com/91program/p/5201536.html
静态代码检测工具百度网盘下载
链接:https://pan.baidu.com/s/18m8HodGLYtbZa7EYyI0Vww
提取码:cx12

你可能感兴趣的:(c++,c++)