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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
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"
42 #define STRICT /* Strict typing, please */
50 #if defined(_MSC_VER) || defined(__DMC__)
52 #endif /* _MSC_VER || __DMC__ */
57 #include <sys/cygwin.h>
63 g_win32_ftruncate (gint fd,
66 return _chsize (fd, size);
74 * The setlocale() function in the Microsoft C library uses locale
75 * names of the form "English_United States.1252" etc. We want the
76 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
77 * current thread locale from Windows - without any encoding info -
78 * and returns it as a string of the above form for use in forming
79 * file names etc. The returned string should be deallocated with
82 * Returns: newly-allocated locale name.
85 #ifndef SUBLANG_SERBIAN_LATIN_BA
86 #define SUBLANG_SERBIAN_LATIN_BA 0x06
90 g_win32_getlocale (void)
98 const gchar *script = NULL;
100 /* Let the user override the system settings through environment
101 * variables, as on POSIX systems. Note that in GTK+ applications
102 * since GTK+ 2.10.7 setting either LC_ALL or LANG also sets the
103 * Win32 locale and C library locale through code in gtkmain.c.
105 if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
106 || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
107 || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
108 return g_strdup (ev);
110 lcid = GetThreadLocale ();
112 if (!GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) ||
113 !GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
114 return g_strdup ("C");
116 /* Strip off the sorting rules, keep only the language part. */
117 langid = LANGIDFROMLCID (lcid);
119 /* Split into language and territory part. */
120 primary = PRIMARYLANGID (langid);
121 sub = SUBLANGID (langid);
123 /* Handle special cases */
129 case SUBLANG_AZERI_LATIN:
132 case SUBLANG_AZERI_CYRILLIC:
137 case LANG_SERBIAN: /* LANG_CROATIAN == LANG_SERBIAN */
140 case SUBLANG_SERBIAN_LATIN:
141 case 0x06: /* Serbian (Latin) - Bosnia and Herzegovina */
149 case SUBLANG_UZBEK_LATIN:
152 case SUBLANG_UZBEK_CYRILLIC:
158 return g_strconcat (iso639, "_", iso3166, script, NULL);
162 * g_win32_error_message:
163 * @error: error code.
165 * Translate a Win32 error code (as returned by GetLastError()) into
166 * the corresponding message. The message is either language neutral,
167 * or in the thread's language, or the user's language, the system's
168 * language, or US English (see docs for FormatMessage()). The
169 * returned string is in UTF-8. It should be deallocated with
172 * Returns: newly-allocated error message
175 g_win32_error_message (gint error)
181 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
182 |FORMAT_MESSAGE_IGNORE_INSERTS
183 |FORMAT_MESSAGE_FROM_SYSTEM,
185 (LPWSTR) &msg, 0, NULL);
188 nchars = wcslen (msg);
190 if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
191 msg[nchars-2] = '\0';
193 retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
198 retval = g_strdup ("");
204 * g_win32_get_package_installation_directory_of_module:
205 * @hmodule: The Win32 handle for a DLL loaded into the current process, or %NULL
207 * This function tries to determine the installation directory of a
208 * software package based on the location of a DLL of the software
211 * @hmodule should be the handle of a loaded DLL or %NULL. The
212 * function looks up the directory that DLL was loaded from. If
213 * @hmodule is NULL, the directory the main executable of the current
214 * process is looked up. If that directory's last component is "bin"
215 * or "lib", its parent directory is returned, otherwise the directory
218 * It thus makes sense to pass only the handle to a "public" DLL of a
219 * software package to this function, as such DLLs typically are known
220 * to be installed in a "bin" or occasionally "lib" subfolder of the
221 * installation folder. DLLs that are of the dynamically loaded module
222 * or plugin variety are often located in more private locations
223 * deeper down in the tree, from which it is impossible for GLib to
224 * deduce the root of the package installation.
226 * The typical use case for this function is to have a DllMain() that
227 * saves the handle for the DLL. Then when code in the DLL needs to
228 * construct names of files in the installation tree it calls this
229 * function passing the DLL handle.
231 * Returns: a string containing the guessed installation directory for
232 * the software package @hmodule is from. The string is in the GLib
233 * file name encoding, i.e. UTF-8. The return value should be freed
234 * with g_free() when not needed any longer. If the function fails
240 g_win32_get_package_installation_directory_of_module (gpointer hmodule)
244 wchar_t wc_fn[MAX_PATH];
246 if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
249 retval = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
251 if ((p = strrchr (retval, G_DIR_SEPARATOR)) != NULL)
254 p = strrchr (retval, G_DIR_SEPARATOR);
255 if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
256 g_ascii_strcasecmp (p + 1, "lib") == 0))
260 /* In Cygwin we need to have POSIX paths */
264 cygwin_conv_to_posix_path (retval, tmp);
266 retval = g_strdup (tmp);
274 get_package_directory_from_module (const gchar *module_name)
276 static GHashTable *module_dirs = NULL;
277 G_LOCK_DEFINE_STATIC (module_dirs);
278 HMODULE hmodule = NULL;
281 G_LOCK (module_dirs);
283 if (module_dirs == NULL)
284 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
286 fn = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
290 G_UNLOCK (module_dirs);
291 return g_strdup (fn);
296 wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
297 hmodule = GetModuleHandleW (wc_module_name);
298 g_free (wc_module_name);
302 G_UNLOCK (module_dirs);
307 fn = g_win32_get_package_installation_directory_of_module (hmodule);
311 G_UNLOCK (module_dirs);
315 g_hash_table_insert (module_dirs, module_name ? g_strdup (module_name) : "", fn);
317 G_UNLOCK (module_dirs);
319 return g_strdup (fn);
323 * g_win32_get_package_installation_directory:
324 * @package: You should pass %NULL for this.
325 * @dll_name: The name of a DLL that a package provides in UTF-8, or %NULL.
327 * Try to determine the installation directory for a software package.
329 * This function is deprecated. Use
330 * g_win32_get_package_installation_directory_of_module() instead.
332 * The use of @package is deprecated. You should always pass %NULL. A
333 * warning is printed if non-NULL is passed as @package.
335 * The original intended use of @package was for a short identifier of
336 * the package, typically the same identifier as used for
337 * <literal>GETTEXT_PACKAGE</literal> in software configured using GNU
338 * autotools. The function first looks in the Windows Registry for the
339 * value <literal>#InstallationDirectory</literal> in the key
340 * <literal>#HKLM\Software\@package</literal>, and if that value
341 * exists and is a string, returns that.
343 * It is strongly recommended that packagers of GLib-using libraries
344 * for Windows do not store installation paths in the Registry to be
345 * used by this function as that interfers with having several
346 * parallel installations of the library. Enabling multiple
347 * installations of different versions of some GLib-using library, or
348 * GLib itself, is desirable for various reasons.
350 * For this reason it is recommeded to always pass %NULL as
351 * @package to this function, to avoid the temptation to use the
352 * Registry. In version 2.20 of GLib the @package parameter
353 * will be ignored and this function won't look in the Registry at all.
355 * If @package is %NULL, or the above value isn't found in the
356 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
357 * into the current process. Typically that would be the name of the
358 * DLL calling this function, looking for its installation
359 * directory. The function then asks Windows what directory that DLL
360 * was loaded from. If that directory's last component is "bin" or
361 * "lib", the parent directory is returned, otherwise the directory
362 * itself. If that DLL isn't loaded, the function proceeds as if
363 * @dll_name was %NULL.
365 * If both @package and @dll_name are %NULL, the directory from where
366 * the main executable of the process was loaded is used instead in
367 * the same way as above.
369 * Returns: a string containing the installation directory for
370 * @package. The string is in the GLib file name encoding,
371 * i.e. UTF-8. The return value should be freed with g_free() when not
372 * needed any longer. If the function fails %NULL is returned.
374 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
375 * g_win32_get_package_installation_directory_of_module() instead.
379 g_win32_get_package_installation_directory_utf8 (const gchar *package,
380 const gchar *dll_name)
382 gchar *result = NULL;
385 g_warning ("Passing a non-NULL package to g_win32_get_package_installation_directory() is deprecated and it is ignored.");
387 if (dll_name != NULL)
388 result = get_package_directory_from_module (dll_name);
391 result = get_package_directory_from_module (NULL);
396 #if !defined (_WIN64)
398 /* DLL ABI binary compatibility version that uses system codepage file names */
401 g_win32_get_package_installation_directory (const gchar *package,
402 const gchar *dll_name)
404 gchar *utf8_package = NULL, *utf8_dll_name = NULL;
405 gchar *utf8_retval, *retval;
408 utf8_package = g_locale_to_utf8 (package, -1, NULL, NULL, NULL);
410 if (dll_name != NULL)
411 utf8_dll_name = g_locale_to_utf8 (dll_name, -1, NULL, NULL, NULL);
414 g_win32_get_package_installation_directory_utf8 (utf8_package,
417 retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
419 g_free (utf8_package);
420 g_free (utf8_dll_name);
421 g_free (utf8_retval);
429 * g_win32_get_package_installation_subdirectory:
430 * @package: You should pass %NULL for this.
431 * @dll_name: The name of a DLL that a package provides, in UTF-8, or %NULL.
432 * @subdir: A subdirectory of the package installation directory, also in UTF-8
434 * This function is deprecated. Use
435 * g_win32_get_package_installation_directory_of_module() and
436 * g_build_filename() instead.
438 * Returns a newly-allocated string containing the path of the
439 * subdirectory @subdir in the return value from calling
440 * g_win32_get_package_installation_directory() with the @package and
441 * @dll_name parameters. See the documentation for
442 * g_win32_get_package_installation_directory() for more details. In
443 * particular, note that it is deprecated to pass anything except NULL
446 * Returns: a string containing the complete path to @subdir inside
447 * the installation directory of @package. The returned string is in
448 * the GLib file name encoding, i.e. UTF-8. The return value should be
449 * freed with g_free() when no longer needed. If something goes wrong,
452 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
453 * g_win32_get_package_installation_directory_of_module() instead, and
454 * then construct a subdirectory pathname with g_build_filename().
458 g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
459 const gchar *dll_name,
465 prefix = g_win32_get_package_installation_directory_utf8 (package, dll_name);
467 dirname = g_build_filename (prefix, subdir, NULL);
473 #if !defined (_WIN64)
475 /* DLL ABI binary compatibility version that uses system codepage file names */
478 g_win32_get_package_installation_subdirectory (const gchar *package,
479 const gchar *dll_name,
485 prefix = g_win32_get_package_installation_directory (package, dll_name);
487 dirname = g_build_filename (prefix, subdir, NULL);
495 static guint windows_version;
498 g_win32_windows_version_init (void)
500 static gboolean beenhere = FALSE;
505 windows_version = GetVersion ();
507 if (windows_version & 0x80000000)
508 g_error ("This version of GLib requires NT-based Windows.");
513 _g_win32_thread_init (void)
515 g_win32_windows_version_init ();
519 * g_win32_get_windows_version:
521 * Returns version information for the Windows operating system the
522 * code is running on. See MSDN documentation for the GetVersion()
523 * function. To summarize, the most significant bit is one on Win9x,
524 * and zero on NT-based systems. Since version 2.14, GLib works only
525 * on NT-based systems, so checking whether your are running on Win9x
526 * in your own software is moot. The least significant byte is 4 on
527 * Windows NT 4, and 5 on Windows XP. Software that needs really
528 * detailled version and feature information should use Win32 API like
529 * GetVersionEx() and VerifyVersionInfo().
531 * Returns: The version information.
536 g_win32_get_windows_version (void)
538 g_win32_windows_version_init ();
540 return windows_version;
544 * g_win32_locale_filename_from_utf8:
545 * @utf8filename: a UTF-8 encoded filename.
547 * Converts a filename from UTF-8 to the system codepage.
549 * On NT-based Windows, on NTFS file systems, file names are in
550 * Unicode. It is quite possible that Unicode file names contain
551 * characters not representable in the system codepage. (For instance,
552 * Greek or Cyrillic characters on Western European or US Windows
553 * installations, or various less common CJK characters on CJK Windows
556 * In such a case, and if the filename refers to an existing file, and
557 * the file system stores alternate short (8.3) names for directory
558 * entries, the short form of the filename is returned. Note that the
559 * "short" name might in fact be longer than the Unicode name if the
560 * Unicode name has very short pathname components containing
561 * non-ASCII characters. If no system codepage name for the file is
562 * possible, %NULL is returned.
564 * The return value is dynamically allocated and should be freed with
565 * g_free() when no longer needed.
567 * Return value: The converted filename, or %NULL on conversion
568 * failure and lack of short names.
573 g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
575 gchar *retval = g_locale_from_utf8 (utf8filename, -1, NULL, NULL, NULL);
579 /* Conversion failed, so convert to wide chars, check if there
580 * is a 8.3 version, and use that.
582 wchar_t *wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL);
585 wchar_t wshortname[MAX_PATH + 1];
586 if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname)))
588 gchar *tem = g_utf16_to_utf8 (wshortname, -1, NULL, NULL, NULL);
589 retval = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);