Added support for Visual Studio 2019.6 (v16.6).
[MUtilities.git] / src / ErrorHandler_Win32.cpp
blob6969496c06985529ce45eb145d8ed7279c7c263c
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
22 //MUtils
23 #include <MUtils/ErrorHandler.h>
24 #include <MUtils/OSSupport.h>
26 //Win32 API
27 #define WIN32_LEAN_AND_MEAN 1
28 #include <Windows.h>
30 //CRT
31 #include <csignal>
33 ///////////////////////////////////////////////////////////////////////////////
34 // CALLBACK FUNCTIONS
35 ///////////////////////////////////////////////////////////////////////////////
37 // Invalid parameters handler
38 static void my_invalid_param_handler(const wchar_t* exp, const wchar_t* fun, const wchar_t* fil, unsigned int, uintptr_t)
40 MUtils::OS::fatal_exit(L"Invalid parameter handler invoked, application will exit!");
43 // Signal handler
44 static void my_signal_handler(int signal_num)
46 signal(signal_num, my_signal_handler);
47 MUtils::OS::fatal_exit(L"Signal handler invoked, application will exit!");
50 // Global exception handler
51 static LONG WINAPI my_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
53 MUtils::OS::fatal_exit(L"Unhandeled exception handler invoked, application will exit!");
54 return LONG_MAX;
57 ///////////////////////////////////////////////////////////////////////////////
58 // DEFAULT DLL DIRECTORIES
59 ///////////////////////////////////////////////////////////////////////////////
61 //Flags
62 #define MY_LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x200
63 #define MY_LOAD_LIBRARY_SEARCH_USER_DIRS 0x400
64 #define MY_LOAD_LIBRARY_SEARCH_SYSTEM32 0x800
66 #ifdef MUTILS_STATIC_LIB
67 #define MY_LOAD_LIBRARY_FLAGS (MY_LOAD_LIBRARY_SEARCH_SYSTEM32 | MY_LOAD_LIBRARY_SEARCH_USER_DIRS)
68 #else
69 #define MY_LOAD_LIBRARY_FLAGS (MY_LOAD_LIBRARY_SEARCH_SYSTEM32 | MY_LOAD_LIBRARY_SEARCH_USER_DIRS | MY_LOAD_LIBRARY_SEARCH_APPLICATION_DIR)
70 #endif
72 static void set_default_dll_directories(void)
74 typedef BOOL(__stdcall *MySetDefaultDllDirectories)(const DWORD DirectoryFlags);
75 if (const HMODULE kernel32 = GetModuleHandleW(L"kernel32"))
77 if (const MySetDefaultDllDirectories pSetDefaultDllDirectories = (MySetDefaultDllDirectories)GetProcAddress(kernel32, "SetDefaultDllDirectories"))
79 pSetDefaultDllDirectories(MY_LOAD_LIBRARY_FLAGS);
84 ///////////////////////////////////////////////////////////////////////////////
85 // SETUP ERROR HANDLERS
86 ///////////////////////////////////////////////////////////////////////////////
88 void MUtils::ErrorHandler::initialize(void)
90 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
91 SetUnhandledExceptionFilter(my_exception_handler);
92 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
93 _set_invalid_parameter_handler(my_invalid_param_handler);
95 /*to prevent DLL pre-loading attacks*/
96 set_default_dll_directories();
97 SetDllDirectoryW(L"");
99 static const int signal_num[6] = { SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM };
101 for(size_t i = 0; i < 6; i++)
103 signal(signal_num[i], my_signal_handler);
107 ///////////////////////////////////////////////////////////////////////////////