loadlibrary.c 5.0 KB

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