Files
2026-05-20 03:08:08 +09:00

115 lines
3.3 KiB
C

// DebugAssist.h: interface for the DebugAssist class.
//
//////////////////////////////////////////////////////////////////////
#if defined(_WIN32) && !defined(_DEBUG)
#ifndef DEBUGASSIST_H__
#define DEBUGASSIST_H__
#pragma once
#include <DbgHelp.h> // in Platform SDK
//#include <c:\Program Files\Microsoft SDKs\Windows\v5.0\Include\DbgHelp.h>
//or #include <c:\Program Files (x86)\Microsoft SDKs\Windows\v5.0\Include\DbgHelp.h>
//or #include <c:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\DbgHelp.h>
//or #include <c:\Program Files (x86)\Microsoft Visual Studio 8\VC\PlatformSDK\Include\DbgHelp.h>
#include <tchar.h>
TCHAR g_szDbgModulePathName[2048] = {0,};
TCHAR g_szDumpPathName[2048] = {0,};
LONG TopExceptionFilter(LPEXCEPTION_POINTERS pException)
{
HMODULE hDll = LoadLibrary(g_szDbgModulePathName);
if (hDll == NULL)
{
OutputDebugString(TEXT("tps, ERROR, Can't found DbgHelp.dll file.\n"));
return EXCEPTION_EXECUTE_HANDLER;
}
MINIDUMP_EXCEPTION_INFORMATION exceptionInfo;
exceptionInfo.ThreadId = GetCurrentThreadId();
exceptionInfo.ExceptionPointers = pException;
exceptionInfo.ClientPointers = NULL;
BOOL (WINAPI * fnWriteDump)(HANDLE, DWORD, ...);
fnWriteDump = (BOOL (WINAPI *)(HANDLE, DWORD, ...))GetProcAddress(hDll, "MiniDumpWriteDump");
if (fnWriteDump == NULL)
{
OutputDebugString(TEXT("tps, ERROR, Can't found MiniDumpWriteDump interface.\n"));
return EXCEPTION_EXECUTE_HANDLER;
}
HANDLE hFile = CreateFile(g_szDumpPathName, GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
fnWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile,
MiniDumpNormal, &exceptionInfo, NULL, NULL);
FreeLibrary(hDll);
CloseHandle(hFile);
return EXCEPTION_EXECUTE_HANDLER;
}
void InitialDumpWriting(LPCTSTR pszDir = NULL, LPCTSTR pszPrefix = NULL)
{
TCHAR szPostFix[32];
SYSTEMTIME systemTime;
GetLocalTime(&systemTime);
_stprintf(szPostFix, TEXT("_%.4d%.2d%.2d%.2d%.2d%.2d.dmp"), systemTime.wYear,
systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute, systemTime.wSecond);
if (pszDir && pszPrefix)
{
// pszDir ex) "C:\\Temp\\"
_tcscpy(g_szDbgModulePathName, pszDir);
_tcscat(g_szDbgModulePathName, TEXT("dbghelp.dll"));
_tcscpy(g_szDumpPathName, pszDir);
_tcscat(g_szDumpPathName, pszPrefix);
_tcscat(g_szDumpPathName, szPostFix);
}
else
{
char szModuleFileName[2048];
GetModuleFileName(NULL, szModuleFileName, sizeof(szModuleFileName));
char szDrive[_MAX_DRIVE], szDir[1024], szFileName[1024], szExt[1024];
_splitpath_s(szModuleFileName, szDrive, _MAX_DRIVE, szDir, 1024, szFileName, 1024, szExt, 1024);
if (pszDir)
{
_tcscpy(g_szDbgModulePathName, pszDir);
_tcscpy(g_szDumpPathName, pszDir);
}
else
{
_tcscpy(g_szDbgModulePathName, szDrive);
_tcscat(g_szDbgModulePathName, szDir);
_tcscpy(g_szDumpPathName, g_szDbgModulePathName);
}
_tcscat(g_szDbgModulePathName, TEXT("dbghelp.dll"));
if (pszPrefix)
{
_tcscat(g_szDumpPathName, pszPrefix);
_tcscat(g_szDumpPathName, szPostFix);
}
else
{
_tcscat(g_szDumpPathName, szFileName);
_tcscat(g_szDumpPathName, szPostFix);
}
}
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)TopExceptionFilter);
}
#endif // DEBUGASSIST_H__
#endif // defined(_WIN32) && !defined(_DEBUG)