1 /* GMODULE - GLIB wrapper code for dynamic module loading
2 * Copyright (C) 1998, 2000 Tim Janik
4 * Win32 GMODULE implementation
5 * Copyright (C) 1998 Tor Lillqvist
7 * SPDX-License-Identifier: LGPL-2.1-or-later
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
42 #include <sys/cygwin.h>
45 static void G_GNUC_PRINTF (2, 3)
46 set_error (GError **error,
55 win32_error = g_win32_error_message (GetLastError ());
57 va_start (args, format);
58 detail = g_strdup_vprintf (format, args);
61 message = g_strconcat (detail, win32_error, NULL);
63 g_module_set_error (message);
64 g_set_error_literal (error, G_MODULE_ERROR, G_MODULE_ERROR_FAILED, message);
71 /* --- functions --- */
73 _g_module_open (const gchar *file_name,
85 cygwin_conv_to_win32_path(file_name, tmp);
88 wfilename = g_utf8_to_utf16 (file_name, -1, NULL, NULL, NULL);
90 /* suppress error dialog */
91 success = SetThreadErrorMode (SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS, &old_mode);
93 set_error (error, "");
95 /* When building for UWP, load app asset DLLs instead of filesystem DLLs.
96 * Needs MSVC, Windows 8 and newer, and is only usable from apps. */
97 #if _WIN32_WINNT >= 0x0602 && defined(G_WINAPI_ONLY_APP)
98 handle = LoadPackagedLibrary (wfilename, 0);
100 handle = LoadLibraryW (wfilename);
104 SetThreadErrorMode (old_mode, NULL);
108 set_error (error, "'%s': ", file_name);
114 static gpointer null_module_handle = &dummy;
117 _g_module_self (void)
119 return null_module_handle;
123 _g_module_close (gpointer handle)
125 if (handle != null_module_handle)
126 if (!FreeLibrary (handle))
127 set_error (NULL, "");
131 find_in_any_module_using_toolhelp (const gchar *symbol_name)
138 /* Under UWP, Module32Next and Module32First are not available since we're
139 * not allowed to search in the address space of arbitrary loaded DLLs */
140 #if !defined(G_WINAPI_ONLY_APP)
141 /* https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot#remarks
142 * If the function fails with ERROR_BAD_LENGTH, retry the function until it succeeds. */
145 snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, 0);
146 if (snapshot == INVALID_HANDLE_VALUE && GetLastError () == ERROR_BAD_LENGTH)
154 if (snapshot == INVALID_HANDLE_VALUE)
157 me32.dwSize = sizeof (me32);
159 if (Module32First (snapshot, &me32))
162 if ((p = GetProcAddress (me32.hModule, symbol_name)) != NULL)
164 } while (Module32Next (snapshot, &me32));
167 CloseHandle (snapshot);
174 find_in_any_module (const gchar *symbol_name)
178 if ((result = find_in_any_module_using_toolhelp (symbol_name)) == NULL)
185 _g_module_symbol (gpointer handle,
186 const gchar *symbol_name)
190 if (handle == null_module_handle)
192 if ((p = GetProcAddress (GetModuleHandle (NULL), symbol_name)) == NULL)
193 p = find_in_any_module (symbol_name);
196 p = GetProcAddress (handle, symbol_name);
199 set_error (NULL, "");
205 _g_module_build_path (const gchar *directory,
206 const gchar *module_name)
210 k = strlen (module_name);
212 if (directory && *directory)
213 if (k > 4 && g_ascii_strcasecmp (module_name + k - 4, ".dll") == 0)
214 return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, NULL);
216 else if (strncmp (module_name, "lib", 3) == 0 || strncmp (module_name, "cyg", 3) == 0)
217 return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, ".dll", NULL);
219 return g_strconcat (directory, G_DIR_SEPARATOR_S, "cyg", module_name, ".dll", NULL);
221 else if (strncmp (module_name, "lib", 3) == 0)
222 return g_strconcat (directory, G_DIR_SEPARATOR_S, module_name, ".dll", NULL);
224 return g_strconcat (directory, G_DIR_SEPARATOR_S, "lib", module_name, ".dll", NULL);
226 else if (k > 4 && g_ascii_strcasecmp (module_name + k - 4, ".dll") == 0)
227 return g_strdup (module_name);
229 else if (strncmp (module_name, "lib", 3) == 0 || strncmp (module_name, "cyg", 3) == 0)
230 return g_strconcat (module_name, ".dll", NULL);
232 return g_strconcat ("cyg", module_name, ".dll", NULL);
234 else if (strncmp (module_name, "lib", 3) == 0)
235 return g_strconcat (module_name, ".dll", NULL);
237 return g_strconcat ("lib", module_name, ".dll", NULL);