loadlibrary.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2016 - 2017, Steve Holme, <[email protected]>.
  9. * Copyright (C) 2017, Expat development team
  10. *
  11. * All rights reserved.
  12. * Licensed under the MIT license:
  13. *
  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.
  17. *
  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.
  25. *
  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
  29. * copyright holder.
  30. *
  31. ***************************************************************************/
  32. #if defined(_WIN32)
  33. #include <windows.h>
  34. #include <tchar.h>
  35. HMODULE _Expat_LoadLibrary(LPCTSTR filename);
  36. #if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
  37. #define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008
  38. #endif
  39. #if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
  40. #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
  41. #endif
  42. /* We use our own typedef here since some headers might lack these */
  43. typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
  44. /* See function definitions in winbase.h */
  45. #ifdef UNICODE
  46. # ifdef _WIN32_WCE
  47. # define LOADLIBARYEX L"LoadLibraryExW"
  48. # else
  49. # define LOADLIBARYEX "LoadLibraryExW"
  50. # endif
  51. #else
  52. # define LOADLIBARYEX "LoadLibraryExA"
  53. #endif
  54. /*
  55. * _Expat_LoadLibrary()
  56. *
  57. * This is used to dynamically load DLLs using the most secure method available
  58. * for the version of Windows that we are running on.
  59. *
  60. * Parameters:
  61. *
  62. * filename [in] - The filename or full path of the DLL to load. If only the
  63. * filename is passed then the DLL will be loaded from the
  64. * Windows system directory.
  65. *
  66. * Returns the handle of the module on success; otherwise NULL.
  67. */
  68. HMODULE _Expat_LoadLibrary(LPCTSTR filename)
  69. {
  70. HMODULE hModule = NULL;
  71. LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
  72. /* Get a handle to kernel32 so we can access it's functions at runtime */
  73. HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
  74. if(!hKernel32)
  75. return NULL; /* LCOV_EXCL_LINE */
  76. /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
  77. and above */
  78. pLoadLibraryEx = (LOADLIBRARYEX_FN) GetProcAddress(hKernel32, LOADLIBARYEX);
  79. /* Detect if there's already a path in the filename and load the library if
  80. there is. Note: Both back slashes and forward slashes have been supported
  81. since the earlier days of DOS at an API level although they are not
  82. supported by command prompt */
  83. if(_tcspbrk(filename, TEXT("\\/"))) {
  84. /** !checksrc! disable BANNEDFUNC 1 **/
  85. hModule = pLoadLibraryEx ?
  86. pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
  87. LoadLibrary(filename);
  88. }
  89. /* Detect if KB2533623 is installed, as LOAD_LIBARY_SEARCH_SYSTEM32 is only
  90. supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
  91. Server 2008 R2 with this patch or natively on Windows 8 and above */
  92. else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
  93. /* Load the DLL from the Windows system directory */
  94. hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
  95. }
  96. else {
  97. /* Attempt to get the Windows system path */
  98. UINT systemdirlen = GetSystemDirectory(NULL, 0);
  99. if(systemdirlen) {
  100. /* Allocate space for the full DLL path (Room for the null terminator
  101. is included in systemdirlen) */
  102. size_t filenamelen = _tcslen(filename);
  103. TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
  104. if(path && GetSystemDirectory(path, systemdirlen)) {
  105. /* Calculate the full DLL path */
  106. _tcscpy(path + _tcslen(path), TEXT("\\"));
  107. _tcscpy(path + _tcslen(path), filename);
  108. /* Load the DLL from the Windows system directory */
  109. /** !checksrc! disable BANNEDFUNC 1 **/
  110. hModule = pLoadLibraryEx ?
  111. pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
  112. LoadLibrary(path);
  113. }
  114. free(path);
  115. }
  116. }
  117. return hModule;
  118. }
  119. #else /* defined(_WIN32) */
  120. /* ISO C requires a translation unit to contain at least one declaration
  121. [-Wempty-translation-unit] */
  122. typedef int _TRANSLATION_UNIT_LOAD_LIBRARY_C_NOT_EMTPY;
  123. #endif /* defined(_WIN32) */