实现dump文件的方法(代码)

1、获得dump文件
#include "stdafx.h"
#include <DbgHelp.h>
#pragma comment(lib, "DbgHelp.lib")
#include <string>

using namespace std;

LONG WINAPI ExceptionFilterFunc( struct _EXCEPTION_POINTERS *ExceptionInfo  )
{
 std::wstring strDumpFileName = _T("c:\\test2.dmp");
 HANDLE hFile = CreateFile(strDumpFileName.c_str(), FILE_ALL_ACCESS, 0, NULL, CREATE_ALWAYS, NULL, NULL);
 if (hFile != INVALID_HANDLE_VALUE)
 {
  DWORD dwProcess = GetCurrentProcessId();
  HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcess);
  MINIDUMP_EXCEPTION_INFORMATION exception_information;
  exception_information.ThreadId = GetCurrentThreadId();
  exception_information.ExceptionPointers = ExceptionInfo;
  exception_information.ClientPointers = TRUE;
  MiniDumpWriteDump(hProcess, dwProcess, hFile, MiniDumpNormal, &exception_information, NULL, NULL);
  CloseHandle(hFile);
 }
 return EXCEPTION_EXECUTE_HANDLER ;
}

int _tmain(int argc, _TCHAR* argv[])
{
 // SetUnhandledExceptionFilter(ExceptionFilterFunc);
 __try
 {
  char *p = NULL;
  *p ='a';
 }
 __except(ExceptionFilterFunc(GetExceptionInformation()))
 {}

 return 0;
}

 

2、定位崩溃行

获得dump文件后,可以通过WinDbg进行定位,将dump文件拖到WinDbg后,使用命令!analyze -vd 即可看到代码崩溃行

你可能感兴趣的:(实现dump文件的方法(代码))