|
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 #include <windows.h> |
|
7 |
|
8 // Delayed load libraries are loaded when the first symbol is used. |
|
9 // The following ensures that we load the delayed loaded libraries from the |
|
10 // system directory. |
|
11 struct AutoLoadSystemDependencies |
|
12 { |
|
13 AutoLoadSystemDependencies() |
|
14 { |
|
15 // Remove the current directory from the search path for dynamically loaded |
|
16 // DLLs as a precaution. This call has no effect for delay load DLLs. |
|
17 SetDllDirectory(L""); |
|
18 |
|
19 // The order that these are loaded matter, for example if we load something |
|
20 // that tries to load profapi.dll first, then profapi.dll would be loaded |
|
21 // wrongly from the current directory. |
|
22 static LPCWSTR delayDLLs[] = { L"profapi.dll", L"wsock32.dll", |
|
23 L"crypt32.dll", L"cryptsp.dll", |
|
24 L"cryptbase.dll", L"msasn1.dll", |
|
25 L"userenv.dll", L"secur32.dll", |
|
26 L"ws2_32.dll", L"ws2help.dll", |
|
27 L"apphelp.dll", L"bcryptprimitives.dll" }; |
|
28 |
|
29 WCHAR systemDirectory[MAX_PATH + 1] = { L'\0' }; |
|
30 // If GetSystemDirectory fails we accept that we'll load the DLLs from the |
|
31 // normal search path. |
|
32 GetSystemDirectory(systemDirectory, MAX_PATH + 1); |
|
33 size_t systemDirLen = wcslen(systemDirectory); |
|
34 |
|
35 // Make the system directory path terminate with a slash |
|
36 if (systemDirectory[systemDirLen - 1] != L'\\' && systemDirLen) { |
|
37 systemDirectory[systemDirLen] = L'\\'; |
|
38 ++systemDirLen; |
|
39 // No need to re-null terminate |
|
40 } |
|
41 |
|
42 // For each known DLL ensure it is loaded from the system32 directory |
|
43 for (size_t i = 0; i < sizeof(delayDLLs) / sizeof(delayDLLs[0]); ++i) { |
|
44 size_t fileLen = wcslen(delayDLLs[i]); |
|
45 wcsncpy(systemDirectory + systemDirLen, delayDLLs[i], |
|
46 MAX_PATH - systemDirLen); |
|
47 if (systemDirLen + fileLen <= MAX_PATH) { |
|
48 systemDirectory[systemDirLen + fileLen] = L'\0'; |
|
49 } else { |
|
50 systemDirectory[MAX_PATH] = L'\0'; |
|
51 } |
|
52 LPCWSTR fullModulePath = systemDirectory; // just for code readability |
|
53 LoadLibraryW(fullModulePath); |
|
54 } |
|
55 } |
|
56 } loadDLLs; |