w32dlfcn.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef COMPAT_W32DLFCN_H
  19. #define COMPAT_W32DLFCN_H
  20. #ifdef _WIN32
  21. #include <windows.h>
  22. #include "config.h"
  23. #if (_WIN32_WINNT < 0x0602) || HAVE_WINRT
  24. #include "libavutil/wchar_filename.h"
  25. #endif
  26. /**
  27. * Safe function used to open dynamic libs. This attempts to improve program security
  28. * by removing the current directory from the dll search path. Only dll's found in the
  29. * executable or system directory are allowed to be loaded.
  30. * @param name The dynamic lib name.
  31. * @return A handle to the opened lib.
  32. */
  33. static inline HMODULE win32_dlopen(const char *name)
  34. {
  35. #if _WIN32_WINNT < 0x0602
  36. // Need to check if KB2533623 is available
  37. if (!GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "SetDefaultDllDirectories")) {
  38. HMODULE module = NULL;
  39. wchar_t *path = NULL, *name_w = NULL;
  40. DWORD pathlen;
  41. if (utf8towchar(name, &name_w))
  42. goto exit;
  43. path = (wchar_t *)av_mallocz_array(MAX_PATH, sizeof(wchar_t));
  44. // Try local directory first
  45. pathlen = GetModuleFileNameW(NULL, path, MAX_PATH);
  46. pathlen = wcsrchr(path, '\\') - path;
  47. if (pathlen == 0 || pathlen + wcslen(name_w) + 2 > MAX_PATH)
  48. goto exit;
  49. path[pathlen] = '\\';
  50. wcscpy(path + pathlen + 1, name_w);
  51. module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  52. if (module == NULL) {
  53. // Next try System32 directory
  54. pathlen = GetSystemDirectoryW(path, MAX_PATH);
  55. if (pathlen == 0 || pathlen + wcslen(name_w) + 2 > MAX_PATH)
  56. goto exit;
  57. path[pathlen] = '\\';
  58. wcscpy(path + pathlen + 1, name_w);
  59. module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  60. }
  61. exit:
  62. av_free(path);
  63. av_free(name_w);
  64. return module;
  65. }
  66. #endif
  67. #ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR
  68. # define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200
  69. #endif
  70. #ifndef LOAD_LIBRARY_SEARCH_SYSTEM32
  71. # define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
  72. #endif
  73. #if HAVE_WINRT
  74. wchar_t *name_w = NULL;
  75. int ret;
  76. if (utf8towchar(name, &name_w))
  77. return NULL;
  78. ret = LoadPackagedLibrary(name_w, 0);
  79. av_free(name_w);
  80. return ret;
  81. #else
  82. return LoadLibraryExA(name, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
  83. #endif
  84. }
  85. #define dlopen(name, flags) win32_dlopen(name)
  86. #define dlclose FreeLibrary
  87. #define dlsym GetProcAddress
  88. #else
  89. #include <dlfcn.h>
  90. #endif
  91. #endif /* COMPAT_W32DLFCN_H */