1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2016 - 2017, Steve Holme, <steve_holme@hotmail.com>.
9 * Copyright (C) 2017, Expat development team
11 * All rights reserved.
12 * Licensed under the MIT license:
14 * Permission to use, copy, modify, and distribute this software for any
15 * purpose with or without fee is hereby granted, provided that the above
16 * copyright notice and this permission notice appear in all copies.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
21 * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
24 * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Except as contained in this notice, the name of a copyright holder shall
27 * not be used in advertising or otherwise to promote the sale, use or other
28 * dealings in this Software without prior written authorization of the
31 ***************************************************************************/
39 HMODULE _Expat_LoadLibrary(LPCTSTR filename);
42 #if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
43 #define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008
46 #if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
47 #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
50 /* We use our own typedef here since some headers might lack these */
51 typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
53 /* See function definitions in winbase.h */
56 # define LOADLIBARYEX L"LoadLibraryExW"
58 # define LOADLIBARYEX "LoadLibraryExW"
61 # define LOADLIBARYEX "LoadLibraryExA"
66 * _Expat_LoadLibrary()
68 * This is used to dynamically load DLLs using the most secure method available
69 * for the version of Windows that we are running on.
73 * filename [in] - The filename or full path of the DLL to load. If only the
74 * filename is passed then the DLL will be loaded from the
75 * Windows system directory.
77 * Returns the handle of the module on success; otherwise NULL.
79 HMODULE _Expat_LoadLibrary(LPCTSTR filename)
81 HMODULE hModule = NULL;
82 LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
84 /* Get a handle to kernel32 so we can access it's functions at runtime */
85 HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
87 return NULL; /* LCOV_EXCL_LINE */
89 /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
91 pLoadLibraryEx = (LOADLIBRARYEX_FN) GetProcAddress(hKernel32, LOADLIBARYEX);
93 /* Detect if there's already a path in the filename and load the library if
94 there is. Note: Both back slashes and forward slashes have been supported
95 since the earlier days of DOS at an API level although they are not
96 supported by command prompt */
97 if(_tcspbrk(filename, TEXT("\\/"))) {
98 /** !checksrc! disable BANNEDFUNC 1 **/
99 hModule = pLoadLibraryEx ?
100 pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
101 LoadLibrary(filename);
103 /* Detect if KB2533623 is installed, as LOAD_LIBARY_SEARCH_SYSTEM32 is only
104 supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
105 Server 2008 R2 with this patch or natively on Windows 8 and above */
106 else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
107 /* Load the DLL from the Windows system directory */
108 hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
111 /* Attempt to get the Windows system path */
112 UINT systemdirlen = GetSystemDirectory(NULL, 0);
114 /* Allocate space for the full DLL path (Room for the null terminator
115 is included in systemdirlen) */
116 size_t filenamelen = _tcslen(filename);
117 TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
118 if(path && GetSystemDirectory(path, systemdirlen)) {
119 /* Calculate the full DLL path */
120 _tcscpy(path + _tcslen(path), TEXT("\\"));
121 _tcscpy(path + _tcslen(path), filename);
123 /* Load the DLL from the Windows system directory */
124 /** !checksrc! disable BANNEDFUNC 1 **/
125 hModule = pLoadLibraryEx ?
126 pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
137 #else /* defined(_WIN32) */
139 /* ISO C requires a translation unit to contain at least one declaration
140 [-Wempty-translation-unit] */
141 typedef int _TRANSLATION_UNIT_LOAD_LIBRARY_C_NOT_EMTPY;
143 #endif /* defined(_WIN32) */