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__ */
58 #include <sys/cygwin.h>
64 g_win32_ftruncate (gint fd,
67 return _chsize (fd, size);
75 * The setlocale() function in the Microsoft C library uses locale
76 * names of the form "English_United States.1252" etc. We want the
77 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
78 * current thread locale from Windows - without any encoding info -
79 * and returns it as a string of the above form for use in forming
80 * file names etc. The returned string should be deallocated with
83 * Returns: newly-allocated locale name.
86 #ifndef SUBLANG_SERBIAN_LATIN_BA
87 #define SUBLANG_SERBIAN_LATIN_BA 0x06
91 g_win32_getlocale (void)
99 const gchar *script = NULL;
101 /* Let the user override the system settings through environment
102 * variables, as on POSIX systems. Note that in GTK+ applications
103 * since GTK+ 2.10.7 setting either LC_ALL or LANG also sets the
104 * Win32 locale and C library locale through code in gtkmain.c.
106 if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
107 || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
108 || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
109 return g_strdup (ev);
111 lcid = GetThreadLocale ();
113 if (!GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) ||
114 !GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
115 return g_strdup ("C");
117 /* Strip off the sorting rules, keep only the language part. */
118 langid = LANGIDFROMLCID (lcid);
120 /* Split into language and territory part. */
121 primary = PRIMARYLANGID (langid);
122 sub = SUBLANGID (langid);
124 /* Handle special cases */
130 case SUBLANG_AZERI_LATIN:
133 case SUBLANG_AZERI_CYRILLIC:
138 case LANG_SERBIAN: /* LANG_CROATIAN == LANG_SERBIAN */
141 case SUBLANG_SERBIAN_LATIN:
142 case 0x06: /* Serbian (Latin) - Bosnia and Herzegovina */
150 case SUBLANG_UZBEK_LATIN:
153 case SUBLANG_UZBEK_CYRILLIC:
159 return g_strconcat (iso639, "_", iso3166, script, NULL);
163 * g_win32_error_message:
164 * @error: error code.
166 * Translate a Win32 error code (as returned by GetLastError()) into
167 * the corresponding message. The message is either language neutral,
168 * or in the thread's language, or the user's language, the system's
169 * language, or US English (see docs for FormatMessage()). The
170 * returned string is in UTF-8. It should be deallocated with
173 * Returns: newly-allocated error message
176 g_win32_error_message (gint error)
182 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
183 |FORMAT_MESSAGE_IGNORE_INSERTS
184 |FORMAT_MESSAGE_FROM_SYSTEM,
186 (LPWSTR) &msg, 0, NULL);
189 nchars = wcslen (msg);
191 if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
192 msg[nchars-2] = '\0';
194 retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
199 retval = g_strdup ("");
205 get_package_directory_from_module (const gchar *module_name)
207 static GHashTable *module_dirs = NULL;
208 G_LOCK_DEFINE_STATIC (module_dirs);
209 HMODULE hmodule = NULL;
213 wchar_t wc_fn[MAX_PATH];
215 G_LOCK (module_dirs);
217 if (module_dirs == NULL)
218 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
220 result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
224 G_UNLOCK (module_dirs);
225 return g_strdup (result);
230 wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
231 hmodule = GetModuleHandleW (wc_module_name);
232 g_free (wc_module_name);
238 if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
240 G_UNLOCK (module_dirs);
243 fn = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
245 if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
248 p = strrchr (fn, G_DIR_SEPARATOR);
249 if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
250 g_ascii_strcasecmp (p + 1, "lib") == 0))
254 /* In Cygwin we need to have POSIX paths */
258 cygwin_conv_to_posix_path(fn, tmp);
264 g_hash_table_insert (module_dirs, module_name ? g_strdup (module_name) : "", fn);
266 G_UNLOCK (module_dirs);
268 return g_strdup (fn);
272 * g_win32_get_package_installation_directory:
273 * @package: You should pass %NULL for this.
274 * @dll_name: The name of a DLL that a package provides in UTF-8, or %NULL.
276 * Try to determine the installation directory for a software package.
278 * The use of @package is deprecated. You should always pass %NULL.
280 * Its original intended use was for a short identifier for
281 * the package, typically the same identifier as used for
282 * <literal>GETTEXT_PACKAGE</literal> in software configured using GNU
283 * autotools. The function first looks in the Windows Registry for the
284 * value <literal>#InstallationDirectory</literal> in the key
285 * <literal>#HKLM\Software\@package</literal>, and if that value
286 * exists and is a string, returns that.
288 * It is strongly recommended that packagers of GLib-using libraries
289 * for Windows do not store installation paths in the Registry to be
290 * used by this function as that interfers with having several
291 * parallel installations of the library. Enabling multiple
292 * installations of different versions of some GLib-using library, or
293 * GLib itself, is desirable for various reasons.
295 * For this reason it is recommeded to always pass %NULL as
296 * @package to this function, to avoid the temptation to use the
297 * Registry. In version 2.18 of GLib the @package parameter
298 * will be ignored and this function won't look in the Registry at all.
300 * If @package is %NULL, or the above value isn't found in the
301 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
302 * into the current process. Typically that would be the name of the
303 * DLL calling this function, looking for its installation
304 * directory. The function then asks Windows what directory that DLL
305 * was loaded from. If that directory's last component is "bin" or
306 * "lib", the parent directory is returned, otherwise the directory
307 * itself. If that DLL isn't loaded, the function proceeds as if
308 * @dll_name was %NULL.
310 * If both @package and @dll_name are %NULL, the directory from where
311 * the main executable of the process was loaded is used instead in
312 * the same way as above.
314 * Returns: a string containing the installation directory for
315 * @package. The string is in the GLib file name encoding, i.e. UTF-8
316 * on Windows. The return value should be freed with g_free() when not
321 g_win32_get_package_installation_directory (const gchar *package,
322 const gchar *dll_name)
324 static GHashTable *package_dirs = NULL;
325 G_LOCK_DEFINE_STATIC (package_dirs);
326 gchar *result = NULL;
335 G_LOCK (package_dirs);
337 if (package_dirs == NULL)
338 package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
340 result = g_hash_table_lookup (package_dirs, package);
342 if (result && result[0])
344 G_UNLOCK (package_dirs);
345 return g_strdup (result);
348 key = g_strconcat ("Software\\", package, NULL);
352 wc_key = g_utf8_to_utf16 (key, -1, NULL, NULL, NULL);
353 if (((RegOpenKeyExW (HKEY_CURRENT_USER, wc_key, 0,
354 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
355 && RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
356 &type, NULL, &nbytes) == ERROR_SUCCESS)
358 (RegOpenKeyExW (HKEY_LOCAL_MACHINE, wc_key, 0,
359 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
360 && RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
361 &type, NULL, &nbytes) == ERROR_SUCCESS))
364 wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
365 RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
366 &type, (LPBYTE) wc_temp, &nbytes);
367 wc_temp[nbytes/2] = '\0';
368 result = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
374 RegCloseKey (reg_key);
380 g_hash_table_insert (package_dirs, g_strdup (package), result);
381 G_UNLOCK (package_dirs);
382 return g_strdup (result);
384 G_UNLOCK (package_dirs);
387 if (dll_name != NULL)
388 result = get_package_directory_from_module (dll_name);
391 result = get_package_directory_from_module (NULL);
396 #undef g_win32_get_package_installation_directory
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);
427 * g_win32_get_package_installation_subdirectory:
428 * @package: You should pass %NULL for this.
429 * @dll_name: The name of a DLL that a package provides, in UTF-8, or %NULL.
430 * @subdir: A subdirectory of the package installation directory, also in UTF-8
432 * Returns a newly-allocated string containing the path of the
433 * subdirectory @subdir in the return value from calling
434 * g_win32_get_package_installation_directory() with the @package and
435 * @dll_name parameters. See the documentation for
436 * g_win32_get_package_installation_directory() for more details. In
437 * particular, note that it is deprecated to pass anything except NULL
440 * Returns: a string containing the complete path to @subdir inside
441 * the installation directory of @package. The returned string is in
442 * the GLib file name encoding, i.e. UTF-8 on Windows. The return
443 * value should be freed with g_free() when no longer needed.
447 g_win32_get_package_installation_subdirectory (const gchar *package,
448 const gchar *dll_name,
454 prefix = g_win32_get_package_installation_directory_utf8 (package, dll_name);
456 dirname = g_build_filename (prefix, subdir, NULL);
462 #undef g_win32_get_package_installation_subdirectory
464 /* DLL ABI binary compatibility version that uses system codepage file names */
467 g_win32_get_package_installation_subdirectory (const gchar *package,
468 const gchar *dll_name,
474 prefix = g_win32_get_package_installation_directory (package, dll_name);
476 dirname = g_build_filename (prefix, subdir, NULL);
482 static guint windows_version;
485 g_win32_windows_version_init (void)
487 static gboolean beenhere = FALSE;
492 windows_version = GetVersion ();
494 if (windows_version & 0x80000000)
495 g_error ("This version of GLib requires NT-based Windows.");
500 _g_win32_thread_init (void)
502 g_win32_windows_version_init ();
506 * g_win32_get_windows_version:
508 * Returns version information for the Windows operating system the
509 * code is running on. See MSDN documentation for the GetVersion()
510 * function. To summarize, the most significant bit is one on Win9x,
511 * and zero on NT-based systems. Since version 2.14, GLib works only
512 * on NT-based systems, so checking whether your are running on Win9x
513 * in your own software is moot. The least significant byte is 4 on
514 * Windows NT 4, and 5 on Windows XP. Software that needs really
515 * detailled version and feature information should use Win32 API like
516 * GetVersionEx() and VerifyVersionInfo().
518 * Returns: The version information.
523 g_win32_get_windows_version (void)
525 g_win32_windows_version_init ();
527 return windows_version;
531 * g_win32_locale_filename_from_utf8:
532 * @utf8filename: a UTF-8 encoded filename.
534 * Converts a filename from UTF-8 to the system codepage.
536 * On NT-based Windows, on NTFS file systems, file names are in
537 * Unicode. It is quite possible that Unicode file names contain
538 * characters not representable in the system codepage. (For instance,
539 * Greek or Cyrillic characters on Western European or US Windows
540 * installations, or various less common CJK characters on CJK Windows
543 * In such a case, and if the filename refers to an existing file, and
544 * the file system stores alternate short (8.3) names for directory
545 * entries, the short form of the filename is returned. Note that the
546 * "short" name might in fact be longer than the Unicode name if the
547 * Unicode name has very short pathname components containing
548 * non-ASCII characters. If no system codepage name for the file is
549 * possible, %NULL is returned.
551 * The return value is dynamically allocated and should be freed with
552 * g_free() when no longer needed.
554 * Return value: The converted filename, or %NULL on conversion
555 * failure and lack of short names.
560 g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
562 gchar *retval = g_locale_from_utf8 (utf8filename, -1, NULL, NULL, NULL);
566 /* Conversion failed, so convert to wide chars, check if there
567 * is a 8.3 version, and use that.
569 wchar_t *wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL);
572 wchar_t wshortname[MAX_PATH + 1];
573 if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname)))
575 gchar *tem = g_utf16_to_utf8 (wshortname, -1, NULL, NULL, NULL);
576 retval = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);
585 #define __G_WIN32_C__
586 #include "galiasdef.c"