Use a safer way to load system libraries.

This commit is contained in:
John Preston 2020-07-02 21:01:25 +04:00
parent 822c0434e8
commit 7f55fd2cad
5 changed files with 58 additions and 58 deletions

View file

@ -7,26 +7,43 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "platform/win/windows_dlls.h"
#include "base/platform/win/base_windows_safe_library.h"
#include <VersionHelpers.h>
#include <QtCore/QSysInfo>
namespace Platform {
namespace Dlls {
f_SetDllDirectory SetDllDirectory;
HINSTANCE LibKernel32;
using base::Platform::SafeLoadLibrary;
using base::Platform::LoadMethod;
void init() {
static bool inited = false;
if (inited) return;
inited = true;
LibKernel32 = LoadLibrary(L"KERNEL32.DLL");
load(LibKernel32, "SetDllDirectoryW", SetDllDirectory);
if (SetDllDirectory) {
// Remove the current directory from the DLL search order.
SetDllDirectory(L"");
// Remove the current directory from the DLL search order.
::SetDllDirectory(L"");
const auto list = {
u"dbghelp.dll"_q,
u"dbgcore.dll"_q,
u"propsys.dll"_q,
u"winsta.dll"_q,
u"textinputframework.dll"_q,
u"uxtheme.dll"_q,
u"igdumdim32.dll"_q,
u"amdhdl32.dll"_q,
u"wtsapi32.dll"_q,
u"propsys.dll"_q,
u"combase.dll"_q,
u"dwmapi.dll"_q,
u"rstrtmgr.dll"_q,
u"psapi.dll"_q,
};
for (const auto &lib : list) {
SafeLoadLibrary(lib);
}
}
@ -53,59 +70,50 @@ f_RmShutdown RmShutdown;
f_RmEndSession RmEndSession;
f_GetProcessMemoryInfo GetProcessMemoryInfo;
HINSTANCE LibUxTheme;
HINSTANCE LibShell32;
HINSTANCE LibWtsApi32;
HINSTANCE LibPropSys;
HINSTANCE LibComBase;
HINSTANCE LibDwmApi;
HINSTANCE LibRstrtMgr;
HINSTANCE LibPsApi;
void start() {
init();
LibShell32 = LoadLibrary(L"SHELL32.DLL");
load(LibShell32, "SHAssocEnumHandlers", SHAssocEnumHandlers);
load(LibShell32, "SHCreateItemFromParsingName", SHCreateItemFromParsingName);
load(LibShell32, "SHOpenWithDialog", SHOpenWithDialog);
load(LibShell32, "OpenAs_RunDLLW", OpenAs_RunDLL);
load(LibShell32, "SHQueryUserNotificationState", SHQueryUserNotificationState);
load(LibShell32, "SHChangeNotify", SHChangeNotify);
load(LibShell32, "SetCurrentProcessExplicitAppUserModelID", SetCurrentProcessExplicitAppUserModelID);
const auto LibShell32 = SafeLoadLibrary(u"shell32.dll"_q);
LoadMethod(LibShell32, "SHAssocEnumHandlers", SHAssocEnumHandlers);
LoadMethod(LibShell32, "SHCreateItemFromParsingName", SHCreateItemFromParsingName);
LoadMethod(LibShell32, "SHOpenWithDialog", SHOpenWithDialog);
LoadMethod(LibShell32, "OpenAs_RunDLLW", OpenAs_RunDLL);
LoadMethod(LibShell32, "SHQueryUserNotificationState", SHQueryUserNotificationState);
LoadMethod(LibShell32, "SHChangeNotify", SHChangeNotify);
LoadMethod(LibShell32, "SetCurrentProcessExplicitAppUserModelID", SetCurrentProcessExplicitAppUserModelID);
LibUxTheme = LoadLibrary(L"UXTHEME.DLL");
load(LibUxTheme, "SetWindowTheme", SetWindowTheme);
const auto LibUxTheme = SafeLoadLibrary(u"uxtheme.dll"_q);
LoadMethod(LibUxTheme, "SetWindowTheme", SetWindowTheme);
if (IsWindowsVistaOrGreater()) {
LibWtsApi32 = LoadLibrary(L"WTSAPI32.DLL");
load(LibWtsApi32, "WTSRegisterSessionNotification", WTSRegisterSessionNotification);
load(LibWtsApi32, "WTSUnRegisterSessionNotification", WTSUnRegisterSessionNotification);
const auto LibWtsApi32 = SafeLoadLibrary(u"wtsapi32.dll"_q);
LoadMethod(LibWtsApi32, "WTSRegisterSessionNotification", WTSRegisterSessionNotification);
LoadMethod(LibWtsApi32, "WTSUnRegisterSessionNotification", WTSUnRegisterSessionNotification);
LibPropSys = LoadLibrary(L"PROPSYS.DLL");
load(LibPropSys, "PropVariantToString", PropVariantToString);
load(LibPropSys, "PSStringFromPropertyKey", PSStringFromPropertyKey);
const auto LibPropSys = SafeLoadLibrary(u"propsys.dll"_q);
LoadMethod(LibPropSys, "PropVariantToString", PropVariantToString);
LoadMethod(LibPropSys, "PSStringFromPropertyKey", PSStringFromPropertyKey);
if (IsWindows8OrGreater()) {
LibComBase = LoadLibrary(L"COMBASE.DLL");
load(LibComBase, "RoGetActivationFactory", RoGetActivationFactory);
load(LibComBase, "WindowsCreateStringReference", WindowsCreateStringReference);
load(LibComBase, "WindowsDeleteString", WindowsDeleteString);
const auto LibComBase = SafeLoadLibrary(u"combase.dll"_q);
LoadMethod(LibComBase, "RoGetActivationFactory", RoGetActivationFactory);
LoadMethod(LibComBase, "WindowsCreateStringReference", WindowsCreateStringReference);
LoadMethod(LibComBase, "WindowsDeleteString", WindowsDeleteString);
}
LibDwmApi = LoadLibrary(L"DWMAPI.DLL");
load(LibDwmApi, "DwmIsCompositionEnabled", DwmIsCompositionEnabled);
const auto LibDwmApi = SafeLoadLibrary(u"dwmapi.dll"_q);
LoadMethod(LibDwmApi, "DwmIsCompositionEnabled", DwmIsCompositionEnabled);
LibRstrtMgr = LoadLibrary(L"RSTRTMGR.DLL");
load(LibRstrtMgr, "RmStartSession", RmStartSession);
load(LibRstrtMgr, "RmRegisterResources", RmRegisterResources);
load(LibRstrtMgr, "RmGetList", RmGetList);
load(LibRstrtMgr, "RmShutdown", RmShutdown);
load(LibRstrtMgr, "RmEndSession", RmEndSession);
const auto LibRstrtMgr = SafeLoadLibrary(u"rstrtmgr.dll"_q);
LoadMethod(LibRstrtMgr, "RmStartSession", RmStartSession);
LoadMethod(LibRstrtMgr, "RmRegisterResources", RmRegisterResources);
LoadMethod(LibRstrtMgr, "RmGetList", RmGetList);
LoadMethod(LibRstrtMgr, "RmShutdown", RmShutdown);
LoadMethod(LibRstrtMgr, "RmEndSession", RmEndSession);
}
LibPsApi = LoadLibrary(L"PSAPI.DLL");
load(LibPsApi, "GetProcessMemoryInfo", GetProcessMemoryInfo);
const auto LibPsApi = SafeLoadLibrary(u"psapi.dll"_q);
LoadMethod(LibPsApi, "GetProcessMemoryInfo", GetProcessMemoryInfo);
}
} // namespace Dlls

View file

@ -30,14 +30,6 @@ extern f_SetDllDirectory SetDllDirectory;
void start();
template <typename Function>
bool load(HINSTANCE library, LPCSTR name, Function &func) {
if (!library) return false;
func = reinterpret_cast<Function>(GetProcAddress(library, name));
return (func != nullptr);
}
// UXTHEME.DLL
using f_SetWindowTheme = HRESULT(FAR STDAPICALLTYPE*)(
HWND hWnd,

@ -1 +1 @@
Subproject commit 3017da83c15e5e27244ab66526fea8cc3bddb7cf
Subproject commit 01ca681ab3aecda8c372b1bb77999f3b7b7a52c6

@ -1 +1 @@
Subproject commit 262b3eb33593c638c46a30423c3774d9fe8171fc
Subproject commit 09918f0133ce0070000b9ca2a798057e6ad8bd5d

2
cmake

@ -1 +1 @@
Subproject commit 6b36a649dc0061138cf60bac65d9a40a5c9faea3
Subproject commit 9346d5f89510c4f9e340a0e26a1f8c2244e7de29