在程序执行过程中不免会遇到崩溃问题:
创建except.h
#ifndef _except_inc__
#define _except_inc__
#pragma once
#include <DbgHelp.h>
#include <Shlwapi.h>
#pragma comment(lib, "dbghelp.lib")
#pragma comment(lib, "shlwapi.lib")
#ifndef CRASH_DUMP_FILE
#define CRASH_DUMP_FILE _T("Crash.dmp")
#endif
static void DumpMiniDump(HANDLE hFile, PEXCEPTION_POINTERS excpInfo)
{
if (excpInfo == NULL)
{
__try
{
RaiseException(EXCEPTION_BREAKPOINT, 0, 0, NULL);
}
__except(DumpMiniDump(hFile, GetExceptionInformation()),
EXCEPTION_CONTINUE_EXECUTION)
{
}
}
else
{
MINIDUMP_EXCEPTION_INFORMATION eInfo;
eInfo.ThreadId = GetCurrentThreadId();
eInfo.ExceptionPointers = excpInfo;
eInfo.ClientPointers = FALSE;
MiniDumpWriteDump(
GetCurrentProcess(),
GetCurrentProcessId(),
hFile,
MiniDumpWithFullMemory,
excpInfo ? &eInfo : NULL,
NULL,
NULL);
}
}
static LONG WINAPI Local_UnhandledExceptionFilter(PEXCEPTION_POINTERS pExcept)
{
TCHAR szModuleName[MAX_PATH] = {0};
GetModuleFileName(NULL, szModuleName, MAX_PATH);
PathRemoveFileSpec(szModuleName);
PathAddBackslash(szModuleName);
lstrcat(szModuleName, _T("Dump\\"));
CreateDirectory(szModuleName, NULL);
lstrcat(szModuleName, CRASH_DUMP_FILE);
HANDLE hFile = CreateFile(szModuleName,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
DumpMiniDump(hFile, pExcept);
CloseHandle(hFile);
}
return 0;
}
#endif
然后再dllmain.cpp的主函数中添加SetUnhandledExceptionFilter(::Local_UnhandledExceptionFilter);即可;
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "stdafx.h"
#define CRASH_DUMP_FILE _T("DUMP.dmp")
#include "..\..\include\except.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
SetUnhandledExceptionFilter(::Local_UnhandledExceptionFilter);
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}