Fix Windows Crash Reporter

This commit is contained in:
ZeldaZach 2025-01-17 21:09:43 -05:00
parent 2bc71095dd
commit 8c2ae8b082
No known key found for this signature in database

View file

@ -65,26 +65,46 @@ static void CockatriceLogger(QtMsgType type, const QMessageLogContext &ctx, cons
// clang-format off // clang-format off
#include <Windows.h> #include <Windows.h>
#include <DbgHelp.h> #include <DbgHelp.h>
#include <tchar.h> #include <ShlObj.h>
#include <ctime>
#include <filesystem>
#pragma comment(lib, "DbgHelp.lib") // Link the DbgHelp library #pragma comment(lib, "DbgHelp.lib") // Link the DbgHelp library
// clang-format on // clang-format on
LONG WINAPI CockatriceUnhandledExceptionFilter(EXCEPTION_POINTERS *pExceptionPointers) LONG WINAPI CockatriceUnhandledExceptionFilter(EXCEPTION_POINTERS *exceptionPointers)
{ {
HANDLE hDumpFile = std::filesystem::path path;
CreateFile(_T("cockatrice.crash.dmp"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDumpFile != INVALID_HANDLE_VALUE) { // Find %LOCALAPPDATA% (or cheat at finding it)
MINIDUMP_EXCEPTION_INFORMATION dumpInfo; wchar_t *localAppDataFolder;
dumpInfo.ExceptionPointers = pExceptionPointers; if (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, NULL, &localAppDataFolder) != S_OK) {
dumpInfo.ThreadId = GetCurrentThreadId(); path = std::filesystem::temp_directory_path().parent_path().parent_path();
dumpInfo.ClientPointers = TRUE; } else {
path = std::filesystem::path(localAppDataFolder);
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpWithFullMemory, &dumpInfo,
NULL, NULL);
CloseHandle(hDumpFile);
} }
// 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; return EXCEPTION_EXECUTE_HANDLER;
} }
#endif #endif