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/. */
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
16 struct AutoLoadSystemDependencies
18 AutoLoadSystemDependencies()
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.
24 HMODULE module
= ::GetModuleHandleW(L
"kernel32.dll");
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
)
34 setDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32
);
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",
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",
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
);
90 // Make the system directory path terminate with a slash
91 if (systemDirectory
[systemDirLen
- 1] != L
'\\' && systemDirLen
)
93 systemDirectory
[systemDirLen
] = L
'\\';
95 // No need to re-null terminate
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
)
101 size_t fileLen
= wcslen(delayDLLs
[i
]);
102 wcsncpy(systemDirectory
+ systemDirLen
, delayDLLs
[i
],
103 MAX_PATH
- systemDirLen
);
104 if (systemDirLen
+ fileLen
<= MAX_PATH
)
106 systemDirectory
[systemDirLen
+ fileLen
] = L
'\0';
110 systemDirectory
[MAX_PATH
] = L
'\0';
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
);