OSDN Git Service

Bump version.
[mutilities/MUtilities.git] / src / ErrorHandler_Win32.cpp
index 7a4222e..80fb760 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // MuldeR's Utilities for Qt
-// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -19,8 +19,6 @@
 // http://www.gnu.org/licenses/lgpl-2.1.txt
 //////////////////////////////////////////////////////////////////////////////////
 
-#pragma once
-
 //MUtils
 #include <MUtils/ErrorHandler.h>
 #include <MUtils/OSSupport.h>
@@ -57,6 +55,33 @@ static LONG WINAPI my_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInf
 }
 
 ///////////////////////////////////////////////////////////////////////////////
+// DEFAULT DLL DIRECTORIES
+///////////////////////////////////////////////////////////////////////////////
+
+//Flags
+#define MY_LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x200
+#define MY_LOAD_LIBRARY_SEARCH_USER_DIRS       0x400
+#define MY_LOAD_LIBRARY_SEARCH_SYSTEM32        0x800
+
+#ifdef MUTILS_STATIC_LIB
+#define MY_LOAD_LIBRARY_FLAGS (MY_LOAD_LIBRARY_SEARCH_SYSTEM32 | MY_LOAD_LIBRARY_SEARCH_USER_DIRS)
+#else
+#define MY_LOAD_LIBRARY_FLAGS (MY_LOAD_LIBRARY_SEARCH_SYSTEM32 | MY_LOAD_LIBRARY_SEARCH_USER_DIRS | MY_LOAD_LIBRARY_SEARCH_APPLICATION_DIR)
+#endif
+
+static void set_default_dll_directories(void)
+{
+       typedef BOOL(__stdcall *MySetDefaultDllDirectories)(const DWORD DirectoryFlags);
+       if (const HMODULE kernel32 = GetModuleHandleW(L"kernel32"))
+       {
+               if (const MySetDefaultDllDirectories pSetDefaultDllDirectories = (MySetDefaultDllDirectories)GetProcAddress(kernel32, "SetDefaultDllDirectories"))
+               {
+                       pSetDefaultDllDirectories(MY_LOAD_LIBRARY_FLAGS);
+               }
+       }
+}
+
+///////////////////////////////////////////////////////////////////////////////
 // SETUP ERROR HANDLERS
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -66,7 +91,11 @@ void MUtils::ErrorHandler::initialize(void)
        SetUnhandledExceptionFilter(my_exception_handler);
        SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
        _set_invalid_parameter_handler(my_invalid_param_handler);
-       
+
+       /*to prevent DLL pre-loading attacks*/
+       set_default_dll_directories();
+       SetDllDirectoryW(L"");
+
        static const int signal_num[6] = { SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM };
 
        for(size_t i = 0; i < 6; i++)