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 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__ */
53 #include "gthreadprivate.h"
56 #include <sys/cygwin.h>
62 g_win32_ftruncate (gint fd,
65 return _chsize (fd, size);
73 * The setlocale() function in the Microsoft C library uses locale
74 * names of the form "English_United States.1252" etc. We want the
75 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
76 * current thread locale from Windows - without any encoding info -
77 * and returns it as a string of the above form for use in forming
78 * file names etc. The returned string should be deallocated with
81 * Returns: newly-allocated locale name.
84 #ifndef SUBLANG_SERBIAN_LATIN_BA
85 #define SUBLANG_SERBIAN_LATIN_BA 0x06
89 g_win32_getlocale (void)
97 const gchar *script = NULL;
99 /* Let the user override the system settings through environment
100 * variables, as on POSIX systems. Note that in GTK+ applications
101 * since GTK+ 2.10.7 setting either LC_ALL or LANG also sets the
102 * Win32 locale and C library locale through code in gtkmain.c.
104 if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
105 || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
106 || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
107 return g_strdup (ev);
109 lcid = GetThreadLocale ();
111 if (!GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) ||
112 !GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
113 return g_strdup ("C");
115 /* Strip off the sorting rules, keep only the language part. */
116 langid = LANGIDFROMLCID (lcid);
118 /* Split into language and territory part. */
119 primary = PRIMARYLANGID (langid);
120 sub = SUBLANGID (langid);
122 /* Handle special cases */
128 case SUBLANG_AZERI_LATIN:
131 case SUBLANG_AZERI_CYRILLIC:
136 case LANG_SERBIAN: /* LANG_CROATIAN == LANG_SERBIAN */
139 case SUBLANG_SERBIAN_LATIN:
140 case 0x06: /* Serbian (Latin) - Bosnia and Herzegovina */
148 case SUBLANG_UZBEK_LATIN:
151 case SUBLANG_UZBEK_CYRILLIC:
157 return g_strconcat (iso639, "_", iso3166, script, NULL);
161 * g_win32_error_message:
162 * @error: error code.
164 * Translate a Win32 error code (as returned by GetLastError()) into
165 * the corresponding message. The message is either language neutral,
166 * or in the thread's language, or the user's language, the system's
167 * language, or US English (see docs for FormatMessage()). The
168 * returned string is in UTF-8. It should be deallocated with
171 * Returns: newly-allocated error message
174 g_win32_error_message (gint error)
180 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
181 |FORMAT_MESSAGE_IGNORE_INSERTS
182 |FORMAT_MESSAGE_FROM_SYSTEM,
184 (LPWSTR) &msg, 0, NULL);
187 nchars = wcslen (msg);
189 if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
190 msg[nchars-2] = '\0';
192 retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
197 retval = g_strdup ("");
203 * g_win32_get_package_installation_directory_of_module:
204 * @hmodule: (allow-none): The Win32 handle for a DLL loaded into the current process, or %NULL
206 * This function tries to determine the installation directory of a
207 * software package based on the location of a DLL of the software
210 * @hmodule should be the handle of a loaded DLL or %NULL. The
211 * function looks up the directory that DLL was loaded from. If
212 * @hmodule is NULL, the directory the main executable of the current
213 * process is looked up. If that directory's last component is "bin"
214 * or "lib", its parent directory is returned, otherwise the directory
217 * It thus makes sense to pass only the handle to a "public" DLL of a
218 * software package to this function, as such DLLs typically are known
219 * to be installed in a "bin" or occasionally "lib" subfolder of the
220 * installation folder. DLLs that are of the dynamically loaded module
221 * or plugin variety are often located in more private locations
222 * deeper down in the tree, from which it is impossible for GLib to
223 * deduce the root of the package installation.
225 * The typical use case for this function is to have a DllMain() that
226 * saves the handle for the DLL. Then when code in the DLL needs to
227 * construct names of files in the installation tree it calls this
228 * function passing the DLL handle.
230 * Returns: a string containing the guessed installation directory for
231 * the software package @hmodule is from. The string is in the GLib
232 * file name encoding, i.e. UTF-8. The return value should be freed
233 * with g_free() when not needed any longer. If the function fails
239 g_win32_get_package_installation_directory_of_module (gpointer hmodule)
243 wchar_t wc_fn[MAX_PATH];
245 if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
248 retval = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
250 if ((p = strrchr (retval, G_DIR_SEPARATOR)) != NULL)
253 p = strrchr (retval, G_DIR_SEPARATOR);
254 if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
255 g_ascii_strcasecmp (p + 1, "lib") == 0))
259 /* In Cygwin we need to have POSIX paths */
263 cygwin_conv_to_posix_path (retval, tmp);
265 retval = g_strdup (tmp);
273 get_package_directory_from_module (const gchar *module_name)
275 static GHashTable *module_dirs = NULL;
276 G_LOCK_DEFINE_STATIC (module_dirs);
277 HMODULE hmodule = NULL;
280 G_LOCK (module_dirs);
282 if (module_dirs == NULL)
283 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
285 fn = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
289 G_UNLOCK (module_dirs);
290 return g_strdup (fn);
295 wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
296 hmodule = GetModuleHandleW (wc_module_name);
297 g_free (wc_module_name);
301 G_UNLOCK (module_dirs);
306 fn = g_win32_get_package_installation_directory_of_module (hmodule);
310 G_UNLOCK (module_dirs);
314 g_hash_table_insert (module_dirs, module_name ? g_strdup (module_name) : "", fn);
316 G_UNLOCK (module_dirs);
318 return g_strdup (fn);
322 * g_win32_get_package_installation_directory:
323 * @package: (allow-none): You should pass %NULL for this.
324 * @dll_name: (allow-none): The name of a DLL that a package provides in UTF-8, or %NULL.
326 * Try to determine the installation directory for a software package.
328 * This function is deprecated. Use
329 * g_win32_get_package_installation_directory_of_module() instead.
331 * The use of @package is deprecated. You should always pass %NULL. A
332 * warning is printed if non-NULL is passed as @package.
334 * The original intended use of @package was for a short identifier of
335 * the package, typically the same identifier as used for
336 * `GETTEXT_PACKAGE` in software configured using GNU
337 * autotools. The function first looks in the Windows Registry for the
338 * value `#InstallationDirectory` in the key
339 * `#HKLM\Software\@package`, and if that value
340 * exists and is a string, returns that.
342 * It is strongly recommended that packagers of GLib-using libraries
343 * for Windows do not store installation paths in the Registry to be
344 * used by this function as that interfers with having several
345 * parallel installations of the library. Enabling multiple
346 * installations of different versions of some GLib-using library, or
347 * GLib itself, is desirable for various reasons.
349 * For this reason it is recommeded to always pass %NULL as
350 * @package to this function, to avoid the temptation to use the
351 * Registry. In version 2.20 of GLib the @package parameter
352 * will be ignored and this function won't look in the Registry at all.
354 * If @package is %NULL, or the above value isn't found in the
355 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
356 * into the current process. Typically that would be the name of the
357 * DLL calling this function, looking for its installation
358 * directory. The function then asks Windows what directory that DLL
359 * was loaded from. If that directory's last component is "bin" or
360 * "lib", the parent directory is returned, otherwise the directory
361 * itself. If that DLL isn't loaded, the function proceeds as if
362 * @dll_name was %NULL.
364 * If both @package and @dll_name are %NULL, the directory from where
365 * the main executable of the process was loaded is used instead in
366 * the same way as above.
368 * Returns: a string containing the installation directory for
369 * @package. The string is in the GLib file name encoding,
370 * i.e. UTF-8. The return value should be freed with g_free() when not
371 * needed any longer. If the function fails %NULL is returned.
373 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
374 * g_win32_get_package_installation_directory_of_module() instead.
378 g_win32_get_package_installation_directory_utf8 (const gchar *package,
379 const gchar *dll_name)
381 gchar *result = NULL;
384 g_warning ("Passing a non-NULL package to g_win32_get_package_installation_directory() is deprecated and it is ignored.");
386 if (dll_name != NULL)
387 result = get_package_directory_from_module (dll_name);
390 result = get_package_directory_from_module (NULL);
395 #if !defined (_WIN64)
397 /* DLL ABI binary compatibility version that uses system codepage file names */
400 g_win32_get_package_installation_directory (const gchar *package,
401 const gchar *dll_name)
403 gchar *utf8_package = NULL, *utf8_dll_name = NULL;
404 gchar *utf8_retval, *retval;
407 utf8_package = g_locale_to_utf8 (package, -1, NULL, NULL, NULL);
409 if (dll_name != NULL)
410 utf8_dll_name = g_locale_to_utf8 (dll_name, -1, NULL, NULL, NULL);
413 g_win32_get_package_installation_directory_utf8 (utf8_package,
416 retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
418 g_free (utf8_package);
419 g_free (utf8_dll_name);
420 g_free (utf8_retval);
428 * g_win32_get_package_installation_subdirectory:
429 * @package: (allow-none): You should pass %NULL for this.
430 * @dll_name: (allow-none): The name of a DLL that a package provides, in UTF-8, or %NULL.
431 * @subdir: A subdirectory of the package installation directory, also in UTF-8
433 * This function is deprecated. Use
434 * g_win32_get_package_installation_directory_of_module() and
435 * g_build_filename() instead.
437 * Returns a newly-allocated string containing the path of the
438 * subdirectory @subdir in the return value from calling
439 * g_win32_get_package_installation_directory() with the @package and
440 * @dll_name parameters. See the documentation for
441 * g_win32_get_package_installation_directory() for more details. In
442 * particular, note that it is deprecated to pass anything except NULL
445 * Returns: a string containing the complete path to @subdir inside
446 * the installation directory of @package. The returned string is in
447 * the GLib file name encoding, i.e. UTF-8. The return value should be
448 * freed with g_free() when no longer needed. If something goes wrong,
451 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
452 * g_win32_get_package_installation_directory_of_module() instead, and
453 * then construct a subdirectory pathname with g_build_filename().
457 g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
458 const gchar *dll_name,
464 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
465 prefix = g_win32_get_package_installation_directory_utf8 (package, dll_name);
466 G_GNUC_END_IGNORE_DEPRECATIONS
468 dirname = g_build_filename (prefix, subdir, NULL);
474 #if !defined (_WIN64)
476 /* DLL ABI binary compatibility version that uses system codepage file names */
479 g_win32_get_package_installation_subdirectory (const gchar *package,
480 const gchar *dll_name,
486 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
487 prefix = g_win32_get_package_installation_directory (package, dll_name);
488 G_GNUC_END_IGNORE_DEPRECATIONS
490 dirname = g_build_filename (prefix, subdir, NULL);
499 * g_win32_get_windows_version:
501 * Returns version information for the Windows operating system the
502 * code is running on. See MSDN documentation for the GetVersion()
503 * function. To summarize, the most significant bit is one on Win9x,
504 * and zero on NT-based systems. Since version 2.14, GLib works only
505 * on NT-based systems, so checking whether your are running on Win9x
506 * in your own software is moot. The least significant byte is 4 on
507 * Windows NT 4, and 5 on Windows XP. Software that needs really
508 * detailed version and feature information should use Win32 API like
509 * GetVersionEx() and VerifyVersionInfo().
511 * Returns: The version information.
516 g_win32_get_windows_version (void)
518 static gsize windows_version;
520 if (g_once_init_enter (&windows_version))
521 g_once_init_leave (&windows_version, GetVersion ());
523 return windows_version;
527 * g_win32_locale_filename_from_utf8:
528 * @utf8filename: a UTF-8 encoded filename.
530 * Converts a filename from UTF-8 to the system codepage.
532 * On NT-based Windows, on NTFS file systems, file names are in
533 * Unicode. It is quite possible that Unicode file names contain
534 * characters not representable in the system codepage. (For instance,
535 * Greek or Cyrillic characters on Western European or US Windows
536 * installations, or various less common CJK characters on CJK Windows
539 * In such a case, and if the filename refers to an existing file, and
540 * the file system stores alternate short (8.3) names for directory
541 * entries, the short form of the filename is returned. Note that the
542 * "short" name might in fact be longer than the Unicode name if the
543 * Unicode name has very short pathname components containing
544 * non-ASCII characters. If no system codepage name for the file is
545 * possible, %NULL is returned.
547 * The return value is dynamically allocated and should be freed with
548 * g_free() when no longer needed.
550 * Returns: The converted filename, or %NULL on conversion
551 * failure and lack of short names.
556 g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
558 gchar *retval = g_locale_from_utf8 (utf8filename, -1, NULL, NULL, NULL);
562 /* Conversion failed, so convert to wide chars, check if there
563 * is a 8.3 version, and use that.
565 wchar_t *wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL);
568 wchar_t wshortname[MAX_PATH + 1];
569 if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname)))
571 gchar *tem = g_utf16_to_utf8 (wshortname, -1, NULL, NULL, NULL);
572 retval = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);
582 * g_win32_get_command_line:
584 * Gets the command line arguments, on Windows, in the GLib filename
585 * encoding (ie: UTF-8).
587 * Normally, on Windows, the command line arguments are passed to main()
588 * in the system codepage encoding. This prevents passing filenames as
589 * arguments if the filenames contain characters that fall outside of
590 * this codepage. If such filenames are passed, then substitutions
591 * will occur (such as replacing some characters with '?').
593 * GLib's policy of using UTF-8 as a filename encoding on Windows was
594 * designed to localise the pain of dealing with filenames outside of
595 * the system codepage to one area: dealing with commandline arguments
598 * As such, most GLib programs should ignore the value of argv passed to
599 * their main() function and call g_win32_get_command_line() instead.
600 * This will get the "full Unicode" commandline arguments using
601 * GetCommandLineW() and convert it to the GLib filename encoding (which
602 * is UTF-8 on Windows).
604 * The strings returned by this function are suitable for use with
605 * functions such as g_open() and g_file_new_for_commandline_arg() but
606 * are not suitable for use with g_option_context_parse(), which assumes
607 * that its input will be in the system codepage. The return value is
608 * suitable for use with g_option_context_parse_strv(), however, which
609 * is a better match anyway because it won't leak memory.
611 * Unlike argv, the returned value is a normal strv and can (and should)
612 * be freed with g_strfreev() when no longer needed.
614 * Returns: (transfer full): the commandline arguments in the GLib
615 * filename encoding (ie: UTF-8)
620 g_win32_get_command_line (void)
626 args = CommandLineToArgvW (GetCommandLineW(), &n);
628 result = g_new (gchar *, n + 1);
629 for (i = 0; i < n; i++)
630 result[i] = g_utf16_to_utf8 (args[i], -1, NULL, NULL, NULL);