19 #ifndef COMPAT_W32DLFCN_H
20 #define COMPAT_W32DLFCN_H
31 static inline wchar_t *get_module_filename(HMODULE module)
33 wchar_t *path =
NULL, *new_path;
34 DWORD path_size = 0, path_len;
37 path_size = path_size ?
FFMIN(2 * path_size, INT16_MAX + 1) : MAX_PATH;
47 path_len = GetModuleFileNameW(module, path, path_size);
48 }
while (path_len && path_size <= INT16_MAX && path_size <= path_len);
64 static inline HMODULE win32_dlopen(
const char *
name)
67 HMODULE module =
NULL;
68 if (utf8towchar(
name, &name_w))
70 #if _WIN32_WINNT < 0x0602
72 if (!GetProcAddress(GetModuleHandleW(
L"kernel32.dll"),
"SetDefaultDllDirectories")) {
73 wchar_t *path =
NULL, *new_path;
74 DWORD pathlen, pathsize, namelen;
77 namelen = wcslen(name_w);
79 path = get_module_filename(
NULL);
82 new_path = wcsrchr(path,
'\\');
85 pathlen = new_path - path;
86 pathsize = pathlen + namelen + 2;
91 wcscpy(path + pathlen + 1, name_w);
92 module = LoadLibraryExW(path,
NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
95 pathlen = GetSystemDirectoryW(path, pathsize);
101 if (pathlen + namelen + 2 > pathsize) {
102 pathsize = pathlen + namelen + 2;
108 pathlen = GetSystemDirectoryW(path, pathsize);
112 path[pathlen] =
L'\\';
113 wcscpy(path + pathlen + 1, name_w);
114 module = LoadLibraryExW(path,
NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
122 #ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR
123 # define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200
125 #ifndef LOAD_LIBRARY_SEARCH_SYSTEM32
126 # define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
131 module = LoadPackagedLibrary(name_w, 0);
133 #define LOAD_FLAGS (LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32)
136 return LoadLibraryExA(
name,
NULL, LOAD_FLAGS);
137 module = LoadLibraryExW(name_w,
NULL, LOAD_FLAGS);
143 #define dlopen(name, flags) win32_dlopen(name)
144 #define dlclose FreeLibrary
145 #define dlsym GetProcAddress