OSDN Git Service

Bump version.
[mutilities/MUtilities.git] / src / ErrorHandler_Win32.cpp
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.
14 //
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
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 //MUtils
23 #include <MUtils/ErrorHandler.h>
24 #include <MUtils/OSSupport.h>
25
26 //Win32 API
27 #define WIN32_LEAN_AND_MEAN 1
28 #include <Windows.h>
29
30 //CRT
31 #include <csignal>
32
33 ///////////////////////////////////////////////////////////////////////////////
34 // CALLBACK FUNCTIONS
35 ///////////////////////////////////////////////////////////////////////////////
36
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)
39 {
40         MUtils::OS::fatal_exit(L"Invalid parameter handler invoked, application will exit!");
41 }
42
43 // Signal handler
44 static void my_signal_handler(int signal_num)
45 {
46         signal(signal_num, my_signal_handler);
47         MUtils::OS::fatal_exit(L"Signal handler invoked, application will exit!");
48 }
49
50 // Global exception handler
51 static LONG WINAPI my_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
52 {
53         MUtils::OS::fatal_exit(L"Unhandeled exception handler invoked, application will exit!");
54         return LONG_MAX;
55 }
56
57 ///////////////////////////////////////////////////////////////////////////////
58 // DEFAULT DLL DIRECTORIES
59 ///////////////////////////////////////////////////////////////////////////////
60
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
65
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
71
72 static void set_default_dll_directories(void)
73 {
74         typedef BOOL(__stdcall *MySetDefaultDllDirectories)(const DWORD DirectoryFlags);
75         if (const HMODULE kernel32 = GetModuleHandleW(L"kernel32"))
76         {
77                 if (const MySetDefaultDllDirectories pSetDefaultDllDirectories = (MySetDefaultDllDirectories)GetProcAddress(kernel32, "SetDefaultDllDirectories"))
78                 {
79                         pSetDefaultDllDirectories(MY_LOAD_LIBRARY_FLAGS);
80                 }
81         }
82 }
83
84 ///////////////////////////////////////////////////////////////////////////////
85 // SETUP ERROR HANDLERS
86 ///////////////////////////////////////////////////////////////////////////////
87
88 void MUtils::ErrorHandler::initialize(void)
89 {
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);
94
95         /*to prevent DLL pre-loading attacks*/
96         set_default_dll_directories();
97         SetDllDirectoryW(L"");
98
99         static const int signal_num[6] = { SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM };
100
101         for(size_t i = 0; i < 6; i++)
102         {
103                 signal(signal_num[i], my_signal_handler);
104         }
105 }
106
107 ///////////////////////////////////////////////////////////////////////////////