VC++生成dump文件

#include <time.h>
#include <Windows.h>
#include <Dbghelp.h>

#pragma comment(lib, "Dbghelp.lib")

long   __stdcall   crush_callback(_EXCEPTION_POINTERS*   ep) 
{
	time_t t;
	struct tm *p;
	char fname[256] = {0};

	t = time(NULL) + 8 * 3600;  	//东八区
	p = gmtime(&t);  

	sprintf(fname, "dump_%d-%d-%d_%d_%d_%d.DMP", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, (p->tm_hour)%24, p->tm_min, p->tm_sec);  

	//TCHAR *pStr = A2T(fname);
	HANDLE hFile = CreateFileA(fname, 
		GENERIC_WRITE, 
		0,
		NULL,
		CREATE_ALWAYS,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		return EXCEPTION_CONTINUE_SEARCH;
	}
	MINIDUMP_EXCEPTION_INFORMATION    exceptioninfo;
	exceptioninfo.ExceptionPointers = ep;
	exceptioninfo.ThreadId          = GetCurrentThreadId();
	exceptioninfo.ClientPointers    = FALSE;

	if (!MiniDumpWriteDump(GetCurrentProcess(),
		GetCurrentProcessId(),
		hFile,
		MiniDumpWithFullMemory, //这里会将程序运行时的内存写入磁盘,文件较大,如果不需要这些及时的内存信息,用:MiniDumpNormal
		&exceptioninfo,
		NULL,
		NULL))
	{
		return EXCEPTION_CONTINUE_SEARCH;
	}
	CloseHandle(hFile);
	return EXCEPTION_EXECUTE_HANDLER;
}

int main(int argc, char* argv[])   
{   
	SetUnhandledExceptionFilter(crush_callback);   
	_asm   int   3   //只是为了让程序崩溃
	return   0;   
}

猜你喜欢

转载自aigo.iteye.com/blog/1914976