summaryrefslogtreecommitdiffstats
path: root/onlineupdate/source/update/updater/loaddlls.cxx
blob: 6a0c8a61ee91bc16fd85504fdb8ec24c06388860 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */


#ifdef _WIN32
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>

// Delayed load libraries are loaded when the first symbol is used.
// The following ensures that we load the delayed loaded libraries from the
// system directory.
struct AutoLoadSystemDependencies
{
    AutoLoadSystemDependencies()
    {
        // Remove the current directory from the search path for dynamically loaded
        // DLLs as a precaution.  This call has no effect for delay load DLLs.
        SetDllDirectory(L"");

        HMODULE module = ::GetModuleHandleW(L"kernel32.dll");
        if (module)
        {
            // SetDefaultDllDirectories is always available on Windows 8 and above. It
            // is also available on Windows Vista, Windows Server 2008, and
            // Windows 7 when MS KB2533623 has been applied.
            decltype(SetDefaultDllDirectories)* setDefaultDllDirectories =
                (decltype(SetDefaultDllDirectories)*) GetProcAddress(module, "SetDefaultDllDirectories");
            if (setDefaultDllDirectories)
            {
                setDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32);
                return;
            }
        }

        // TODO: moggi: do we need all that code?
        // When SetDefaultDllDirectories is not available, fallback to preloading
        // dlls. The order that these are loaded does not matter since they are
        // loaded using the LOAD_WITH_ALTERED_SEARCH_PATH flag.
#ifdef HAVE_64BIT_BUILD
        // DLLs for Firefox x64 on Windows 7 (x64).
        // Note: dwmapi.dll is preloaded since a crash will try to load it from the
        // application's directory.
        static LPCWSTR delayDLLs[] = { L"apphelp.dll",
                                       L"cryptbase.dll",
                                       L"cryptsp.dll",
                                       L"dwmapi.dll",
                                       L"mpr.dll",
                                       L"ntmarta.dll",
                                       L"profapi.dll",
                                       L"propsys.dll",
                                       L"sspicli.dll",
                                       L"wsock32.dll"
                                     };

#else
        // DLLs for Firefox x86 on Windows XP through Windows 7 (x86 and x64).
        // Note: dwmapi.dll is preloaded since a crash will try to load it from the
        // application's directory.
        static LPCWSTR delayDLLs[] = { L"apphelp.dll",
                                       L"crypt32.dll",
                                       L"cryptbase.dll",
                                       L"cryptsp.dll",
                                       L"dwmapi.dll",
                                       L"mpr.dll",
                                       L"msasn1.dll",
                                       L"ntmarta.dll",
                                       L"profapi.dll",
                                       L"propsys.dll",
                                       L"psapi.dll",
                                       L"secur32.dll",
                                       L"sspicli.dll",
                                       L"userenv.dll",
                                       L"uxtheme.dll",
                                       L"ws2_32.dll",
                                       L"ws2help.dll",
                                       L"wsock32.dll"
                                     };
#endif

        WCHAR systemDirectory[MAX_PATH + 1] = { L'\0' };
        // If GetSystemDirectory fails we accept that we'll load the DLLs from the
        // normal search path.
        GetSystemDirectoryW(systemDirectory, MAX_PATH + 1);
        size_t systemDirLen = wcslen(systemDirectory);

        // Make the system directory path terminate with a slash
        if (systemDirectory[systemDirLen - 1] != L'\\' && systemDirLen)
        {
            systemDirectory[systemDirLen] = L'\\';
            ++systemDirLen;
            // No need to re-null terminate
        }

        // For each known DLL ensure it is loaded from the system32 directory
        for (size_t i = 0; i < sizeof(delayDLLs) / sizeof(delayDLLs[0]); ++i)
        {
            size_t fileLen = wcslen(delayDLLs[i]);
            wcsncpy(systemDirectory + systemDirLen, delayDLLs[i],
                    MAX_PATH - systemDirLen);
            if (systemDirLen + fileLen <= MAX_PATH)
            {
                systemDirectory[systemDirLen + fileLen] = L'\0';
            }
            else
            {
                systemDirectory[MAX_PATH] = L'\0';
            }
            LPCWSTR fullModulePath = systemDirectory; // just for code readability
            // LOAD_WITH_ALTERED_SEARCH_PATH makes a dll look in its own directory for
            // dependencies and is only available on Win 7 and below.
            LoadLibraryExW(fullModulePath, nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
        }
    }
} loadDLLs;
#endif