主要应用到的函数:
SetUnhandledExceptionFilter(
__in LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter
);
该函数的作用是获取系统中没有捕获的异常,也就是说,有些异常是通过try{}catch进行扑捉的,但是,对于有异常,但是没有扑捉的系统直接会用这个函数来处理了。
设置没有处理异常捕捉:SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
LONG WINAPI MyUnhandledExceptionFilter(struct _EXCEPTION_POINTERS *pExceptionPointers)
{
SetErrorMode( SEM_NOOPENFILEERRORBOX );
CString strBuild;
strBuild.Format("Build:%s, %s", __DATE__, __TIME__);
CString strError;
HMODULE hModule;
CHAR szModuleName[MAX_PATH] = "";
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCSTR)pExceptionPointers->ExceptionRecord->ExceptionAddress, &hModule),
GetModuleFileName(hModule, szModuleName, ARRAYSIZE(szModuleName)),
strError.Format("%s %d , %d ,%d.", szModuleName,pExceptionPointers->ExceptionRecord->ExceptionCode, pExceptionPointers->ExceptionRecord->ExceptionFlags, pExceptionPointers->ExceptionRecord->ExceptionAddress);
BOOL bMiniDumpSuccessful;
CHAR szPath[MAX_PATH];
CHAR szFileName[MAX_PATH];
CHAR* szAppName = "AppName";
CHAR* szVersion = "v1.0";
DWORD dwBufferSize = MAX_PATH;
HANDLE hDumpFile;
SYSTEMTIME stLocalTime;
MINIDUMP_EXCEPTION_INFORMATION ExpParam;
GetLocalTime( &stLocalTime );
GetTempPath( dwBufferSize, szPath );
sprintf(szFileName, "%s%s", szPath, szAppName );
CreateDirectory( szFileName, NULL );
sprintf(szFileName, "%s%s\\%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp",
szPath, szAppName, szVersion,
stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond,
GetCurrentProcessId(), GetCurrentThreadId());
//收集息
// MessageBox
hDumpFile = CreateFile("D:\\aa.dmp", 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 = CommentStreamW;
UserStream[0].BufferSize = strBuild.GetLength()*sizeof(WCHAR);
UserStream[0].Buffer = strBuild.GetBuffer();
UserStream[1].Type = CommentStreamW;
UserStream[1].BufferSize = strError.GetLength()*sizeof(WCHAR);
UserStream[1].Buffer = strError.GetBuffer();
ExpParam.ThreadId = GetCurrentThreadId();
ExpParam.ExceptionPointers = pExceptionPointers;
ExpParam.ClientPointers = TRUE;
MINIDUMP_TYPE MiniDumpWithDataSegs = (MINIDUMP_TYPE)(MiniDumpNormal
| MiniDumpWithHandleData
| MiniDumpWithUnloadedModules
| MiniDumpWithIndirectlyReferencedMemory
| MiniDumpScanMemory
| MiniDumpWithProcessThreadData);
bMiniDumpSuccessful = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
hDumpFile,//写入的文件句柄
MiniDumpWithDataSegs, &ExpParam, NULL, NULL);
这时候会将文件dmp文件写入到hDumpFile中去,如果你现在需要上传文件什么的,可以传送hDumpFile文件。
return 1;
}

本文介绍如何使用SetUnhandledExceptionFilter函数捕获未处理的异常,并通过MiniDumpWriteDump生成内存转储文件(.dmp),以便于进一步的错误诊断。
2万+

被折叠的 条评论
为什么被折叠?



