15,471
社区成员
发帖
与我相关
我的任务
分享
#if !defined(__JADE_EXCEPTION_H_)
#define __JADE_EXCEPTION_H_
class CExceptionByJade
{
public:
/*****************************************************************************\
函数名称:CExceptionByJade
函数功能:异常类型对象构造函数
参数:
input:
DWORD dwCode 异常信息代号
std::string szFun 异常函数名称
LPEXCEPTION_POINTERS lpException 异常信息结构
返回值:
无
异常类型:
无
\*****************************************************************************/
CExceptionByJade(DWORD dwCode,std::string szFun,LPEXCEPTION_POINTERS lpException);
/*****************************************************************************\
函数名称:CExceptionByJade
函数功能:异常类型对象构造函数
参数:
input:
DWORD dwCode 异常信息代号
std::string szFun 异常函数名称
CComBSTR cError 错误信息描述串
返回值:
无
异常类型:
无
\*****************************************************************************/
CExceptionByJade(DWORD dwCode,std::string szFun,CComBSTR cError);
/*****************************************************************************\
函数名称:CExceptionByJade
函数功能:异常类型对象构造函数
参数:
input:
std::string szFun 异常函数名称
返回值:
无
异常类型:
无
\*****************************************************************************/
CExceptionByJade(std::string szFun);
CExceptionByJade(DWORD dwCode,std::string szFun);
CExceptionByJade(void);
~CExceptionByJade(void);
public:
/*****************************************************************************\
函数名称:DumpFile
函数功能:将异常环境最小调试信息记录文件
参数:
input:
LPCTSTR lpFile 生成的文件完全路径
BOOL bExcept 环境调试信息是否包含异常信息结构
DWORD dwOption Dump选项该参数保留
返回值:
0 —— 正常值
非0 —— 错误代号
\*****************************************************************************/
DWORD DumpFile(LPCTSTR lpFile,BOOL bExcept,DWORD dwOption = 0);
/*****************************************************************************\
函数名称:FormatMsg
函数功能:查询错误代号在该系统下的信息描述
参数:
input:
USHORT usPrimaryLang 主要语言类型代号
USHORT usSubLange 语言种类描述
output:
std::wstring &szMsg 错误代号在该系统下的字符描述
返回值:
0 —— 正常值
非0 —— 错误代号
\*****************************************************************************/
DWORD FormatMsg(std::wstring &szMsg,USHORT usPrimaryLang,USHORT usSubLang);
/*****************************************************************************\
函数名称:ErrorCode
函数功能:得到错误代号
参数:
input:
无
output:
无
返回值:
错误代号
\*****************************************************************************/
DWORD ErrorCode(void);
/*****************************************************************************\
函数名称:GetRecord
函数功能:得到异常记录
参数:
input:
EXCEPTION_RECORD *lpRecord 异常记录指针
DWORD dwLen lpRecord长度
output:
无
返回值:
0 —— 正常值
非0 —— 错误代号
\*****************************************************************************/
DWORD GetRecord(EXCEPTION_RECORD *lpRecord,DWORD dwLen);
/*****************************************************************************\
函数名称:GetFunName
函数功能:异常函数描述
参数:
input:
无
output:
std::string &szFun 异常函数描述符
返回值:
无
\*****************************************************************************/
void GetFunName(std::string &szFun);
/*****************************************************************************\
函数名称:Install
函数功能:C++异常处理转换为SEH
参数:
input:
无
output:
无
返回值:
无
异常类型:
无
\*****************************************************************************/
void Install(void) throw();
protected:
inline static void TranslatorSEH(UINT unCode,LPEXCEPTION_POINTERS lptgInfo);
private:
EXCEPTION_POINTERS m_tgException;
DWORD m_dwCode;
CComBSTR m_cError;
std::string m_szFun;
};
inline void CExceptionByJade::GetFunName(std::string &szFun)
{
szFun = m_szFun;
}
inline DWORD CExceptionByJade::ErrorCode(void)
{
return m_dwCode;
}
inline void CExceptionByJade::Install(void) throw()
{
_set_se_translator(TranslatorSEH);
}
inline void CExceptionByJade::TranslatorSEH(UINT unCode,LPEXCEPTION_POINTERS lptgInfo)
{
throw CExceptionByJade(unCode,"TranslatorSEH",lptgInfo);
}
inline DWORD CExceptionByJade::GetRecord(EXCEPTION_RECORD *lpRecord,DWORD dwLen)
{
if(IsBadWritePtr(lpRecord,sizeof(EXCEPTION_POINTERS)))
return -1;
if(IsBadReadPtr(lpRecord,sizeof(EXCEPTION_POINTERS)))
return -1;
memcpy(lpRecord,&m_tgException.ExceptionRecord,sizeof(EXCEPTION_POINTERS));
return 0;
}
#endif /*__JADE_EXCEPTION_H_H*/
/*-------------------------------------------------------------------------------\
宏功能:
依据版本进行头文件的选择
\-------------------------------------------------------------------------------*/
#if (_WIN32_WINNT>=0x0501)
#include <DbgHelp.h>
#else
#include "DbgHelp.h"
#endif
#include "ExceptionByJade.h"
#pragma comment(lib,"DbgHelp")
CExceptionByJade::CExceptionByJade(DWORD dwCode,std::string szFun,LPEXCEPTION_POINTERS lpException)
:m_dwCode(dwCode),m_szFun(szFun)
{
memcpy(&m_tgException,lpException,sizeof(EXCEPTION_POINTERS));
}
CExceptionByJade::CExceptionByJade(DWORD dwCode,std::string szFun,CComBSTR cError)
:m_dwCode(dwCode),m_szFun(szFun)
{
m_cError = cError;
ZeroMemory(&m_tgException,sizeof(EXCEPTION_POINTERS));
}
CExceptionByJade::CExceptionByJade(std::string szFun) : m_szFun(szFun)
{
ZeroMemory(&m_tgException,sizeof(EXCEPTION_POINTERS));
}
CExceptionByJade::CExceptionByJade(DWORD dwCode,std::string szFun)
: m_szFun(szFun),m_dwCode(dwCode)
{
ZeroMemory(&m_tgException,sizeof(EXCEPTION_POINTERS));
}
CExceptionByJade::CExceptionByJade(void) : m_dwCode(0)
{
ZeroMemory(&m_tgException,sizeof(EXCEPTION_POINTERS));
}
CExceptionByJade::~CExceptionByJade(void)
{
ZeroMemory(&m_tgException,sizeof(EXCEPTION_POINTERS));
}
DWORD CExceptionByJade::DumpFile(LPCTSTR lpFile,BOOL bExcept,DWORD dwOption)
{
HANDLE hThis = GetCurrentProcess();
DWORD dwId = GetCurrentProcessId();
MINIDUMP_EXCEPTION_INFORMATION tgExcpetion = { 0 };
HANDLE hFile = CreateFile(lpFile,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL
,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile == INVALID_HANDLE_VALUE)
return GetLastError();
tgExcpetion.ClientPointers = TRUE;
tgExcpetion.ThreadId = GetCurrentThreadId();
memcpy(&tgExcpetion.ExceptionPointers,&m_tgException,sizeof(EXCEPTION_POINTERS));
switch(dwOption)
{
case 0:
if(bExcept)
MiniDumpWriteDump(hThis,dwId,hFile,MiniDumpNormal,&tgExcpetion,NULL,NULL);
else
MiniDumpWriteDump(hThis,dwId,hFile,MiniDumpNormal,NULL,NULL,NULL);
break;
}
return 0;
}
DWORD CExceptionByJade::FormatMsg(std::wstring &szMsg,USHORT usPrimaryLang,USHORT usSubLang)
{
LPVOID lpMsgBuf = NULL;
DWORD dwLang = MAKELANGID(usPrimaryLang,usSubLang) , dwRetVal = 0;
dwRetVal = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
NULL,m_dwCode,dwLang,(LPTSTR) &lpMsgBuf,0,NULL);
if(!dwRetVal) return GetLastError();
szMsg = (WCHAR*)lpMsgBuf;
LocalFree(lpMsgBuf);
return 0;
}
#include <Windows.h>
#include "Dumper.h"
#include <iostream>
using namespace std;
DWORD WINAPI ThreadProc(
LPVOID lpParameter
)
{
cout << "Thread Start" << endl;
char *p = NULL;
*p = 1;
cout << "Thread Stop" << endl;
return 0;
}
class A
{
public:
int m_nStep;
int* m_pTest;
public:
A(void) : m_nStep(0),m_pTest(NULL)
{
m_pTest = new int;
};
~A(void)
{
if(m_pTest != NULL)
{
delete m_pTest;
m_pTest = NULL;
}
};
static A* Create()
{
return new A;
}
void Test(void)
{
cout << "A::Test" << endl;
}
};
int main()
{
#ifndef _DEBUG
__try
{
#endif
A *value = A::Create();
value->Test();
//char *p = NULL;
//*p = 1;
DWORD dwThreadID;
::CreateThread(NULL,0,ThreadProc,NULL,0,&dwThreadID);
EXCEPTION_INT_DIVIDE_BY_ZERO
#ifndef _DEBUG
}
__except(CDumper::OnError(GetExceptionInformation()))
{
cout << "OnError" << endl;
}
#endif
system("pause");
return 0;
}
#include "stdafx.h"
#include "Dumper.h"
class A
{
public:
int m_nStep;
int* m_pTest;
public:
A(void) : m_nStep(0),m_pTest(NULL)
{
m_pTest = new int;
};
~A(void)
{
if(m_pTest != NULL)
{
delete m_pTest;
m_pTest = NULL;
}
};
static A* Create()
{
return new A;
}
void Test(void)
{
}
};
int main()
{
#ifndef _DEBUG
__try
{
#endif
char *p = NULL;
*p = 1;
A *value = A::Create();
value->Test();
#ifndef _DEBUG
}
__except(CDumper::OnError(GetExceptionInformation()))
{
}
#endif
return 0;
}
#include "stdafx.h"
#include "Dumper.h"
class A
{
int m_nStep;
int* m_pTest;
public:
A(void) : m_nStep(0),m_pTest(NULL)
{
m_pTest = new int;
};
~A(void)
{
if(m_pTest != NULL)
{
delete m_pTest;
m_pTest = NULL;
}
};
};
int main()
{
#ifndef _DEBUG
__try
{
#endif
char *p = NULL;
*p = 1;
A value;
#ifndef _DEBUG
}
__except(CDumper::OnError(GetExceptionInformation()))
{
}
#endif
return 0;
}