1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1998 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 1998-1999 Tor Lillqvist
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GLib Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GLib at ftp://ftp.gtk.org/pub/gtk/.
29 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
34 #include "glibconfig.h"
36 #include <glib/gstdio.h>
44 #define STRICT /* Strict typing, please */
53 #if defined(_MSC_VER) || defined(__DMC__)
55 #endif /* _MSC_VER || __DMC__ */
57 #define MODERN_API_FAMILY 2
59 #if WINAPI_FAMILY == MODERN_API_FAMILY
60 /* This is for modern UI Builds, where we can't use LoadLibraryW()/GetProcAddress() */
61 /* ntddk.h is found in the WDK, and MinGW */
65 #pragma comment (lib, "ntoskrnl.lib")
67 #elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
68 /* mingw-w64 must use winternl.h, but not MinGW */
75 #include "gthreadprivate.h"
76 #include "glib-init.h"
79 #include <sys/cygwin.h>
85 g_win32_ftruncate (gint fd,
88 return _chsize (fd, size);
96 * The setlocale() function in the Microsoft C library uses locale
97 * names of the form "English_United States.1252" etc. We want the
98 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
99 * current thread locale from Windows - without any encoding info -
100 * and returns it as a string of the above form for use in forming
101 * file names etc. The returned string should be deallocated with
104 * Returns: newly-allocated locale name.
107 #ifndef SUBLANG_SERBIAN_LATIN_BA
108 #define SUBLANG_SERBIAN_LATIN_BA 0x06
112 g_win32_getlocale (void)
123 const gchar *script = NULL;
125 /* Let the user override the system settings through environment
126 * variables, as on POSIX systems. Note that in GTK applications
127 * since GTK 2.10.7 setting either LC_ALL or LANG also sets the
128 * Win32 locale and C library locale through code in gtkmain.c.
130 if (((ev = g_getenv ("LC_ALL")) != NULL && ev[0] != '\0')
131 || ((ev = g_getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
132 || ((ev = g_getenv ("LANG")) != NULL && ev[0] != '\0'))
133 return g_strdup (ev);
135 lcid = GetThreadLocale ();
137 if (!GetLocaleInfoW (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) ||
138 !GetLocaleInfoW (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
139 return g_strdup ("C");
141 /* Strip off the sorting rules, keep only the language part. */
142 langid = LANGIDFROMLCID (lcid);
144 /* Split into language and territory part. */
145 primary = PRIMARYLANGID (langid);
146 sub = SUBLANGID (langid);
148 /* Handle special cases */
154 case SUBLANG_AZERI_LATIN:
157 case SUBLANG_AZERI_CYRILLIC:
162 case LANG_SERBIAN: /* LANG_CROATIAN == LANG_SERBIAN */
165 case SUBLANG_SERBIAN_LATIN:
166 case 0x06: /* Serbian (Latin) - Bosnia and Herzegovina */
174 case SUBLANG_UZBEK_LATIN:
177 case SUBLANG_UZBEK_CYRILLIC:
184 iso639_utf8 = g_utf16_to_utf8 (iso639, -1, NULL, NULL, NULL);
185 iso3166_utf8 = g_utf16_to_utf8 (iso3166, -1, NULL, NULL, NULL);
187 result = g_strconcat (iso639_utf8, "_", iso3166_utf8, script, NULL);
189 g_free (iso3166_utf8);
190 g_free (iso639_utf8);
196 * g_win32_error_message:
197 * @error: error code.
199 * Translate a Win32 error code (as returned by GetLastError() or
200 * WSAGetLastError()) into the corresponding message. The message is
201 * either language neutral, or in the thread's language, or the user's
202 * language, the system's language, or US English (see docs for
203 * FormatMessage()). The returned string is in UTF-8. It should be
204 * deallocated with g_free().
206 * Returns: newly-allocated error message
209 g_win32_error_message (gint error)
215 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
216 |FORMAT_MESSAGE_IGNORE_INSERTS
217 |FORMAT_MESSAGE_FROM_SYSTEM,
219 (LPWSTR) &msg, 0, NULL);
222 nchars = wcslen (msg);
224 if (nchars >= 2 && msg[nchars-1] == L'\n' && msg[nchars-2] == L'\r')
225 msg[nchars-2] = L'\0';
227 retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
232 retval = g_strdup ("");
238 * g_win32_get_package_installation_directory_of_module:
239 * @hmodule: (nullable): The Win32 handle for a DLL loaded into the current process, or %NULL
241 * This function tries to determine the installation directory of a
242 * software package based on the location of a DLL of the software
245 * @hmodule should be the handle of a loaded DLL or %NULL. The
246 * function looks up the directory that DLL was loaded from. If
247 * @hmodule is NULL, the directory the main executable of the current
248 * process is looked up. If that directory's last component is "bin"
249 * or "lib", its parent directory is returned, otherwise the directory
252 * It thus makes sense to pass only the handle to a "public" DLL of a
253 * software package to this function, as such DLLs typically are known
254 * to be installed in a "bin" or occasionally "lib" subfolder of the
255 * installation folder. DLLs that are of the dynamically loaded module
256 * or plugin variety are often located in more private locations
257 * deeper down in the tree, from which it is impossible for GLib to
258 * deduce the root of the package installation.
260 * The typical use case for this function is to have a DllMain() that
261 * saves the handle for the DLL. Then when code in the DLL needs to
262 * construct names of files in the installation tree it calls this
263 * function passing the DLL handle.
265 * Returns: a string containing the guessed installation directory for
266 * the software package @hmodule is from. The string is in the GLib
267 * file name encoding, i.e. UTF-8. The return value should be freed
268 * with g_free() when not needed any longer. If the function fails
274 g_win32_get_package_installation_directory_of_module (gpointer hmodule)
279 wchar_t wc_fn[MAX_PATH];
281 /* NOTE: it relies that GetModuleFileNameW returns only canonical paths */
282 if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
285 filename = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
287 if ((p = strrchr (filename, G_DIR_SEPARATOR)) != NULL)
290 retval = g_strdup (filename);
294 p = strrchr (retval, G_DIR_SEPARATOR);
300 if (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
301 g_ascii_strcasecmp (p + 1, "lib") == 0)
315 /* In Cygwin we need to have POSIX paths */
319 cygwin_conv_to_posix_path (retval, tmp);
321 retval = g_strdup (tmp);
329 get_package_directory_from_module (const gchar *module_name)
331 static GHashTable *module_dirs = NULL;
332 G_LOCK_DEFINE_STATIC (module_dirs);
333 HMODULE hmodule = NULL;
336 G_LOCK (module_dirs);
338 if (module_dirs == NULL)
339 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
341 fn = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
345 G_UNLOCK (module_dirs);
346 return g_strdup (fn);
351 wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
352 hmodule = GetModuleHandleW (wc_module_name);
353 g_free (wc_module_name);
357 G_UNLOCK (module_dirs);
362 fn = g_win32_get_package_installation_directory_of_module (hmodule);
366 G_UNLOCK (module_dirs);
370 g_hash_table_insert (module_dirs, module_name ? g_strdup (module_name) : "", fn);
372 G_UNLOCK (module_dirs);
374 return g_strdup (fn);
378 * g_win32_get_package_installation_directory:
379 * @package: (nullable): You should pass %NULL for this.
380 * @dll_name: (nullable): The name of a DLL that a package provides in UTF-8, or %NULL.
382 * Try to determine the installation directory for a software package.
384 * This function is deprecated. Use
385 * g_win32_get_package_installation_directory_of_module() instead.
387 * The use of @package is deprecated. You should always pass %NULL. A
388 * warning is printed if non-NULL is passed as @package.
390 * The original intended use of @package was for a short identifier of
391 * the package, typically the same identifier as used for
392 * `GETTEXT_PACKAGE` in software configured using GNU
393 * autotools. The function first looks in the Windows Registry for the
394 * value `#InstallationDirectory` in the key
395 * `#HKLM\Software\@package`, and if that value
396 * exists and is a string, returns that.
398 * It is strongly recommended that packagers of GLib-using libraries
399 * for Windows do not store installation paths in the Registry to be
400 * used by this function as that interfers with having several
401 * parallel installations of the library. Enabling multiple
402 * installations of different versions of some GLib-using library, or
403 * GLib itself, is desirable for various reasons.
405 * For this reason it is recommended to always pass %NULL as
406 * @package to this function, to avoid the temptation to use the
407 * Registry. In version 2.20 of GLib the @package parameter
408 * will be ignored and this function won't look in the Registry at all.
410 * If @package is %NULL, or the above value isn't found in the
411 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
412 * into the current process. Typically that would be the name of the
413 * DLL calling this function, looking for its installation
414 * directory. The function then asks Windows what directory that DLL
415 * was loaded from. If that directory's last component is "bin" or
416 * "lib", the parent directory is returned, otherwise the directory
417 * itself. If that DLL isn't loaded, the function proceeds as if
418 * @dll_name was %NULL.
420 * If both @package and @dll_name are %NULL, the directory from where
421 * the main executable of the process was loaded is used instead in
422 * the same way as above.
424 * Returns: a string containing the installation directory for
425 * @package. The string is in the GLib file name encoding,
426 * i.e. UTF-8. The return value should be freed with g_free() when not
427 * needed any longer. If the function fails %NULL is returned.
429 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
430 * g_win32_get_package_installation_directory_of_module() instead.
434 g_win32_get_package_installation_directory (const gchar *package,
435 const gchar *dll_name)
437 gchar *result = NULL;
440 g_warning ("Passing a non-NULL package to g_win32_get_package_installation_directory() is deprecated and it is ignored.");
442 if (dll_name != NULL)
443 result = get_package_directory_from_module (dll_name);
446 result = get_package_directory_from_module (NULL);
452 * g_win32_get_package_installation_subdirectory:
453 * @package: (nullable): You should pass %NULL for this.
454 * @dll_name: (nullable): The name of a DLL that a package provides, in UTF-8, or %NULL.
455 * @subdir: A subdirectory of the package installation directory, also in UTF-8
457 * This function is deprecated. Use
458 * g_win32_get_package_installation_directory_of_module() and
459 * g_build_filename() instead.
461 * Returns a newly-allocated string containing the path of the
462 * subdirectory @subdir in the return value from calling
463 * g_win32_get_package_installation_directory() with the @package and
464 * @dll_name parameters. See the documentation for
465 * g_win32_get_package_installation_directory() for more details. In
466 * particular, note that it is deprecated to pass anything except NULL
469 * Returns: a string containing the complete path to @subdir inside
470 * the installation directory of @package. The returned string is in
471 * the GLib file name encoding, i.e. UTF-8. The return value should be
472 * freed with g_free() when no longer needed. If something goes wrong,
475 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
476 * g_win32_get_package_installation_directory_of_module() instead, and
477 * then construct a subdirectory pathname with g_build_filename().
481 g_win32_get_package_installation_subdirectory (const gchar *package,
482 const gchar *dll_name,
488 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
489 prefix = g_win32_get_package_installation_directory (package, dll_name);
490 G_GNUC_END_IGNORE_DEPRECATIONS
492 dirname = g_build_filename (prefix, subdir, NULL);
499 * private API to call Windows's RtlGetVersion(), which may need to be called
500 * via GetProcAddress()
503 _g_win32_call_rtl_version (OSVERSIONINFOEXW *info)
505 static OSVERSIONINFOEXW result;
506 static gsize inited = 0;
508 g_return_val_if_fail (info != NULL, FALSE);
510 if (g_once_init_enter (&inited))
512 #if WINAPI_FAMILY != MODERN_API_FAMILY
513 /* For non-modern UI Apps, use the LoadLibraryW()/GetProcAddress() thing */
514 typedef NTSTATUS (WINAPI fRtlGetVersion) (PRTL_OSVERSIONINFOEXW);
516 fRtlGetVersion *RtlGetVersion;
517 HMODULE hmodule = LoadLibraryW (L"ntdll.dll");
518 g_return_val_if_fail (hmodule != NULL, FALSE);
520 RtlGetVersion = (fRtlGetVersion *) GetProcAddress (hmodule, "RtlGetVersion");
521 g_return_val_if_fail (RtlGetVersion != NULL, FALSE);
524 memset (&result, 0, sizeof (OSVERSIONINFOEXW));
525 result.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEXW);
527 RtlGetVersion (&result);
529 #if WINAPI_FAMILY != MODERN_API_FAMILY
530 FreeLibrary (hmodule);
532 g_once_init_leave (&inited, TRUE);
541 * g_win32_check_windows_version:
542 * @major: major version of Windows
543 * @minor: minor version of Windows
544 * @spver: Windows Service Pack Level, 0 if none
545 * @os_type: Type of Windows OS
547 * Returns whether the version of the Windows operating system the
548 * code is running on is at least the specified major, minor and
549 * service pack versions. See MSDN documentation for the Operating
550 * System Version. Software that needs even more detailed version and
551 * feature information should use the Win32 API VerifyVersionInfo()
554 * Successive calls of this function can be used for enabling or
555 * disabling features at run-time for a range of Windows versions,
556 * as per the VerifyVersionInfo() API documentation.
558 * Returns: %TRUE if the Windows Version is the same or greater than
559 * the specified major, minor and service pack versions, and
560 * whether the running Windows is a workstation or server edition
561 * of Windows, if specifically specified.
566 g_win32_check_windows_version (const gint major,
569 const GWin32OSType os_type)
571 OSVERSIONINFOEXW osverinfo;
572 gboolean is_ver_checked = FALSE;
573 gboolean is_type_checked = FALSE;
575 /* We Only Support Checking for XP or later */
576 g_return_val_if_fail (major >= 5 && (major <= 6 || major == 10), FALSE);
577 g_return_val_if_fail ((major >= 5 && minor >= 1) || major >= 6, FALSE);
579 /* Check for Service Pack Version >= 0 */
580 g_return_val_if_fail (spver >= 0, FALSE);
582 if (!_g_win32_call_rtl_version (&osverinfo))
585 /* check the OS and Service Pack Versions */
586 if (osverinfo.dwMajorVersion > (DWORD) major)
587 is_ver_checked = TRUE;
588 else if (osverinfo.dwMajorVersion == (DWORD) major)
590 if (osverinfo.dwMinorVersion > (DWORD) minor)
591 is_ver_checked = TRUE;
592 else if (osverinfo.dwMinorVersion == (DWORD) minor)
593 if (osverinfo.wServicePackMajor >= (DWORD) spver)
594 is_ver_checked = TRUE;
603 is_type_checked = TRUE;
605 case G_WIN32_OS_WORKSTATION:
606 if (osverinfo.wProductType == VER_NT_WORKSTATION)
607 is_type_checked = TRUE;
609 case G_WIN32_OS_SERVER:
610 if (osverinfo.wProductType == VER_NT_SERVER ||
611 osverinfo.wProductType == VER_NT_DOMAIN_CONTROLLER)
612 is_type_checked = TRUE;
615 /* shouldn't get here normally */
616 g_warning ("Invalid os_type specified");
621 return is_ver_checked && is_type_checked;
625 * g_win32_get_windows_version:
627 * This function is deprecated. Use
628 * g_win32_check_windows_version() instead.
630 * Returns version information for the Windows operating system the
631 * code is running on. See MSDN documentation for the GetVersion()
632 * function. To summarize, the most significant bit is one on Win9x,
633 * and zero on NT-based systems. Since version 2.14, GLib works only
634 * on NT-based systems, so checking whether your are running on Win9x
635 * in your own software is moot. The least significant byte is 4 on
636 * Windows NT 4, and 5 on Windows XP. Software that needs really
637 * detailed version and feature information should use Win32 API like
638 * GetVersionEx() and VerifyVersionInfo().
640 * Returns: The version information.
642 * Deprecated: 2.44: Be aware that for Windows 8.1 and Windows Server
643 * 2012 R2 and later, this will return 62 unless the application is
644 * manifested for Windows 8.1/Windows Server 2012 R2, for example.
645 * MSDN stated that GetVersion(), which is used here, is subject to
646 * further change or removal after Windows 8.1.
649 g_win32_get_windows_version (void)
651 static gsize windows_version;
653 if (g_once_init_enter (&windows_version))
654 g_once_init_leave (&windows_version, GetVersion ());
656 return windows_version;
660 * Doesn't use gettext (and gconv), preventing recursive calls when
661 * g_win32_locale_filename_from_utf8() is called during
662 * gettext initialization.
665 special_wchar_to_locale_encoding (wchar_t *wstring)
670 BOOL not_representable = FALSE;
672 sizeof_output = WideCharToMultiByte (CP_ACP,
673 WC_NO_BEST_FIT_CHARS,
679 if (not_representable ||
680 sizeof_output == 0 ||
681 sizeof_output > MAX_PATH)
684 result = g_malloc0 (sizeof_output + 1);
686 wctmb_result = WideCharToMultiByte (CP_ACP,
687 WC_NO_BEST_FIT_CHARS,
689 result, sizeof_output + 1,
693 if (wctmb_result == sizeof_output &&
694 not_representable == FALSE)
703 * g_win32_locale_filename_from_utf8:
704 * @utf8filename: a UTF-8 encoded filename.
706 * Converts a filename from UTF-8 to the system codepage.
708 * On NT-based Windows, on NTFS file systems, file names are in
709 * Unicode. It is quite possible that Unicode file names contain
710 * characters not representable in the system codepage. (For instance,
711 * Greek or Cyrillic characters on Western European or US Windows
712 * installations, or various less common CJK characters on CJK Windows
715 * In such a case, and if the filename refers to an existing file, and
716 * the file system stores alternate short (8.3) names for directory
717 * entries, the short form of the filename is returned. Note that the
718 * "short" name might in fact be longer than the Unicode name if the
719 * Unicode name has very short pathname components containing
720 * non-ASCII characters. If no system codepage name for the file is
721 * possible, %NULL is returned.
723 * The return value is dynamically allocated and should be freed with
724 * g_free() when no longer needed.
726 * Returns: The converted filename, or %NULL on conversion
727 * failure and lack of short names.
732 g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
737 wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL);
742 retval = special_wchar_to_locale_encoding (wname);
746 /* Conversion failed, so check if there is a 8.3 version, and use that. */
747 wchar_t wshortname[MAX_PATH + 1];
749 if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname)))
750 retval = special_wchar_to_locale_encoding (wshortname);
759 * g_win32_get_command_line:
761 * Gets the command line arguments, on Windows, in the GLib filename
762 * encoding (ie: UTF-8).
764 * Normally, on Windows, the command line arguments are passed to main()
765 * in the system codepage encoding. This prevents passing filenames as
766 * arguments if the filenames contain characters that fall outside of
767 * this codepage. If such filenames are passed, then substitutions
768 * will occur (such as replacing some characters with '?').
770 * GLib's policy of using UTF-8 as a filename encoding on Windows was
771 * designed to localise the pain of dealing with filenames outside of
772 * the system codepage to one area: dealing with commandline arguments
775 * As such, most GLib programs should ignore the value of argv passed to
776 * their main() function and call g_win32_get_command_line() instead.
777 * This will get the "full Unicode" commandline arguments using
778 * GetCommandLineW() and convert it to the GLib filename encoding (which
779 * is UTF-8 on Windows).
781 * The strings returned by this function are suitable for use with
782 * functions such as g_open() and g_file_new_for_commandline_arg() but
783 * are not suitable for use with g_option_context_parse(), which assumes
784 * that its input will be in the system codepage. The return value is
785 * suitable for use with g_option_context_parse_strv(), however, which
786 * is a better match anyway because it won't leak memory.
788 * Unlike argv, the returned value is a normal strv and can (and should)
789 * be freed with g_strfreev() when no longer needed.
791 * Returns: (transfer full): the commandline arguments in the GLib
792 * filename encoding (ie: UTF-8)
797 g_win32_get_command_line (void)
803 args = CommandLineToArgvW (GetCommandLineW(), &n);
805 result = g_new (gchar *, n + 1);
806 for (i = 0; i < n; i++)
807 result[i] = g_utf16_to_utf8 (args[i], -1, NULL, NULL, NULL);
814 /* Binary compatibility versions. Not for newly compiled code. */
816 _GLIB_EXTERN gchar *g_win32_get_package_installation_directory_utf8 (const gchar *package,
817 const gchar *dll_name);
819 _GLIB_EXTERN gchar *g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
820 const gchar *dll_name,
821 const gchar *subdir);
824 g_win32_get_package_installation_directory_utf8 (const gchar *package,
825 const gchar *dll_name)
827 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
828 return g_win32_get_package_installation_directory (package, dll_name);
829 G_GNUC_END_IGNORE_DEPRECATIONS
833 g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
834 const gchar *dll_name,
837 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
838 return g_win32_get_package_installation_subdirectory (package,
841 G_GNUC_END_IGNORE_DEPRECATIONS
844 /* This function looks up two environment
845 * variables, G_WIN32_ALLOC_CONSOLE and G_WIN32_ATTACH_CONSOLE.
846 * G_WIN32_ALLOC_CONSOLE, if set to 1, makes the process
847 * call AllocConsole(). This is useful for binaries that
848 * are compiled to run without automatically-allocated console
849 * (like most GUI applications).
850 * G_WIN32_ATTACH_CONSOLE, if set to a comma-separated list
851 * of one or more strings "stdout", "stdin" and "stderr",
852 * makes the process reopen the corresponding standard streams
853 * to ensure that they are attached to the files that
854 * GetStdHandle() returns, which, hopefully, would be
855 * either a file handle or a console handle.
857 * This function is called automatically when glib DLL is
858 * attached to a process, from DllMain().
861 g_console_win32_init (void)
867 const gchar *stream_name;
868 DWORD std_handle_type;
874 { FALSE, stdin, "stdin", STD_INPUT_HANDLE, _O_RDONLY, "rb" },
875 { FALSE, stdout, "stdout", STD_OUTPUT_HANDLE, 0, "wb" },
876 { FALSE, stderr, "stderr", STD_ERROR_HANDLE, 0, "wb" },
879 const gchar *attach_envvar;
883 /* Note: it's not a very good practice to use DllMain()
884 * to call any functions not in Kernel32.dll.
885 * The following only works if there are no weird
886 * circular DLL dependencies that could cause glib DllMain()
887 * to be called before CRT DllMain().
890 if (g_strcmp0 (g_getenv ("G_WIN32_ALLOC_CONSOLE"), "1") == 0)
891 AllocConsole (); /* no error handling, fails if console already exists */
893 attach_envvar = g_getenv ("G_WIN32_ATTACH_CONSOLE");
895 if (attach_envvar == NULL)
898 /* Re-use parent console, if we don't have our own.
899 * If we do, it will fail, so just ignore the error.
901 AttachConsole (ATTACH_PARENT_PROCESS);
903 attach_strs = g_strsplit (attach_envvar, ",", -1);
905 for (i = 0; attach_strs[i]; i++)
907 if (g_strcmp0 (attach_strs[i], "stdout") == 0)
908 streams[1].redirect = TRUE;
909 else if (g_strcmp0 (attach_strs[i], "stderr") == 0)
910 streams[2].redirect = TRUE;
911 else if (g_strcmp0 (attach_strs[i], "stdin") == 0)
912 streams[0].redirect = TRUE;
914 g_warning ("Unrecognized stream name %s", attach_strs[i]);
917 g_strfreev (attach_strs);
919 for (i = 0; i < G_N_ELEMENTS (streams); i++)
924 int preferred_fd = i;
928 if (!streams[i].redirect)
931 if (ferror (streams[i].stream) != 0)
933 g_warning ("Stream %s is in error state", streams[i].stream_name);
937 std_handle = GetStdHandle (streams[i].std_handle_type);
939 if (std_handle == INVALID_HANDLE_VALUE)
941 DWORD gle = GetLastError ();
942 g_warning ("Standard handle for %s can't be obtained: %lu",
943 streams[i].stream_name, gle);
947 old_fd = fileno (streams[i].stream);
949 /* We need the stream object to be associated with
950 * any valid integer fd for the code to work.
951 * If it isn't, reopen it with NUL (/dev/null) to
956 if (freopen ("NUL", streams[i].mode, streams[i].stream) == NULL)
959 g_warning ("Failed to redirect %s: %d - %s",
960 streams[i].stream_name,
966 old_fd = fileno (streams[i].stream);
970 g_warning ("Stream %s does not have a valid fd",
971 streams[i].stream_name);
976 new_fd = _open_osfhandle ((intptr_t) std_handle, streams[i].flags);
980 g_warning ("Failed to create new fd for stream %s",
981 streams[i].stream_name);
985 backup_fd = dup (old_fd);
988 g_warning ("Failed to backup old fd %d for stream %s",
989 old_fd, streams[i].stream_name);
993 /* Force old_fd to be associated with the same file
994 * as new_fd, i.e with the standard handle we need
995 * (or, rather, with the same kernel object; handle
996 * value will be different, but the kernel object
999 /* NOTE: MSDN claims that _dup2() returns 0 on success and -1 on error,
1000 * POSIX claims that dup2() reurns new FD on success and -1 on error.
1001 * The "< 0" check satisfies the error condition for either implementation.
1003 if (_dup2 (new_fd, old_fd) < 0)
1006 g_warning ("Failed to substitute fd %d for stream %s: %d : %s",
1007 old_fd, streams[i].stream_name, errsv, strerror (errsv));
1016 /* Try to restore old_fd back to its previous
1017 * handle, in case the _dup2() call above succeeded partially.
1019 if (_dup2 (backup_fd, old_fd) < 0)
1022 g_warning ("Failed to restore fd %d for stream %s: %d : %s",
1023 old_fd, streams[i].stream_name, errsv, strerror (errsv));
1031 /* Success, drop the backup */
1035 /* Sadly, there's no way to check that preferred_fd
1036 * is currently valid, so we can't back it up.
1037 * Doing operations on invalid FDs invokes invalid
1038 * parameter handler, which is bad for us.
1040 if (old_fd != preferred_fd)
1041 /* This extra code will also try to ensure that
1042 * the expected file descriptors 0, 1 and 2 are
1043 * associated with the appropriate standard
1046 if (_dup2 (new_fd, preferred_fd) < 0)
1047 g_warning ("Failed to dup fd %d into fd %d", new_fd, preferred_fd);
1053 /* This is a handle to the Vectored Exception Handler that
1054 * we install on library initialization. If installed correctly,
1055 * it will be non-NULL. Only used to later de-install the handler
1056 * on library de-initialization.
1058 static void *WinVEH_handle = NULL;
1060 #define DEBUGGER_BUFFER_SIZE (MAX_PATH + 1)
1061 /* This is the debugger that we'll run on crash */
1062 static wchar_t debugger[DEBUGGER_BUFFER_SIZE];
1064 static gsize number_of_exceptions_to_catch = 0;
1065 static DWORD *exceptions_to_catch = NULL;
1067 static HANDLE debugger_wakeup_event = 0;
1068 static DWORD debugger_spawn_flags = 0;
1070 #include "gwin32-private.c"
1073 copy_chars (char *buffer,
1075 const char *to_copy)
1077 gsize copy_count = MIN (strlen (to_copy), *buffer_size - 1);
1078 memset (buffer, 0x20, copy_count);
1079 strncpy_s (buffer, *buffer_size, to_copy, _TRUNCATE);
1080 *buffer_size -= copy_count;
1081 return &buffer[copy_count];
1084 /* Handles exceptions (useful for debugging).
1085 * Issues a DebugBreak() call if the process is being debugged (not really
1086 * useful - if the process is being debugged, this handler won't be invoked
1087 * anyway). If it is not, runs a debugger from G_DEBUGGER env var,
1088 * substituting first %p in it for PID, and the first %e for the event handle -
1089 * that event should be set once the debugger attaches itself (otherwise the
1090 * only way out of WaitForSingleObject() is to time out after 1 minute).
1091 * For example, G_DEBUGGER can be set to the following command:
1093 * gdb.exe -ex "attach %p" -ex "signal-event %e" -ex "bt" -ex "c"
1095 * This will make GDB attach to the process, signal the event (GDB must be
1096 * recent enough for the signal-event command to be available),
1097 * show the backtrace and resume execution, which should make it catch
1098 * the exception when Windows re-raises it again.
1099 * The command line can't be longer than MAX_PATH (260 characters).
1101 * This function will only stop (and run a debugger) on the following exceptions:
1102 * * EXCEPTION_ACCESS_VIOLATION
1103 * * EXCEPTION_STACK_OVERFLOW
1104 * * EXCEPTION_ILLEGAL_INSTRUCTION
1105 * To make it stop at other exceptions one should set the G_VEH_CATCH
1106 * environment variable to a list of comma-separated hexadecimal numbers,
1107 * where each number is the code of an exception that should be caught.
1108 * This is done to prevent GLib from breaking when Windows uses
1109 * exceptions to shuttle information (SetThreadName(), OutputDebugString())
1110 * or for control flow.
1112 * This function deliberately avoids calling any GLib code.
1113 * This is done on purpose. This function can be called when the program
1114 * is in a bad state (crashing). It can also be called very early, as soon
1115 * as the handler is installed. Therefore, it's imperative that
1116 * it does as little as possible. Preferably, all the work that can be
1117 * done in advance (when the program is not crashing yet) should be done
1120 static LONG __stdcall
1121 g_win32_veh_handler (PEXCEPTION_POINTERS ExceptionInfo)
1123 EXCEPTION_RECORD *er;
1126 PROCESS_INFORMATION pi;
1127 #define ITOA_BUFFER_SIZE 100
1128 char itoa_buffer[ITOA_BUFFER_SIZE];
1129 #define DEBUG_STRING_SIZE 1024
1130 gsize dbgs = DEBUG_STRING_SIZE;
1131 char debug_string[DEBUG_STRING_SIZE];
1134 if (ExceptionInfo == NULL ||
1135 ExceptionInfo->ExceptionRecord == NULL ||
1136 IsDebuggerPresent () ||
1138 return EXCEPTION_CONTINUE_SEARCH;
1140 er = ExceptionInfo->ExceptionRecord;
1142 switch (er->ExceptionCode)
1144 case EXCEPTION_ACCESS_VIOLATION:
1145 case EXCEPTION_STACK_OVERFLOW:
1146 case EXCEPTION_ILLEGAL_INSTRUCTION:
1149 for (i = 0; i < number_of_exceptions_to_catch; i++)
1150 if (exceptions_to_catch[i] == er->ExceptionCode)
1153 if (i == number_of_exceptions_to_catch)
1154 return EXCEPTION_CONTINUE_SEARCH;
1159 memset (&si, 0, sizeof (si));
1160 memset (&pi, 0, sizeof (pi));
1161 si.cb = sizeof (si);
1163 /* Run the debugger */
1164 if (0 != CreateProcessW (NULL, debugger, NULL, NULL, TRUE, debugger_spawn_flags, NULL, NULL, &si, &pi))
1166 CloseHandle (pi.hProcess);
1167 CloseHandle (pi.hThread);
1168 /* If successful, wait for 60 seconds on the event
1169 * we passed. The debugger should signal that event.
1170 * 60 second limit is here to prevent us from hanging
1171 * up forever in case the debugger does not support
1174 WaitForSingleObject (debugger_wakeup_event, 60000);
1176 dbgp = &debug_string[0];
1178 dbgp = copy_chars (dbgp, &dbgs, "Exception code=0x");
1180 _ui64toa_s (er->ExceptionCode, itoa_buffer, ITOA_BUFFER_SIZE, 16);
1181 dbgp = copy_chars (dbgp, &dbgs, itoa_buffer);
1182 dbgp = copy_chars (dbgp, &dbgs, " flags=0x");
1184 _ui64toa_s (er->ExceptionFlags, itoa_buffer, ITOA_BUFFER_SIZE, 16);
1185 dbgp = copy_chars (dbgp, &dbgs, itoa_buffer);
1186 dbgp = copy_chars (dbgp, &dbgs, " at 0x");
1188 _ui64toa_s ((guintptr) er->ExceptionAddress, itoa_buffer, ITOA_BUFFER_SIZE, 16);
1189 dbgp = copy_chars (dbgp, &dbgs, itoa_buffer);
1191 switch (er->ExceptionCode)
1193 case EXCEPTION_ACCESS_VIOLATION:
1194 dbgp = copy_chars (dbgp, &dbgs, ". Access violation - attempting to ");
1195 if (er->ExceptionInformation[0] == 0)
1196 dbgp = copy_chars (dbgp, &dbgs, "read data");
1197 else if (er->ExceptionInformation[0] == 1)
1198 dbgp = copy_chars (dbgp, &dbgs, "write data");
1199 else if (er->ExceptionInformation[0] == 8)
1200 dbgp = copy_chars (dbgp, &dbgs, "execute data");
1202 dbgp = copy_chars (dbgp, &dbgs, "do something bad");
1203 dbgp = copy_chars (dbgp, &dbgs, " at address 0x");
1205 _ui64toa_s (er->ExceptionInformation[1], itoa_buffer, ITOA_BUFFER_SIZE, 16);
1206 dbgp = copy_chars (dbgp, &dbgs, itoa_buffer);
1208 case EXCEPTION_IN_PAGE_ERROR:
1209 dbgp = copy_chars (dbgp, &dbgs, ". Page access violation - attempting to ");
1210 if (er->ExceptionInformation[0] == 0)
1211 dbgp = copy_chars (dbgp, &dbgs, "read from an inaccessible page");
1212 else if (er->ExceptionInformation[0] == 1)
1213 dbgp = copy_chars (dbgp, &dbgs, "write to an inaccessible page");
1214 else if (er->ExceptionInformation[0] == 8)
1215 dbgp = copy_chars (dbgp, &dbgs, "execute data in page");
1217 dbgp = copy_chars (dbgp, &dbgs, "do something bad with a page");
1218 dbgp = copy_chars (dbgp, &dbgs, " at address 0x");
1220 _ui64toa_s (er->ExceptionInformation[1], itoa_buffer, ITOA_BUFFER_SIZE, 16);
1221 dbgp = copy_chars (dbgp, &dbgs, itoa_buffer);
1222 dbgp = copy_chars (dbgp, &dbgs, " with status ");
1224 _ui64toa_s (er->ExceptionInformation[2], itoa_buffer, ITOA_BUFFER_SIZE, 16);
1225 dbgp = copy_chars (dbgp, &dbgs, itoa_buffer);
1231 dbgp = copy_chars (dbgp, &dbgs, "\n");
1232 OutputDebugStringA (debug_string);
1235 /* Now the debugger is present, and we can try
1236 * resuming execution, re-triggering the exception,
1237 * which will be caught by debugger this time around.
1239 if (IsDebuggerPresent ())
1240 return EXCEPTION_CONTINUE_EXECUTION;
1242 return EXCEPTION_CONTINUE_SEARCH;
1246 parse_catch_list (const wchar_t *catch_buffer,
1248 gsize num_exceptions)
1250 const wchar_t *catch_list = catch_buffer;
1254 while (catch_list != NULL &&
1257 unsigned long catch_code;
1260 catch_code = wcstoul (catch_list, &end, 16);
1261 if (errno != NO_ERROR)
1264 if (catch_list != NULL && catch_list[0] == L',')
1266 if (exceptions && i < num_exceptions)
1267 exceptions[i++] = catch_code;
1274 g_crash_handler_win32_init (void)
1276 wchar_t debugger_env[DEBUGGER_BUFFER_SIZE];
1277 #define CATCH_BUFFER_SIZE 1024
1278 wchar_t catch_buffer[CATCH_BUFFER_SIZE];
1279 SECURITY_ATTRIBUTES sa;
1281 if (WinVEH_handle != NULL)
1284 /* Do not register an exception handler if we're not supposed to catch any
1285 * exceptions. Exception handlers are considered dangerous to use, and can
1286 * break advanced exception handling such as in CLRs like C# or other managed
1287 * code. See: http://www.windows-tech.info/13/785f590867bd6316.php
1289 debugger_env[0] = 0;
1290 if (!GetEnvironmentVariableW (L"G_DEBUGGER", debugger_env, DEBUGGER_BUFFER_SIZE))
1293 /* Create an inheritable event */
1294 memset (&sa, 0, sizeof (sa));
1295 sa.nLength = sizeof (sa);
1296 sa.bInheritHandle = TRUE;
1297 debugger_wakeup_event = CreateEvent (&sa, FALSE, FALSE, NULL);
1299 /* Put process ID and event handle into debugger commandline */
1300 if (!_g_win32_subst_pid_and_event_w (debugger, G_N_ELEMENTS (debugger),
1301 debugger_env, GetCurrentProcessId (),
1302 (guintptr) debugger_wakeup_event))
1304 CloseHandle (debugger_wakeup_event);
1305 debugger_wakeup_event = 0;
1309 debugger[MAX_PATH] = L'\0';
1311 catch_buffer[0] = 0;
1312 if (GetEnvironmentVariableW (L"G_VEH_CATCH", catch_buffer, CATCH_BUFFER_SIZE))
1314 number_of_exceptions_to_catch = parse_catch_list (catch_buffer, NULL, 0);
1315 if (number_of_exceptions_to_catch > 0)
1317 exceptions_to_catch = g_new0 (DWORD, number_of_exceptions_to_catch);
1318 parse_catch_list (catch_buffer, exceptions_to_catch, number_of_exceptions_to_catch);
1322 if (GetEnvironmentVariableW (L"G_DEBUGGER_OLD_CONSOLE", (wchar_t *) &debugger_spawn_flags, 1))
1323 debugger_spawn_flags = 0;
1325 debugger_spawn_flags = CREATE_NEW_CONSOLE;
1327 WinVEH_handle = AddVectoredExceptionHandler (0, &g_win32_veh_handler);
1331 g_crash_handler_win32_deinit (void)
1333 if (WinVEH_handle != NULL)
1334 RemoveVectoredExceptionHandler (WinVEH_handle);
1336 WinVEH_handle = NULL;
1340 * g_win32_find_helper_executable_path:
1341 * @executable_name: (transfer none): name of the helper executable to find
1342 * (something like gspawn-win64-helper.exe or gdbus.exe for example).
1343 * @dll_handle: handle of the DLL to use as searching base path. Pass NULL
1344 * to take current process executable as searching base path.
1346 * Find an external executable path and name starting in the same folder
1347 * as a specified DLL or current process executable path. Helper executables
1348 * (like gspawn-win64-helper.exe, gspawn-win64-helper-console.exe or
1349 * gdbus.exe for example) are generally installed in the same folder as the
1350 * corresponding DLL file.
1352 * So, if package has been correctly installed, with a dynamic build of GLib,
1353 * the helper executable should be in the same directory as the corresponding
1354 * DLL file and searching should be straightforward.
1356 * But if built statically, DLL handle is not available and we have to start
1357 * searching from the directory holding current executable. It may be very
1358 * different from the directory containing the helper program. In order to
1359 * find the right helper program automatically in all common situations, we
1373 * starting at base searching path (DLL or current executable directory) and
1374 * getting up until the root path. If we cannot still find the helper program,
1375 * we'll rely on PATH as the last resort.
1377 * Returns: (transfer full) (type filename) (nullable): the helper executable
1378 * path and name in the GLib filename encoding or NULL in case of error. It
1379 * should be deallocated with g_free().
1382 g_win32_find_helper_executable_path (const gchar *executable_name, void *dll_handle)
1384 static const gchar *const subdirs[] = { "", "bin", "lib", "glib", "gio" };
1385 static const gsize nb_subdirs = G_N_ELEMENTS (subdirs);
1387 DWORD module_path_len;
1388 wchar_t module_path[MAX_PATH + 2] = { 0 };
1389 gchar *base_searching_path;
1391 gchar *executable_path;
1394 g_return_val_if_fail (executable_name && *executable_name, NULL);
1396 module_path_len = GetModuleFileNameW (dll_handle, module_path, MAX_PATH + 1);
1397 /* The > MAX_PATH check prevents truncated module path usage */
1398 if (module_path_len == 0 || module_path_len > MAX_PATH)
1401 base_searching_path = g_utf16_to_utf8 (module_path, -1, NULL, NULL, NULL);
1402 if (base_searching_path == NULL)
1405 p = strrchr (base_searching_path, G_DIR_SEPARATOR);
1408 g_free (base_searching_path);
1415 /* Search in subdirectories */
1416 for (i = 0; i < nb_subdirs; ++i)
1418 /* As this function is exclusively used on Windows, the
1419 * executable_path is always an absolute path. At worse, when
1420 * reaching the root of the filesystem, base_searching_path may
1421 * equal something like "[Drive letter]:" but never "/" like on
1423 * For the peace of mind we still assert this, just in case that
1424 * one day someone tries to use this function on Linux or Mac.
1426 executable_path = g_build_filename (base_searching_path, subdirs[i], executable_name, NULL);
1427 g_assert (g_path_is_absolute (executable_path));
1428 if (g_file_test (executable_path, G_FILE_TEST_IS_REGULAR))
1431 g_free (executable_path);
1432 executable_path = NULL;
1435 if (executable_path != NULL)
1438 /* Let's get one directory level up */
1439 p = strrchr (base_searching_path, G_DIR_SEPARATOR);
1445 g_free (base_searching_path);
1447 if (executable_path == NULL)
1449 /* Search in system PATH */
1450 executable_path = g_find_program_in_path (executable_name);
1451 if (executable_path == NULL)
1452 executable_path = g_strdup (executable_name);
1455 return executable_path;
1459 * g_win32_handle_is_socket:
1460 * @h: a win32 HANDLE
1462 * Returns: %TRUE if the handle is a `SOCKET`.
1465 g_win32_handle_is_socket (HANDLE h)
1468 int optlen = sizeof (option);
1470 /* according to: https://stackoverflow.com/a/50981652/1277510, this is reasonable */
1471 if (getsockopt ((SOCKET) h, SOL_SOCKET, SO_DEBUG, (char *) &option, &optlen) == SOCKET_ERROR)
1478 * g_win32_reopen_noninherited:
1479 * @fd: (transfer full): A file descriptor
1480 * @mode: _open_osfhandle flags
1481 * @error: A location to return an error of type %G_FILE_ERROR
1483 * Reopen the given @fd with `_O_NOINHERIT`.
1485 * The @fd is closed on success.
1487 * Returns: (transfer full): The new file-descriptor, or -1 on error.
1490 g_win32_reopen_noninherited (int fd,
1498 h = (HANDLE) _get_osfhandle (fd);
1501 if (h == INVALID_HANDLE_VALUE)
1503 const char *emsg = g_strerror (errsv);
1504 g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errsv),
1505 "_get_osfhandle() failed: %s", emsg);
1509 if (g_win32_handle_is_socket (h))
1511 WSAPROTOCOL_INFO info;
1513 if (WSADuplicateSocket ((SOCKET) h,
1514 GetCurrentProcessId (),
1517 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1518 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
1519 "WSADuplicateSocket() failed: %s", emsg);
1524 duph = (HANDLE) WSASocket (FROM_PROTOCOL_INFO,
1528 if (duph == (HANDLE) INVALID_SOCKET)
1530 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1531 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
1532 "WSASocket() failed: %s", emsg);
1537 else if (DuplicateHandle (GetCurrentProcess (), h,
1538 GetCurrentProcess (), &duph,
1539 0, FALSE, DUPLICATE_SAME_ACCESS) == 0)
1541 char *emsg = g_win32_error_message (GetLastError ());
1542 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
1543 "DuplicateHandle() failed: %s", emsg);
1548 /* the duph ownership is transferred to dupfd */
1549 dupfd = _open_osfhandle ((gintptr) duph, mode | _O_NOINHERIT);
1552 g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
1553 "_open_osfhandle() failed");
1558 if (!g_close (fd, error))
1560 /* ignore extra errors in this case */
1561 g_close (dupfd, NULL);