OSDN Git Service

Moved translation support into MUtilities library + make clean-up of temporary files...
[mutilities/MUtilities.git] / src / ErrorHandler_Win32.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2014 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 // SETUP ERROR HANDLERS
59 ///////////////////////////////////////////////////////////////////////////////
60
61 void MUtils::ErrorHandler::initialize(void)
62 {
63         SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
64         SetUnhandledExceptionFilter(my_exception_handler);
65         SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
66         _set_invalid_parameter_handler(my_invalid_param_handler);
67         SetDllDirectoryW(L""); /*don'tload DLL from "current" directory*/
68         
69         static const int signal_num[6] = { SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM };
70
71         for(size_t i = 0; i < 6; i++)
72         {
73                 signal(signal_num[i], my_signal_handler);
74         }
75
76 }
77
78 ///////////////////////////////////////////////////////////////////////////////