19 #ifndef COMPAT_W32DLFCN_H
20 #define COMPAT_W32DLFCN_H
32 static inline wchar_t *get_module_filename(HMODULE module)
34 wchar_t *path =
NULL, *new_path;
35 DWORD path_size = 0, path_len;
38 path_size = path_size ?
FFMIN(2 * path_size, INT16_MAX + 1) : MAX_PATH;
48 path_len = GetModuleFileNameW(module, path, path_size);
49 }
while (path_len && path_size <= INT16_MAX && path_size <= path_len);
65 static inline HMODULE win32_dlopen(
const char *
name)
68 HMODULE module =
NULL;
69 if (utf8towchar(
name, &name_w))
71 #if _WIN32_WINNT < 0x0602
73 if (!GetProcAddress(GetModuleHandleW(
L"kernel32.dll"),
"SetDefaultDllDirectories")) {
74 wchar_t *path =
NULL, *new_path;
75 DWORD pathlen, pathsize, namelen;
78 namelen = wcslen(name_w);
80 path = get_module_filename(
NULL);
83 new_path = wcsrchr(path,
'\\');
86 pathlen = new_path - path;
87 pathsize = pathlen + namelen + 2;
92 wcscpy(path + pathlen + 1, name_w);
93 module = LoadLibraryExW(path,
NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
96 pathlen = GetSystemDirectoryW(path, pathsize);
102 if (pathlen + namelen + 2 > pathsize) {
103 pathsize = pathlen + namelen + 2;
109 pathlen = GetSystemDirectoryW(path, pathsize);
113 path[pathlen] =
L'\\';
114 wcscpy(path + pathlen + 1, name_w);
115 module = LoadLibraryExW(path,
NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
123 #ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR
124 # define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200
126 #ifndef LOAD_LIBRARY_SEARCH_SYSTEM32
127 # define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
132 module = LoadPackagedLibrary(name_w, 0);
134 #define LOAD_FLAGS (LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32)
137 return LoadLibraryExA(
name,
NULL, LOAD_FLAGS);
138 module = LoadLibraryExW(name_w,
NULL, LOAD_FLAGS);
144 #define dlopen(name, flags) win32_dlopen(name)
145 #define dlclose FreeLibrary
146 #define dlsym GetProcAddress