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 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GLib Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
32 #include "glibconfig.h"
40 #define STRICT /* Strict typing, please */
48 #if defined(_MSC_VER) || defined(__DMC__)
50 #endif /* _MSC_VER || __DMC__ */
52 #define MODERN_API_FAMILY 2
54 #if WINAPI_FAMILY == MODERN_API_FAMILY
55 /* This is for modern UI Builds, where we can't use LoadLibraryW()/GetProcAddress() */
56 /* ntddk.h is found in the WDK, and MinGW */
60 #pragma comment (lib, "ntoskrnl.lib")
62 #elif defined (__MINGW32__)
63 /* mingw-w64, not MinGW, has winternl.h */
70 #include "gthreadprivate.h"
73 #include <sys/cygwin.h>
79 g_win32_ftruncate (gint fd
,
82 return _chsize (fd
, size
);
90 * The setlocale() function in the Microsoft C library uses locale
91 * names of the form "English_United States.1252" etc. We want the
92 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
93 * current thread locale from Windows - without any encoding info -
94 * and returns it as a string of the above form for use in forming
95 * file names etc. The returned string should be deallocated with
98 * Returns: newly-allocated locale name.
101 #ifndef SUBLANG_SERBIAN_LATIN_BA
102 #define SUBLANG_SERBIAN_LATIN_BA 0x06
106 g_win32_getlocale (void)
114 const gchar
*script
= NULL
;
116 /* Let the user override the system settings through environment
117 * variables, as on POSIX systems. Note that in GTK+ applications
118 * since GTK+ 2.10.7 setting either LC_ALL or LANG also sets the
119 * Win32 locale and C library locale through code in gtkmain.c.
121 if (((ev
= getenv ("LC_ALL")) != NULL
&& ev
[0] != '\0')
122 || ((ev
= getenv ("LC_MESSAGES")) != NULL
&& ev
[0] != '\0')
123 || ((ev
= getenv ("LANG")) != NULL
&& ev
[0] != '\0'))
124 return g_strdup (ev
);
126 lcid
= GetThreadLocale ();
128 if (!GetLocaleInfo (lcid
, LOCALE_SISO639LANGNAME
, iso639
, sizeof (iso639
)) ||
129 !GetLocaleInfo (lcid
, LOCALE_SISO3166CTRYNAME
, iso3166
, sizeof (iso3166
)))
130 return g_strdup ("C");
132 /* Strip off the sorting rules, keep only the language part. */
133 langid
= LANGIDFROMLCID (lcid
);
135 /* Split into language and territory part. */
136 primary
= PRIMARYLANGID (langid
);
137 sub
= SUBLANGID (langid
);
139 /* Handle special cases */
145 case SUBLANG_AZERI_LATIN
:
148 case SUBLANG_AZERI_CYRILLIC
:
153 case LANG_SERBIAN
: /* LANG_CROATIAN == LANG_SERBIAN */
156 case SUBLANG_SERBIAN_LATIN
:
157 case 0x06: /* Serbian (Latin) - Bosnia and Herzegovina */
165 case SUBLANG_UZBEK_LATIN
:
168 case SUBLANG_UZBEK_CYRILLIC
:
174 return g_strconcat (iso639
, "_", iso3166
, script
, NULL
);
178 * g_win32_error_message:
179 * @error: error code.
181 * Translate a Win32 error code (as returned by GetLastError() or
182 * WSAGetLastError()) into the corresponding message. The message is
183 * either language neutral, or in the thread's language, or the user's
184 * language, the system's language, or US English (see docs for
185 * FormatMessage()). The returned string is in UTF-8. It should be
186 * deallocated with g_free().
188 * Returns: newly-allocated error message
191 g_win32_error_message (gint error
)
197 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
198 |FORMAT_MESSAGE_IGNORE_INSERTS
199 |FORMAT_MESSAGE_FROM_SYSTEM
,
201 (LPWSTR
) &msg
, 0, NULL
);
204 nchars
= wcslen (msg
);
206 if (nchars
>= 2 && msg
[nchars
-1] == L
'\n' && msg
[nchars
-2] == L
'\r')
207 msg
[nchars
-2] = L
'\0';
209 retval
= g_utf16_to_utf8 (msg
, -1, NULL
, NULL
, NULL
);
214 retval
= g_strdup ("");
220 * g_win32_get_package_installation_directory_of_module:
221 * @hmodule: (nullable): The Win32 handle for a DLL loaded into the current process, or %NULL
223 * This function tries to determine the installation directory of a
224 * software package based on the location of a DLL of the software
227 * @hmodule should be the handle of a loaded DLL or %NULL. The
228 * function looks up the directory that DLL was loaded from. If
229 * @hmodule is NULL, the directory the main executable of the current
230 * process is looked up. If that directory's last component is "bin"
231 * or "lib", its parent directory is returned, otherwise the directory
234 * It thus makes sense to pass only the handle to a "public" DLL of a
235 * software package to this function, as such DLLs typically are known
236 * to be installed in a "bin" or occasionally "lib" subfolder of the
237 * installation folder. DLLs that are of the dynamically loaded module
238 * or plugin variety are often located in more private locations
239 * deeper down in the tree, from which it is impossible for GLib to
240 * deduce the root of the package installation.
242 * The typical use case for this function is to have a DllMain() that
243 * saves the handle for the DLL. Then when code in the DLL needs to
244 * construct names of files in the installation tree it calls this
245 * function passing the DLL handle.
247 * Returns: a string containing the guessed installation directory for
248 * the software package @hmodule is from. The string is in the GLib
249 * file name encoding, i.e. UTF-8. The return value should be freed
250 * with g_free() when not needed any longer. If the function fails
256 g_win32_get_package_installation_directory_of_module (gpointer hmodule
)
261 wchar_t wc_fn
[MAX_PATH
];
263 /* NOTE: it relies that GetModuleFileNameW returns only canonical paths */
264 if (!GetModuleFileNameW (hmodule
, wc_fn
, MAX_PATH
))
267 filename
= g_utf16_to_utf8 (wc_fn
, -1, NULL
, NULL
, NULL
);
269 if ((p
= strrchr (filename
, G_DIR_SEPARATOR
)) != NULL
)
272 retval
= g_strdup (filename
);
276 p
= strrchr (retval
, G_DIR_SEPARATOR
);
282 if (g_ascii_strcasecmp (p
+ 1, "bin") == 0 ||
283 g_ascii_strcasecmp (p
+ 1, "lib") == 0)
297 /* In Cygwin we need to have POSIX paths */
301 cygwin_conv_to_posix_path (retval
, tmp
);
303 retval
= g_strdup (tmp
);
311 get_package_directory_from_module (const gchar
*module_name
)
313 static GHashTable
*module_dirs
= NULL
;
314 G_LOCK_DEFINE_STATIC (module_dirs
);
315 HMODULE hmodule
= NULL
;
318 G_LOCK (module_dirs
);
320 if (module_dirs
== NULL
)
321 module_dirs
= g_hash_table_new (g_str_hash
, g_str_equal
);
323 fn
= g_hash_table_lookup (module_dirs
, module_name
? module_name
: "");
327 G_UNLOCK (module_dirs
);
328 return g_strdup (fn
);
333 wchar_t *wc_module_name
= g_utf8_to_utf16 (module_name
, -1, NULL
, NULL
, NULL
);
334 hmodule
= GetModuleHandleW (wc_module_name
);
335 g_free (wc_module_name
);
339 G_UNLOCK (module_dirs
);
344 fn
= g_win32_get_package_installation_directory_of_module (hmodule
);
348 G_UNLOCK (module_dirs
);
352 g_hash_table_insert (module_dirs
, module_name
? g_strdup (module_name
) : "", fn
);
354 G_UNLOCK (module_dirs
);
356 return g_strdup (fn
);
360 * g_win32_get_package_installation_directory:
361 * @package: (nullable): You should pass %NULL for this.
362 * @dll_name: (nullable): The name of a DLL that a package provides in UTF-8, or %NULL.
364 * Try to determine the installation directory for a software package.
366 * This function is deprecated. Use
367 * g_win32_get_package_installation_directory_of_module() instead.
369 * The use of @package is deprecated. You should always pass %NULL. A
370 * warning is printed if non-NULL is passed as @package.
372 * The original intended use of @package was for a short identifier of
373 * the package, typically the same identifier as used for
374 * `GETTEXT_PACKAGE` in software configured using GNU
375 * autotools. The function first looks in the Windows Registry for the
376 * value `#InstallationDirectory` in the key
377 * `#HKLM\Software\@package`, and if that value
378 * exists and is a string, returns that.
380 * It is strongly recommended that packagers of GLib-using libraries
381 * for Windows do not store installation paths in the Registry to be
382 * used by this function as that interfers with having several
383 * parallel installations of the library. Enabling multiple
384 * installations of different versions of some GLib-using library, or
385 * GLib itself, is desirable for various reasons.
387 * For this reason it is recommeded to always pass %NULL as
388 * @package to this function, to avoid the temptation to use the
389 * Registry. In version 2.20 of GLib the @package parameter
390 * will be ignored and this function won't look in the Registry at all.
392 * If @package is %NULL, or the above value isn't found in the
393 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
394 * into the current process. Typically that would be the name of the
395 * DLL calling this function, looking for its installation
396 * directory. The function then asks Windows what directory that DLL
397 * was loaded from. If that directory's last component is "bin" or
398 * "lib", the parent directory is returned, otherwise the directory
399 * itself. If that DLL isn't loaded, the function proceeds as if
400 * @dll_name was %NULL.
402 * If both @package and @dll_name are %NULL, the directory from where
403 * the main executable of the process was loaded is used instead in
404 * the same way as above.
406 * Returns: a string containing the installation directory for
407 * @package. The string is in the GLib file name encoding,
408 * i.e. UTF-8. The return value should be freed with g_free() when not
409 * needed any longer. If the function fails %NULL is returned.
411 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
412 * g_win32_get_package_installation_directory_of_module() instead.
416 g_win32_get_package_installation_directory (const gchar
*package
,
417 const gchar
*dll_name
)
419 gchar
*result
= NULL
;
422 g_warning ("Passing a non-NULL package to g_win32_get_package_installation_directory() is deprecated and it is ignored.");
424 if (dll_name
!= NULL
)
425 result
= get_package_directory_from_module (dll_name
);
428 result
= get_package_directory_from_module (NULL
);
434 * g_win32_get_package_installation_subdirectory:
435 * @package: (nullable): You should pass %NULL for this.
436 * @dll_name: (nullable): The name of a DLL that a package provides, in UTF-8, or %NULL.
437 * @subdir: A subdirectory of the package installation directory, also in UTF-8
439 * This function is deprecated. Use
440 * g_win32_get_package_installation_directory_of_module() and
441 * g_build_filename() instead.
443 * Returns a newly-allocated string containing the path of the
444 * subdirectory @subdir in the return value from calling
445 * g_win32_get_package_installation_directory() with the @package and
446 * @dll_name parameters. See the documentation for
447 * g_win32_get_package_installation_directory() for more details. In
448 * particular, note that it is deprecated to pass anything except NULL
451 * Returns: a string containing the complete path to @subdir inside
452 * the installation directory of @package. The returned string is in
453 * the GLib file name encoding, i.e. UTF-8. The return value should be
454 * freed with g_free() when no longer needed. If something goes wrong,
457 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
458 * g_win32_get_package_installation_directory_of_module() instead, and
459 * then construct a subdirectory pathname with g_build_filename().
463 g_win32_get_package_installation_subdirectory (const gchar
*package
,
464 const gchar
*dll_name
,
470 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
471 prefix
= g_win32_get_package_installation_directory (package
, dll_name
);
472 G_GNUC_END_IGNORE_DEPRECATIONS
474 dirname
= g_build_filename (prefix
, subdir
, NULL
);
481 * g_win32_check_windows_version:
482 * @major: major version of Windows
483 * @minor: minor version of Windows
484 * @spver: Windows Service Pack Level, 0 if none
485 * @os_type: Type of Windows OS
487 * Returns whether the version of the Windows operating system the
488 * code is running on is at least the specified major, minor and
489 * service pack versions. See MSDN documentation for the Operating
490 * System Version. Software that needs even more detailed version and
491 * feature information should use the Win32 API VerifyVersionInfo()
494 * Successive calls of this function can be used for enabling or
495 * disabling features at run-time for a range of Windows versions,
496 * as per the VerifyVersionInfo() API documentation.
498 * Returns: %TRUE if the Windows Version is the same or greater than
499 * the specified major, minor and service pack versions, and
500 * whether the running Windows is a workstation or server edition
501 * of Windows, if specifically specified.
506 g_win32_check_windows_version (const gint major
,
509 const GWin32OSType os_type
)
511 OSVERSIONINFOEXW osverinfo
;
512 gboolean is_ver_checked
= FALSE
;
513 gboolean is_type_checked
= FALSE
;
515 #if WINAPI_FAMILY != MODERN_API_FAMILY
516 /* For non-modern UI Apps, use the LoadLibraryW()/GetProcAddress() thing */
517 typedef NTSTATUS (WINAPI fRtlGetVersion
) (PRTL_OSVERSIONINFOEXW
);
519 fRtlGetVersion
*RtlGetVersion
;
522 /* We Only Support Checking for XP or later */
523 g_return_val_if_fail (major
>= 5 && (major
<=6 || major
== 10), FALSE
);
524 g_return_val_if_fail ((major
>= 5 && minor
>= 1) || major
>= 6, FALSE
);
526 /* Check for Service Pack Version >= 0 */
527 g_return_val_if_fail (spver
>= 0, FALSE
);
529 #if WINAPI_FAMILY != MODERN_API_FAMILY
530 hmodule
= LoadLibraryW (L
"ntdll.dll");
531 g_return_val_if_fail (hmodule
!= NULL
, FALSE
);
533 RtlGetVersion
= (fRtlGetVersion
*) GetProcAddress (hmodule
, "RtlGetVersion");
534 g_return_val_if_fail (RtlGetVersion
!= NULL
, FALSE
);
537 memset (&osverinfo
, 0, sizeof (OSVERSIONINFOEXW
));
538 osverinfo
.dwOSVersionInfoSize
= sizeof (OSVERSIONINFOEXW
);
539 RtlGetVersion (&osverinfo
);
541 /* check the OS and Service Pack Versions */
542 if (osverinfo
.dwMajorVersion
> major
)
543 is_ver_checked
= TRUE
;
544 else if (osverinfo
.dwMajorVersion
== major
)
546 if (osverinfo
.dwMinorVersion
> minor
)
547 is_ver_checked
= TRUE
;
548 else if (osverinfo
.dwMinorVersion
== minor
)
549 if (osverinfo
.wServicePackMajor
>= spver
)
550 is_ver_checked
= TRUE
;
559 is_type_checked
= TRUE
;
561 case G_WIN32_OS_WORKSTATION
:
562 if (osverinfo
.wProductType
== VER_NT_WORKSTATION
)
563 is_type_checked
= TRUE
;
565 case G_WIN32_OS_SERVER
:
566 if (osverinfo
.wProductType
== VER_NT_SERVER
||
567 osverinfo
.wProductType
== VER_NT_DOMAIN_CONTROLLER
)
568 is_type_checked
= TRUE
;
571 /* shouldn't get here normally */
572 g_warning ("Invalid os_type specified");
577 #if WINAPI_FAMILY != MODERN_API_FAMILY
578 FreeLibrary (hmodule
);
581 return is_ver_checked
&& is_type_checked
;
585 * g_win32_get_windows_version:
587 * This function is deprecated. Use
588 * g_win32_check_windows_version() instead.
590 * Returns version information for the Windows operating system the
591 * code is running on. See MSDN documentation for the GetVersion()
592 * function. To summarize, the most significant bit is one on Win9x,
593 * and zero on NT-based systems. Since version 2.14, GLib works only
594 * on NT-based systems, so checking whether your are running on Win9x
595 * in your own software is moot. The least significant byte is 4 on
596 * Windows NT 4, and 5 on Windows XP. Software that needs really
597 * detailed version and feature information should use Win32 API like
598 * GetVersionEx() and VerifyVersionInfo().
600 * Returns: The version information.
602 * Deprecated: 2.44: Be aware that for Windows 8.1 and Windows Server
603 * 2012 R2 and later, this will return 62 unless the application is
604 * manifested for Windows 8.1/Windows Server 2012 R2, for example.
605 * MSDN stated that GetVersion(), which is used here, is subject to
606 * further change or removal after Windows 8.1.
609 g_win32_get_windows_version (void)
611 static gsize windows_version
;
613 if (g_once_init_enter (&windows_version
))
614 g_once_init_leave (&windows_version
, GetVersion ());
616 return windows_version
;
620 * Doesn't use gettext (and gconv), preventing recursive calls when
621 * g_win32_locale_filename_from_utf8() is called during
622 * gettext initialization.
625 special_wchar_to_locale_enoding (wchar_t *wstring
)
630 BOOL not_representable
= FALSE
;
632 sizeof_output
= WideCharToMultiByte (CP_ACP
,
633 WC_NO_BEST_FIT_CHARS
,
639 if (not_representable
||
640 sizeof_output
== 0 ||
641 sizeof_output
> MAX_PATH
)
644 result
= g_malloc0 (sizeof_output
+ 1);
646 wctmb_result
= WideCharToMultiByte (CP_ACP
,
647 WC_NO_BEST_FIT_CHARS
,
649 result
, sizeof_output
+ 1,
653 if (wctmb_result
== sizeof_output
&&
654 not_representable
== FALSE
)
663 * g_win32_locale_filename_from_utf8:
664 * @utf8filename: a UTF-8 encoded filename.
666 * Converts a filename from UTF-8 to the system codepage.
668 * On NT-based Windows, on NTFS file systems, file names are in
669 * Unicode. It is quite possible that Unicode file names contain
670 * characters not representable in the system codepage. (For instance,
671 * Greek or Cyrillic characters on Western European or US Windows
672 * installations, or various less common CJK characters on CJK Windows
675 * In such a case, and if the filename refers to an existing file, and
676 * the file system stores alternate short (8.3) names for directory
677 * entries, the short form of the filename is returned. Note that the
678 * "short" name might in fact be longer than the Unicode name if the
679 * Unicode name has very short pathname components containing
680 * non-ASCII characters. If no system codepage name for the file is
681 * possible, %NULL is returned.
683 * The return value is dynamically allocated and should be freed with
684 * g_free() when no longer needed.
686 * Returns: The converted filename, or %NULL on conversion
687 * failure and lack of short names.
692 g_win32_locale_filename_from_utf8 (const gchar
*utf8filename
)
697 wname
= g_utf8_to_utf16 (utf8filename
, -1, NULL
, NULL
, NULL
);
702 retval
= special_wchar_to_locale_enoding (wname
);
706 /* Conversion failed, so check if there is a 8.3 version, and use that. */
707 wchar_t wshortname
[MAX_PATH
+ 1];
709 if (GetShortPathNameW (wname
, wshortname
, G_N_ELEMENTS (wshortname
)))
710 retval
= special_wchar_to_locale_enoding (wshortname
);
719 * g_win32_get_command_line:
721 * Gets the command line arguments, on Windows, in the GLib filename
722 * encoding (ie: UTF-8).
724 * Normally, on Windows, the command line arguments are passed to main()
725 * in the system codepage encoding. This prevents passing filenames as
726 * arguments if the filenames contain characters that fall outside of
727 * this codepage. If such filenames are passed, then substitutions
728 * will occur (such as replacing some characters with '?').
730 * GLib's policy of using UTF-8 as a filename encoding on Windows was
731 * designed to localise the pain of dealing with filenames outside of
732 * the system codepage to one area: dealing with commandline arguments
735 * As such, most GLib programs should ignore the value of argv passed to
736 * their main() function and call g_win32_get_command_line() instead.
737 * This will get the "full Unicode" commandline arguments using
738 * GetCommandLineW() and convert it to the GLib filename encoding (which
739 * is UTF-8 on Windows).
741 * The strings returned by this function are suitable for use with
742 * functions such as g_open() and g_file_new_for_commandline_arg() but
743 * are not suitable for use with g_option_context_parse(), which assumes
744 * that its input will be in the system codepage. The return value is
745 * suitable for use with g_option_context_parse_strv(), however, which
746 * is a better match anyway because it won't leak memory.
748 * Unlike argv, the returned value is a normal strv and can (and should)
749 * be freed with g_strfreev() when no longer needed.
751 * Returns: (transfer full): the commandline arguments in the GLib
752 * filename encoding (ie: UTF-8)
757 g_win32_get_command_line (void)
763 args
= CommandLineToArgvW (GetCommandLineW(), &n
);
765 result
= g_new (gchar
*, n
+ 1);
766 for (i
= 0; i
< n
; i
++)
767 result
[i
] = g_utf16_to_utf8 (args
[i
], -1, NULL
, NULL
, NULL
);
776 /* Binary compatibility versions. Not for newly compiled code. */
778 _GLIB_EXTERN gchar
*g_win32_get_package_installation_directory_utf8 (const gchar
*package
,
779 const gchar
*dll_name
);
781 _GLIB_EXTERN gchar
*g_win32_get_package_installation_subdirectory_utf8 (const gchar
*package
,
782 const gchar
*dll_name
,
783 const gchar
*subdir
);
786 g_win32_get_package_installation_directory_utf8 (const gchar
*package
,
787 const gchar
*dll_name
)
789 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
790 return g_win32_get_package_installation_directory (package
, dll_name
);
791 G_GNUC_END_IGNORE_DEPRECATIONS
795 g_win32_get_package_installation_subdirectory_utf8 (const gchar
*package
,
796 const gchar
*dll_name
,
799 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
800 return g_win32_get_package_installation_subdirectory (package
,
803 G_GNUC_END_IGNORE_DEPRECATIONS