16,471
社区成员
发帖
与我相关
我的任务
分享
LONG WINAPI MyUnhandledExceptionFilter(struct _EXCEPTION_POINTERS *pExceptionPointers)
{
SetErrorMode( SEM_NOGPFAULTERRORBOX );
//收集信息
CString strBuild;
strBuild.Format(_T("Build: %s %s"), __DATE__, __TIME__);
CString strError;
HMODULE hModule;
TCHAR szModuleName[MAX_PATH] = _T("");
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)pExceptionPointers->ExceptionRecord->ExceptionAddress, &hModule);
GetModuleFileName(hModule, szModuleName, ARRAYSIZE(szModuleName));
strError.AppendFormat(_T("%s %d , %d ,%d."), szModuleName,pExceptionPointers->ExceptionRecord->ExceptionCode, pExceptionPointers->ExceptionRecord->ExceptionFlags, pExceptionPointers->ExceptionRecord->ExceptionAddress);
//生成 mini crash dump
BOOL bMiniDumpSuccessful;
// TCHAR szPath[MAX_PATH];
TCHAR szFileName[MAX_PATH];
TCHAR* szAppName = _T("OCTOPUS");
TCHAR* szVersion = _T("v1.0");
DWORD dwBufferSize = MAX_PATH;
HANDLE hDumpFile;
SYSTEMTIME stLocalTime;
MINIDUMP_EXCEPTION_INFORMATION ExpParam;
GetLocalTime( &stLocalTime );
CString cstrName = GetAppPath();
memset(szFileName,0,MAX_PATH);
TCHAR abPath[MAX_PATH];
memset(abPath,0,MAX_PATH);
memcpy(abPath,cstrName.GetBuffer(),cstrName.GetLength());
// StringCchPrintfA( szFileName, MAX_PATH, _T("%s%s"), cstrName.GetBuffer(), szAppName );
// CreateDirectory( szFileName, NULL );
StringCchPrintfA( szFileName, MAX_PATH, _T("%04d%02d%02d-%02d%02d%02d-%ld-%ld_%s.dmp"),
stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond,
GetCurrentProcessId(), GetCurrentThreadId(),OCTOPUS_VERSION_NUM);
strcat(abPath,szFileName);
hDumpFile = CreateFile(abPath, GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);
// MINIDUMP_USER_STREAM UserStream[2];
// MINIDUMP_USER_STREAM_INFORMATION UserInfo;
// UserInfo.UserStreamCount = 1;
// UserInfo.UserStreamArray = UserStream;
// UserStream[0].Type = CommentStreamA;
// UserStream[0].BufferSize = strBuild.GetLength()*sizeof(TCHAR);
// UserStream[0].Buffer = strBuild.GetBuffer();
// UserStream[1].Type = CommentStreamA;
// UserStream[1].BufferSize = strError.GetLength()*sizeof(TCHAR);
// UserStream[1].Buffer = strError.GetBuffer();
ExpParam.ThreadId = GetCurrentThreadId();
ExpParam.ExceptionPointers = pExceptionPointers;
ExpParam.ClientPointers = TRUE;
// MINIDUMP_TYPE MiniDumpWithDataSegs = MiniDumpNormal
// | MiniDumpWithUnloadedModules
// | MiniDumpWithIndirectlyReferencedMemory
// | MiniDumpScanMemory
// | MiniDumpWithProcessThreadData
// | MiniDumpWithThreadInfo;
bMiniDumpSuccessful = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
hDumpFile, MiniDumpNormal/*MiniDumpWithPrivateReadWriteMemory*/, &ExpParam, NULL, NULL);
if(TRUE == bMiniDumpSuccessful)
{
//AfxMessageBox(_T("Please send dmp file to the developers!\n They will deal with the trouble!"));
if(IDOK==MessageBox(NULL,_T("Confirm to send the crash information to the developer for improvement?"),_T("Er...,Octopus crashed!"),MB_OKCANCEL))
{
char cUserName[255];
char errInfo[256];
memset(cUserName,0,sizeof(cUserName));
memset(errInfo,0,sizeof(errInfo));
DWORD dwcbBuffer = 255;
GetUserName(cUserName,&dwcbBuffer);
sprintf_s(errInfo,"%s%s","this dmp file is sent off by user:",cUserName);
char* attachFiles[2];
attachFiles[0]=(char*)malloc(sizeof(char)*256);
attachFiles[1]=(char*)malloc(sizeof(char)*256);
memset(attachFiles[0],0,256);
memset(attachFiles[1],0,256);
strcpy(attachFiles[0],abPath);
int res=sendmail(errInfo,attachFiles,1);
if(0 == res)
{
AfxMessageBox(_T("Send dmp File Successfully!"));
}
else
{
AfxMessageBox(_T("Send dmp File Failed!"));
}
}
}
else
{
int id;
id = GetLastError();
CString cstrId;
cstrId.Format(_T("dmp File Create Failed!Please check your memory!ID:%d"),id);
AfxMessageBox(cstrId);
}
exit(0);
return EXCEPTION_EXECUTE_HANDLER/*EXCEPTION_CONTINUE_SEARCH*/; //或者 EXCEPTION_EXECUTE_HANDLER 关闭程序
}