From 148af59615aac7e5b31ca15ba7deece7e0fee347 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 1 Jul 2021 00:15:56 +0300 Subject: [PATCH] Don't check dll-s if "SetDefaultDllDirectories" is available. --- Telegram/CMakeLists.txt | 7 ++ Telegram/SourceFiles/_other/updater_win.cpp | 68 +-------------- .../SourceFiles/platform/win/specific_win.cpp | 11 +-- .../SourceFiles/platform/win/windows_dlls.cpp | 85 ++++--------------- .../SourceFiles/platform/win/windows_dlls.h | 3 - Telegram/lib_ui | 2 +- 6 files changed, 27 insertions(+), 149 deletions(-) diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 13523aa3c..d3b6c8507 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -1399,6 +1399,13 @@ if ((NOT DESKTOP_APP_DISABLE_AUTOUPDATE OR APPLE) AND NOT build_macstore AND NOT set_target_properties(Updater PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${output_folder}) if (WIN32) + get_filename_component(lib_base_loc lib_base REALPATH) + nice_target_sources(Updater ${lib_base_loc} + PRIVATE + base/platform/win/base_windows_safe_library.cpp + base/platform/win/base_windows_safe_library.h + ) + target_include_directories(Updater PRIVATE ${lib_base_loc}) if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") target_link_options(Updater PRIVATE diff --git a/Telegram/SourceFiles/_other/updater_win.cpp b/Telegram/SourceFiles/_other/updater_win.cpp index 8da2af4c8..172a71656 100644 --- a/Telegram/SourceFiles/_other/updater_win.cpp +++ b/Telegram/SourceFiles/_other/updater_win.cpp @@ -7,69 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "updater.h" -using Handle = HINSTANCE; - -Handle SafeLoadLibrary(const wchar_t *name, bool required = false) { - static const auto SystemPath = [] { - WCHAR buffer[MAX_PATH + 1] = { 0 }; - return GetSystemDirectory(buffer, MAX_PATH) - ? std::wstring(buffer) - : std::wstring(); - }(); - static const auto WindowsPath = [] { - WCHAR buffer[MAX_PATH + 1] = { 0 }; - return GetWindowsDirectory(buffer, MAX_PATH) - ? std::wstring(buffer) - : std::wstring(); - }(); - const auto tryPath = [&](const std::wstring &path) { - if (!path.empty()) { - const auto full = path + L'\\' + name; - if (const auto result = Handle(LoadLibrary(full.c_str()))) { - return result; - } - } - return Handle(); - }; - if (const auto result1 = tryPath(SystemPath)) { - return result1; - } else if (const auto result2 = tryPath(WindowsPath)) { - return result2; - } else if (required) { - const auto text = L"Could not load required DLL '" - + std::wstring(name) - + L"'!"; - MessageBox(nullptr, text.c_str(), L"Fatal Error", MB_ICONERROR); - } - return nullptr; -} - -[[nodiscard]] bool Init() { - // Remove the current directory from the DLL search order. - SetDllDirectory(L""); - - const auto required = { - L"user32.dll", - L"advapi32.dll", - L"shell32.dll", - L"ole32.dll", - L"shlwapi.dll", - L"propsys.dll", - }; - const auto optional = { - L"profapi.dll", - L"cryptbase.dll", - }; - for (const auto lib : required) { - if (!SafeLoadLibrary(lib, true)) { - return false; - } - } - for (const auto lib : optional) { - SafeLoadLibrary(lib); - } - return true; -} +#include "base/platform/win/base_windows_safe_library.h" bool _debug = false; @@ -393,9 +331,7 @@ void updateRegistry() { } int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prevInstance, LPWSTR cmdParamarg, int cmdShow) { - if (!Init()) { - return -1; - } + base::Platform::InitDynamicLibraries(); openLog(); diff --git a/Telegram/SourceFiles/platform/win/specific_win.cpp b/Telegram/SourceFiles/platform/win/specific_win.cpp index b0f9d5ac8..8d9417827 100644 --- a/Telegram/SourceFiles/platform/win/specific_win.cpp +++ b/Telegram/SourceFiles/platform/win/specific_win.cpp @@ -75,14 +75,6 @@ bool finished = true; QMargins simpleMargins, margins; HICON bigIcon = 0, smallIcon = 0, overlayIcon = 0; -class _PsInitializer { -public: - _PsInitializer() { - Dlls::start(); - } -}; -_PsInitializer _psInitializer; - BOOL CALLBACK _ActivateProcess(HWND hWnd, LPARAM lParam) { uint64 &processId(*(uint64*)lParam); @@ -104,7 +96,7 @@ BOOL CALLBACK _ActivateProcess(HWND hWnd, LPARAM lParam) { return TRUE; } -} +} // namespace void psActivateProcess(uint64 pid) { if (pid) { @@ -244,7 +236,6 @@ void start() { } // namespace ThirdParty void start() { - Dlls::init(); } void finish() { diff --git a/Telegram/SourceFiles/platform/win/windows_dlls.cpp b/Telegram/SourceFiles/platform/win/windows_dlls.cpp index 9fcc078f2..a9169befd 100644 --- a/Telegram/SourceFiles/platform/win/windows_dlls.cpp +++ b/Telegram/SourceFiles/platform/win/windows_dlls.cpp @@ -16,72 +16,16 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Platform { namespace Dlls { +namespace { -using base::Platform::SafeLoadLibrary; -using base::Platform::LoadMethod; +struct SafeIniter { + SafeIniter(); +}; -void init() { - static bool inited = false; - if (inited) return; - inited = true; +SafeIniter::SafeIniter() { + base::Platform::InitDynamicLibraries(); - base::Platform::CheckDynamicLibraries(); - - // Remove the current directory from the DLL search order. - SetDllDirectory(L""); - - const auto required = { - u"secur32.dll"_q, - u"winmm.dll"_q, - u"ws2_32.dll"_q, - u"user32.dll"_q, - u"gdi32.dll"_q, - u"advapi32.dll"_q, - u"shell32.dll"_q, - u"ole32.dll"_q, - u"oleaut32.dll"_q, - u"shlwapi.dll"_q, - u"iphlpapi.dll"_q, - u"gdiplus.dll"_q, - u"version.dll"_q, - u"dwmapi.dll"_q, - u"crypt32.dll"_q, - u"bcrypt.dll"_q, - u"imm32.dll"_q, - u"netapi32.dll"_q, - u"userenv.dll"_q, - u"wtsapi32.dll"_q, - u"propsys.dll"_q, - u"psapi.dll"_q, - u"uxtheme.dll"_q, - }; - const auto optional = { - u"dbghelp.dll"_q, - u"dbgcore.dll"_q, - u"winsta.dll"_q, - u"uxtheme.dll"_q, - u"igdumdim32.dll"_q, - u"amdhdl32.dll"_q, - u"combase.dll"_q, - u"rstrtmgr.dll"_q, - u"d3d9.dll"_q, - u"d3d11.dll"_q, - u"dxgi.dll"_q, - u"profapi.dll"_q, - u"cryptbase.dll"_q, - }; - for (const auto &lib : required) { - SafeLoadLibrary(lib, true); - } - for (const auto &lib : optional) { - SafeLoadLibrary(lib); - } -} - -void start() { - init(); - - const auto LibShell32 = SafeLoadLibrary(u"shell32.dll"_q); + const auto LibShell32 = LoadLibrary(L"shell32.dll"); LOAD_SYMBOL(LibShell32, SHAssocEnumHandlers); LOAD_SYMBOL(LibShell32, SHCreateItemFromParsingName); LOAD_SYMBOL(LibShell32, SHOpenWithDialog); @@ -90,7 +34,7 @@ void start() { LOAD_SYMBOL(LibShell32, SHChangeNotify); LOAD_SYMBOL(LibShell32, SetCurrentProcessExplicitAppUserModelID); - const auto LibUxTheme = SafeLoadLibrary(u"uxtheme.dll"_q); + const auto LibUxTheme = LoadLibrary(L"uxtheme.dll"); LOAD_SYMBOL(LibUxTheme, SetWindowTheme); //if (IsWindows10OrGreater()) { // static const auto kSystemVersion = QOperatingSystemVersion::current(); @@ -108,24 +52,27 @@ void start() { // } //} - const auto LibWtsApi32 = SafeLoadLibrary(u"wtsapi32.dll"_q); + const auto LibWtsApi32 = LoadLibrary(L"wtsapi32.dll"); LOAD_SYMBOL(LibWtsApi32, WTSRegisterSessionNotification); LOAD_SYMBOL(LibWtsApi32, WTSUnRegisterSessionNotification); - const auto LibPropSys = SafeLoadLibrary(u"propsys.dll"_q); + const auto LibPropSys = LoadLibrary(L"propsys.dll"); LOAD_SYMBOL(LibPropSys, PropVariantToString); LOAD_SYMBOL(LibPropSys, PSStringFromPropertyKey); - const auto LibDwmApi = SafeLoadLibrary(u"dwmapi.dll"_q); + const auto LibDwmApi = LoadLibrary(L"dwmapi.dll"); LOAD_SYMBOL(LibDwmApi, DwmIsCompositionEnabled); LOAD_SYMBOL(LibDwmApi, DwmSetWindowAttribute); - const auto LibPsApi = SafeLoadLibrary(u"psapi.dll"_q); + const auto LibPsApi = LoadLibrary(L"psapi.dll"); LOAD_SYMBOL(LibPsApi, GetProcessMemoryInfo); - const auto LibUser32 = SafeLoadLibrary(u"user32.dll"_q); + const auto LibUser32 = LoadLibrary(L"user32.dll"); LOAD_SYMBOL(LibUser32, SetWindowCompositionAttribute); } +SafeIniter kSafeIniter; + +} // namespace } // namespace Dlls } // namespace Platform diff --git a/Telegram/SourceFiles/platform/win/windows_dlls.h b/Telegram/SourceFiles/platform/win/windows_dlls.h index e60e40676..15fab5d43 100644 --- a/Telegram/SourceFiles/platform/win/windows_dlls.h +++ b/Telegram/SourceFiles/platform/win/windows_dlls.h @@ -22,9 +22,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Platform { namespace Dlls { -void init(); -void start(); - // UXTHEME.DLL inline HRESULT(__stdcall *SetWindowTheme)( HWND hWnd, diff --git a/Telegram/lib_ui b/Telegram/lib_ui index 9255d7103..7577f063a 160000 --- a/Telegram/lib_ui +++ b/Telegram/lib_ui @@ -1 +1 @@ -Subproject commit 9255d7103857395e91c2e546edbf8eaed86da4ca +Subproject commit 7577f063a6c6075dcee5556de9e1f96d07b2a12d