From d3a1538af3ca7f559e80fc8defee5e6d90941b3c Mon Sep 17 00:00:00 2001 From: Zach H Date: Fri, 17 Jan 2025 21:23:05 -0500 Subject: [PATCH] Fix Windows Crash Reporter (#5493) * Fix Windows Crash Reporter * Fix NSIS template --- cmake/NSIS.template.in | 2 +- cockatrice/src/main.cpp | 46 +++++++++++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/cmake/NSIS.template.in b/cmake/NSIS.template.in index 2651dd72c..fbdde0b86 100644 --- a/cmake/NSIS.template.in +++ b/cmake/NSIS.template.in @@ -238,7 +238,7 @@ ${If} $PortableMode = 0 ; Enable Windows User-Mode Dumps ; https://learn.microsoft.com/en-us/windows/win32/wer/collecting-user-mode-dumps - WriteRegStr HKLM "Software\Microsoft\Windows\Windows Error Reporting\LocalDumps\cockatrice.exe" "DumpFolder" "%LOCALAPPDATA%\CrashDumps\Cockatrice" + WriteRegExpandStr HKLM "Software\Microsoft\Windows\Windows Error Reporting\LocalDumps\cockatrice.exe" "DumpFolder" "%LOCALAPPDATA%\CrashDumps\Cockatrice" WriteRegDWORD HKLM "Software\Microsoft\Windows\Windows Error Reporting\LocalDumps\cockatrice.exe" "DumpCount" "5" WriteRegDWORD HKLM "Software\Microsoft\Windows\Windows Error Reporting\LocalDumps\cockatrice.exe" "DumpType" "2" diff --git a/cockatrice/src/main.cpp b/cockatrice/src/main.cpp index f81e09fed..554d89c45 100644 --- a/cockatrice/src/main.cpp +++ b/cockatrice/src/main.cpp @@ -65,26 +65,46 @@ static void CockatriceLogger(QtMsgType type, const QMessageLogContext &ctx, cons // clang-format off #include #include -#include +#include +#include +#include #pragma comment(lib, "DbgHelp.lib") // Link the DbgHelp library // clang-format on -LONG WINAPI CockatriceUnhandledExceptionFilter(EXCEPTION_POINTERS *pExceptionPointers) +LONG WINAPI CockatriceUnhandledExceptionFilter(EXCEPTION_POINTERS *exceptionPointers) { - HANDLE hDumpFile = - CreateFile(_T("cockatrice.crash.dmp"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + std::filesystem::path path; - if (hDumpFile != INVALID_HANDLE_VALUE) { - MINIDUMP_EXCEPTION_INFORMATION dumpInfo; - dumpInfo.ExceptionPointers = pExceptionPointers; - dumpInfo.ThreadId = GetCurrentThreadId(); - dumpInfo.ClientPointers = TRUE; - - MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpWithFullMemory, &dumpInfo, - NULL, NULL); - CloseHandle(hDumpFile); + // Find %LOCALAPPDATA% (or cheat at finding it) + wchar_t *localAppDataFolder; + if (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, NULL, &localAppDataFolder) != S_OK) { + path = std::filesystem::temp_directory_path().parent_path().parent_path(); + } else { + path = std::filesystem::path(localAppDataFolder); } + // Plan on writing crash files into %LOCALAPPDATA%/CrashDumps/Cockatrice/cockatrice.crash.*.dmp + path /= "CrashDumps"; + path /= "Cockatrice"; + if (!std::filesystem::exists(path)) { + std::filesystem::create_directories(path); + } + path /= "cockatrice.crash." + std::to_string(std::time(0)) + ".dmp"; + + // Create and write crash files + HANDLE hDumpFile = + CreateFile(path.wstring().c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + + MINIDUMP_EXCEPTION_INFORMATION mei; + mei.ExceptionPointers = exceptionPointers; + mei.ThreadId = GetCurrentThreadId(); + mei.ClientPointers = 1; + + MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpWithFullMemory, &mei, nullptr, + nullptr); + + CloseHandle(hDumpFile); + return EXCEPTION_EXECUTE_HANDLER; } #endif