LibreOffice Module onlineupdate (master) 1
loaddlls.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
7#ifdef _WIN32
8#ifndef UNICODE
9#define UNICODE
10#endif
11#include <windows.h>
12
13// Delayed load libraries are loaded when the first symbol is used.
14// The following ensures that we load the delayed loaded libraries from the
15// system directory.
16struct AutoLoadSystemDependencies
17{
18 AutoLoadSystemDependencies()
19 {
20 // Remove the current directory from the search path for dynamically loaded
21 // DLLs as a precaution. This call has no effect for delay load DLLs.
22 SetDllDirectory(L"");
23
24 HMODULE module = ::GetModuleHandleW(L"kernel32.dll");
25 if (module)
26 {
27 // SetDefaultDllDirectories is always available on Windows 8 and above. It
28 // is also available on Windows Vista, Windows Server 2008, and
29 // Windows 7 when MS KB2533623 has been applied.
30 decltype(SetDefaultDllDirectories)* setDefaultDllDirectories =
31 (decltype(SetDefaultDllDirectories)*) GetProcAddress(module, "SetDefaultDllDirectories");
32 if (setDefaultDllDirectories)
33 {
34 setDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32);
35 return;
36 }
37 }
38
39 // TODO: moggi: do we need all that code?
40 // When SetDefaultDllDirectories is not available, fallback to preloading
41 // dlls. The order that these are loaded does not matter since they are
42 // loaded using the LOAD_WITH_ALTERED_SEARCH_PATH flag.
43#ifdef HAVE_64BIT_BUILD
44 // DLLs for Firefox x64 on Windows 7 (x64).
45 // Note: dwmapi.dll is preloaded since a crash will try to load it from the
46 // application's directory.
47 static LPCWSTR delayDLLs[] = { L"apphelp.dll",
48 L"cryptbase.dll",
49 L"cryptsp.dll",
50 L"dwmapi.dll",
51 L"mpr.dll",
52 L"ntmarta.dll",
53 L"profapi.dll",
54 L"propsys.dll",
55 L"sspicli.dll",
56 L"wsock32.dll"
57 };
58
59#else
60 // DLLs for Firefox x86 on Windows XP through Windows 7 (x86 and x64).
61 // Note: dwmapi.dll is preloaded since a crash will try to load it from the
62 // application's directory.
63 static LPCWSTR delayDLLs[] = { L"apphelp.dll",
64 L"crypt32.dll",
65 L"cryptbase.dll",
66 L"cryptsp.dll",
67 L"dwmapi.dll",
68 L"mpr.dll",
69 L"msasn1.dll",
70 L"ntmarta.dll",
71 L"profapi.dll",
72 L"propsys.dll",
73 L"psapi.dll",
74 L"secur32.dll",
75 L"sspicli.dll",
76 L"userenv.dll",
77 L"uxtheme.dll",
78 L"ws2_32.dll",
79 L"ws2help.dll",
80 L"wsock32.dll"
81 };
82#endif
83
84 WCHAR systemDirectory[MAX_PATH + 1] = { L'\0' };
85 // If GetSystemDirectory fails we accept that we'll load the DLLs from the
86 // normal search path.
87 GetSystemDirectoryW(systemDirectory, MAX_PATH + 1);
88 size_t systemDirLen = wcslen(systemDirectory);
89
90 // Make the system directory path terminate with a slash
91 if (systemDirectory[systemDirLen - 1] != L'\\' && systemDirLen)
92 {
93 systemDirectory[systemDirLen] = L'\\';
94 ++systemDirLen;
95 // No need to re-null terminate
96 }
97
98 // For each known DLL ensure it is loaded from the system32 directory
99 for (size_t i = 0; i < sizeof(delayDLLs) / sizeof(delayDLLs[0]); ++i)
100 {
101 size_t fileLen = wcslen(delayDLLs[i]);
102 wcsncpy(systemDirectory + systemDirLen, delayDLLs[i],
103 MAX_PATH - systemDirLen);
104 if (systemDirLen + fileLen <= MAX_PATH)
105 {
106 systemDirectory[systemDirLen + fileLen] = L'\0';
107 }
108 else
109 {
110 systemDirectory[MAX_PATH] = L'\0';
111 }
112 LPCWSTR fullModulePath = systemDirectory; // just for code readability
113 // LOAD_WITH_ALTERED_SEARCH_PATH makes a dll look in its own directory for
114 // dependencies and is only available on Win 7 and below.
115 LoadLibraryExW(fullModulePath, nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
116 }
117 }
118} loadDLLs;
119#endif
#define MAX_PATH
int i
module