1#include "crashdump.h"
2
3#include "detect.h"
4
5#if defined(CONF_CRASHDUMP)
6#if !defined(CONF_FAMILY_WINDOWS)
7#error crash dumping not implemented
8#else
9
10#include "log.h"
11#include "windows.h"
12
13#include <windows.h>
14
15static const char *CRASHDUMP_LIB = "exchndl.dll";
16static const char *CRASHDUMP_FN = "ExcHndlSetLogFileNameW";
17void crashdump_init_if_available(const char *log_file_path)
18{
19 HMODULE pCrashdumpLib = LoadLibraryA(CRASHDUMP_LIB);
20 if(pCrashdumpLib == nullptr)
21 {
22 const DWORD LastError = GetLastError();
23 const std::string ErrorMsg = windows_format_system_message(LastError);
24 log_error("crashdump", "failed to load crashdump library '%s' (error %ld %s)", CRASHDUMP_LIB, LastError, ErrorMsg.c_str());
25 return;
26 }
27 const std::wstring wide_log_file_path = windows_utf8_to_wide(log_file_path);
28 // Intentional
29#ifdef __MINGW32__
30#pragma GCC diagnostic push
31#pragma GCC diagnostic ignored "-Wcast-function-type"
32#endif
33 auto exception_log_file_path_func = (BOOL(APIENTRY *)(const WCHAR *))(GetProcAddress(pCrashdumpLib, CRASHDUMP_FN));
34#ifdef __MINGW32__
35#pragma GCC diagnostic pop
36#endif
37 if(exception_log_file_path_func == nullptr)
38 {
39 const DWORD LastError = GetLastError();
40 const std::string ErrorMsg = windows_format_system_message(LastError);
41 log_error("exception_handling", "could not find function '%s' in exception handling library (error %ld %s)", CRASHDUMP_FN, LastError, ErrorMsg.c_str());
42 return;
43 }
44
45 exception_log_file_path_func(wide_log_file_path.c_str());
46}
47#endif
48#else
49void crashdump_init_if_available(const char *log_file_path)
50{
51 (void)log_file_path;
52}
53#endif
54