#if defined WIN32 || defined __WINDOWS__
#if defined _MSC_VER || defined __USE_MINIDUMP__
long ExceptionHandler::MiniDumpExceptionHandler(struct _EXCEPTION_POINTERS *pExceptionInfo)
{
HMODULE hDll = NULL;
char szAppPath[_MAX_PATH];
std::string strAppDirectory;
GetModuleFileName(NULL, szAppPath, _MAX_PATH);
strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.substr(0, strAppDirectory.rfind("\\"));
if(strAppDirectory.rfind('\\') != strAppDirectory.size()){
strAppDirectory += '\\';
}
std::string strFileNameDbgHelp = strAppDirectory + "DBGHELP.DLL";
hDll = ::LoadLibrary(strFileNameDbgHelp.c_str());
if(!hDll){
// load any version we can
hDll = ::LoadLibrary("DBGHELP.DLL");
}
if(!hDll){
std::cout << "Could not generate report - DBGHELP.DLL could not be found." << std::endl;
return EXCEPTION_CONTINUE_SEARCH;
}
MINIDUMPWRITEDUMP pDump = (MINIDUMPWRITEDUMP)::GetProcAddress( hDll, "MiniDumpWriteDump");
if(!pDump){
std::cout << "Could not generate report - DBGHELP.DLL is to old." << std::endl;
return EXCEPTION_CONTINUE_SEARCH;
}
SYSTEMTIME stLocalTime;
GetLocalTime(&stLocalTime);
char dumpfile[250] = {'\0'};
sprintf(dumpfile, "%04d-%02d-%02d_%02d%02d%02d.dmp",
stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond);
std::string strFileNameDump = strAppDirectory + dumpfile;
HANDLE hFile = ::CreateFile(strFileNameDump.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL );
if(hFile == INVALID_HANDLE_VALUE){
std::cout << "Could not create memory dump file." << std::endl;
return EXCEPTION_EXECUTE_HANDLER;
}
_MINIDUMP_EXCEPTION_INFORMATION ExInfo;
ExInfo.ThreadId = ::GetCurrentThreadId();
ExInfo.ExceptionPointers = pExceptionInfo;
ExInfo.ClientPointers = NULL;
std::cout << "Generating minidump file... " << dumpfile << std::endl;
BOOL bOK = pDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL );
if(!bOK){
std::cout << "Could not dump memory to file." << std::endl;
::CloseHandle(hFile);
return EXCEPTION_CONTINUE_SEARCH;
}
::CloseHandle(hFile);
return EXCEPTION_EXECUTE_HANDLER;
}