From 9a9b97998c82a3de5d3ac2ddb22ebff66eec5bfd Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 28 May 2021 00:12:54 +0200 Subject: [PATCH] Fix loading native dependencies for binary modules from other folders than engine binaries or project root --- .../Platform/Windows/WindowsPlatform.cpp | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Platform/Windows/WindowsPlatform.cpp b/Source/Engine/Platform/Windows/WindowsPlatform.cpp index 00a5c0d0f..2086cd8dc 100644 --- a/Source/Engine/Platform/Windows/WindowsPlatform.cpp +++ b/Source/Engine/Platform/Windows/WindowsPlatform.cpp @@ -4,6 +4,7 @@ #include "Engine/Platform/Platform.h" #include "Engine/Platform/Window.h" +#include "Engine/Platform/FileSystem.h" #include "Engine/Platform/CreateWindowSettings.h" #include "Engine/Platform/WindowsManager.h" #include "Engine/Platform/MemoryStats.h" @@ -1118,6 +1119,19 @@ void* WindowsPlatform::LoadLibrary(const Char* filename) { ASSERT(filename); + // Add folder to search path to load dependency libraries + StringView folder = StringUtils::GetDirectoryName(filename); + if (folder.HasChars() && FileSystem::IsRelative(folder)) + folder = StringView::Empty; + if (folder.HasChars()) + { + Char& end = ((Char*)folder.Get())[folder.Length()]; + const Char c = end; + end = 0; + SetDllDirectoryW(*folder); + end = c; + } + // Avoiding windows dialog boxes if missing const DWORD errorMode = SEM_NOOPENFILEERRORBOX; DWORD prevErrorMode = 0; @@ -1134,12 +1148,15 @@ void* WindowsPlatform::LoadLibrary(const Char* filename) { SetThreadErrorMode(prevErrorMode, nullptr); } + if (folder.HasChars()) + { + SetDllDirectoryW(nullptr); + } #if CRASH_LOG_ENABLE // Refresh modules info during next stack trace collecting to have valid debug symbols information SymLocker.Lock(); - const auto folder = StringUtils::GetDirectoryName(filename); - if (!SymbolsPath.Contains(folder)) + if (folder.HasChars() && !SymbolsPath.Contains(folder)) { SymbolsPath.Add(folder); OnSymbolsPathModified();