michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: // Delayed load libraries are loaded when the first symbol is used. michael@0: // The following ensures that we load the delayed loaded libraries from the michael@0: // system directory. michael@0: struct AutoLoadSystemDependencies michael@0: { michael@0: AutoLoadSystemDependencies() michael@0: { michael@0: // Remove the current directory from the search path for dynamically loaded michael@0: // DLLs as a precaution. This call has no effect for delay load DLLs. michael@0: SetDllDirectory(L""); michael@0: michael@0: // The order that these are loaded matter, for example if we load something michael@0: // that tries to load profapi.dll first, then profapi.dll would be loaded michael@0: // wrongly from the current directory. michael@0: static LPCWSTR delayDLLs[] = { L"profapi.dll", L"wsock32.dll", michael@0: L"crypt32.dll", L"cryptsp.dll", michael@0: L"cryptbase.dll", L"msasn1.dll", michael@0: L"userenv.dll", L"secur32.dll", michael@0: L"ws2_32.dll", L"ws2help.dll", michael@0: L"apphelp.dll", L"bcryptprimitives.dll" }; michael@0: michael@0: WCHAR systemDirectory[MAX_PATH + 1] = { L'\0' }; michael@0: // If GetSystemDirectory fails we accept that we'll load the DLLs from the michael@0: // normal search path. michael@0: GetSystemDirectory(systemDirectory, MAX_PATH + 1); michael@0: size_t systemDirLen = wcslen(systemDirectory); michael@0: michael@0: // Make the system directory path terminate with a slash michael@0: if (systemDirectory[systemDirLen - 1] != L'\\' && systemDirLen) { michael@0: systemDirectory[systemDirLen] = L'\\'; michael@0: ++systemDirLen; michael@0: // No need to re-null terminate michael@0: } michael@0: michael@0: // For each known DLL ensure it is loaded from the system32 directory michael@0: for (size_t i = 0; i < sizeof(delayDLLs) / sizeof(delayDLLs[0]); ++i) { michael@0: size_t fileLen = wcslen(delayDLLs[i]); michael@0: wcsncpy(systemDirectory + systemDirLen, delayDLLs[i], michael@0: MAX_PATH - systemDirLen); michael@0: if (systemDirLen + fileLen <= MAX_PATH) { michael@0: systemDirectory[systemDirLen + fileLen] = L'\0'; michael@0: } else { michael@0: systemDirectory[MAX_PATH] = L'\0'; michael@0: } michael@0: LPCWSTR fullModulePath = systemDirectory; // just for code readability michael@0: LoadLibraryW(fullModulePath); michael@0: } michael@0: } michael@0: } loadDLLs;