Blame glib/glib/gwin32.c

Packit db3073
/* GLIB - Library of useful routines for C programming
Packit db3073
 * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit db3073
 * Copyright (C) 1998-1999  Tor Lillqvist
Packit db3073
 *
Packit db3073
 * This library is free software; you can redistribute it and/or
Packit db3073
 * modify it under the terms of the GNU Lesser General Public
Packit db3073
 * License as published by the Free Software Foundation; either
Packit db3073
 * version 2 of the License, or (at your option) any later version.
Packit db3073
 *
Packit db3073
 * This library is distributed in the hope that it will be useful,
Packit db3073
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit db3073
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit db3073
 * Lesser General Public License for more details.
Packit db3073
 *
Packit db3073
 * You should have received a copy of the GNU Lesser General Public
Packit db3073
 * License along with this library; if not, write to the
Packit db3073
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit db3073
 * Boston, MA 02111-1307, USA.
Packit db3073
 */
Packit db3073
Packit db3073
/*
Packit db3073
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit db3073
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit db3073
 * files for a list of changes.  These files are distributed with
Packit db3073
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
Packit db3073
 */
Packit db3073
Packit db3073
/* 
Packit db3073
 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
Packit db3073
 */
Packit db3073
Packit db3073
#include "config.h"
Packit db3073
Packit db3073
#include "glibconfig.h"
Packit db3073
Packit db3073
#include <stdlib.h>
Packit db3073
#include <stdio.h>
Packit db3073
#include <string.h>
Packit db3073
#include <wchar.h>
Packit db3073
#include <errno.h>
Packit db3073
Packit db3073
#define STRICT			/* Strict typing, please */
Packit db3073
#include <windows.h>
Packit db3073
#undef STRICT
Packit db3073
#ifndef G_WITH_CYGWIN
Packit db3073
#include <direct.h>
Packit db3073
#endif
Packit db3073
#include <errno.h>
Packit db3073
#include <ctype.h>
Packit db3073
#if defined(_MSC_VER) || defined(__DMC__)
Packit db3073
#  include <io.h>
Packit db3073
#endif /* _MSC_VER || __DMC__ */
Packit db3073
Packit db3073
#include "glib.h"
Packit db3073
#include "gthreadprivate.h"
Packit db3073
Packit db3073
#ifdef G_WITH_CYGWIN
Packit db3073
#include <sys/cygwin.h>
Packit db3073
#endif
Packit db3073
Packit db3073
#ifndef G_WITH_CYGWIN
Packit db3073
Packit db3073
gint
Packit db3073
g_win32_ftruncate (gint  fd,
Packit db3073
		   guint size)
Packit db3073
{
Packit db3073
  return _chsize (fd, size);
Packit db3073
}
Packit db3073
Packit db3073
#endif
Packit db3073
Packit db3073
/**
Packit db3073
 * g_win32_getlocale:
Packit db3073
 *
Packit db3073
 * The setlocale() function in the Microsoft C library uses locale
Packit db3073
 * names of the form "English_United States.1252" etc. We want the
Packit db3073
 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
Packit db3073
 * current thread locale from Windows - without any encoding info -
Packit db3073
 * and returns it as a string of the above form for use in forming
Packit db3073
 * file names etc. The returned string should be deallocated with
Packit db3073
 * g_free().
Packit db3073
 *
Packit db3073
 * Returns: newly-allocated locale name.
Packit db3073
 **/
Packit db3073
Packit db3073
#ifndef SUBLANG_SERBIAN_LATIN_BA
Packit db3073
#define SUBLANG_SERBIAN_LATIN_BA 0x06
Packit db3073
#endif
Packit db3073
Packit db3073
gchar *
Packit db3073
g_win32_getlocale (void)
Packit db3073
{
Packit db3073
  LCID lcid;
Packit db3073
  LANGID langid;
Packit db3073
  gchar *ev;
Packit db3073
  gint primary, sub;
Packit db3073
  char iso639[10];
Packit db3073
  char iso3166[10];
Packit db3073
  const gchar *script = NULL;
Packit db3073
Packit db3073
  /* Let the user override the system settings through environment
Packit db3073
   * variables, as on POSIX systems. Note that in GTK+ applications
Packit db3073
   * since GTK+ 2.10.7 setting either LC_ALL or LANG also sets the
Packit db3073
   * Win32 locale and C library locale through code in gtkmain.c.
Packit db3073
   */
Packit db3073
  if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
Packit db3073
      || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
Packit db3073
      || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
Packit db3073
    return g_strdup (ev);
Packit db3073
Packit db3073
  lcid = GetThreadLocale ();
Packit db3073
Packit db3073
  if (!GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) ||
Packit db3073
      !GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
Packit db3073
    return g_strdup ("C");
Packit db3073
  
Packit db3073
  /* Strip off the sorting rules, keep only the language part.  */
Packit db3073
  langid = LANGIDFROMLCID (lcid);
Packit db3073
Packit db3073
  /* Split into language and territory part.  */
Packit db3073
  primary = PRIMARYLANGID (langid);
Packit db3073
  sub = SUBLANGID (langid);
Packit db3073
Packit db3073
  /* Handle special cases */
Packit db3073
  switch (primary)
Packit db3073
    {
Packit db3073
    case LANG_AZERI:
Packit db3073
      switch (sub)
Packit db3073
	{
Packit db3073
	case SUBLANG_AZERI_LATIN:
Packit db3073
	  script = "@Latn";
Packit db3073
	  break;
Packit db3073
	case SUBLANG_AZERI_CYRILLIC:
Packit db3073
	  script = "@Cyrl";
Packit db3073
	  break;
Packit db3073
	}
Packit db3073
      break;
Packit db3073
    case LANG_SERBIAN:		/* LANG_CROATIAN == LANG_SERBIAN */
Packit db3073
      switch (sub)
Packit db3073
	{
Packit db3073
	case SUBLANG_SERBIAN_LATIN:
Packit db3073
	case 0x06: /* Serbian (Latin) - Bosnia and Herzegovina */
Packit db3073
	  script = "@Latn";
Packit db3073
	  break;
Packit db3073
	}
Packit db3073
      break;
Packit db3073
    case LANG_UZBEK:
Packit db3073
      switch (sub)
Packit db3073
	{
Packit db3073
	case SUBLANG_UZBEK_LATIN:
Packit db3073
	  script = "@Latn";
Packit db3073
	  break;
Packit db3073
	case SUBLANG_UZBEK_CYRILLIC:
Packit db3073
	  script = "@Cyrl";
Packit db3073
	  break;
Packit db3073
	}
Packit db3073
      break;
Packit db3073
    }
Packit db3073
  return g_strconcat (iso639, "_", iso3166, script, NULL);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_win32_error_message:
Packit db3073
 * @error: error code.
Packit db3073
 *
Packit db3073
 * Translate a Win32 error code (as returned by GetLastError()) into
Packit db3073
 * the corresponding message. The message is either language neutral,
Packit db3073
 * or in the thread's language, or the user's language, the system's
Packit db3073
 * language, or US English (see docs for FormatMessage()). The
Packit db3073
 * returned string is in UTF-8. It should be deallocated with
Packit db3073
 * g_free().
Packit db3073
 *
Packit db3073
 * Returns: newly-allocated error message
Packit db3073
 **/
Packit db3073
gchar *
Packit db3073
g_win32_error_message (gint error)
Packit db3073
{
Packit db3073
  gchar *retval;
Packit db3073
  wchar_t *msg = NULL;
Packit db3073
  int nchars;
Packit db3073
Packit db3073
  FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
Packit db3073
		  |FORMAT_MESSAGE_IGNORE_INSERTS
Packit db3073
		  |FORMAT_MESSAGE_FROM_SYSTEM,
Packit db3073
		  NULL, error, 0,
Packit db3073
		  (LPWSTR) &msg, 0, NULL);
Packit db3073
  if (msg != NULL)
Packit db3073
    {
Packit db3073
      nchars = wcslen (msg);
Packit db3073
      
Packit db3073
      if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
Packit db3073
	msg[nchars-2] = '\0';
Packit db3073
      
Packit db3073
      retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
Packit db3073
      
Packit db3073
      LocalFree (msg);
Packit db3073
    }
Packit db3073
  else
Packit db3073
    retval = g_strdup ("");
Packit db3073
Packit db3073
  return retval;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_win32_get_package_installation_directory_of_module:
Packit db3073
 * @hmodule: (allow-none): The Win32 handle for a DLL loaded into the current process, or %NULL
Packit db3073
 *
Packit db3073
 * This function tries to determine the installation directory of a
Packit db3073
 * software package based on the location of a DLL of the software
Packit db3073
 * package.
Packit db3073
 *
Packit db3073
 * @hmodule should be the handle of a loaded DLL or %NULL. The
Packit db3073
 * function looks up the directory that DLL was loaded from. If
Packit db3073
 * @hmodule is NULL, the directory the main executable of the current
Packit db3073
 * process is looked up. If that directory's last component is "bin"
Packit db3073
 * or "lib", its parent directory is returned, otherwise the directory
Packit db3073
 * itself.
Packit db3073
 *
Packit db3073
 * It thus makes sense to pass only the handle to a "public" DLL of a
Packit db3073
 * software package to this function, as such DLLs typically are known
Packit db3073
 * to be installed in a "bin" or occasionally "lib" subfolder of the
Packit db3073
 * installation folder. DLLs that are of the dynamically loaded module
Packit db3073
 * or plugin variety are often located in more private locations
Packit db3073
 * deeper down in the tree, from which it is impossible for GLib to
Packit db3073
 * deduce the root of the package installation.
Packit db3073
 *
Packit db3073
 * The typical use case for this function is to have a DllMain() that
Packit db3073
 * saves the handle for the DLL. Then when code in the DLL needs to
Packit db3073
 * construct names of files in the installation tree it calls this
Packit db3073
 * function passing the DLL handle.
Packit db3073
 *
Packit db3073
 * Returns: a string containing the guessed installation directory for
Packit db3073
 * the software package @hmodule is from. The string is in the GLib
Packit db3073
 * file name encoding, i.e. UTF-8. The return value should be freed
Packit db3073
 * with g_free() when not needed any longer. If the function fails
Packit db3073
 * %NULL is returned.
Packit db3073
 *
Packit db3073
 * Since: 2.16
Packit db3073
 */
Packit db3073
gchar *
Packit db3073
g_win32_get_package_installation_directory_of_module (gpointer hmodule)
Packit db3073
{
Packit db3073
  gchar *retval;
Packit db3073
  gchar *p;
Packit db3073
  wchar_t wc_fn[MAX_PATH];
Packit db3073
Packit db3073
  if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
Packit db3073
    return NULL;
Packit db3073
Packit db3073
  retval = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
Packit db3073
Packit db3073
  if ((p = strrchr (retval, G_DIR_SEPARATOR)) != NULL)
Packit db3073
    *p = '\0';
Packit db3073
Packit db3073
  p = strrchr (retval, G_DIR_SEPARATOR);
Packit db3073
  if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
Packit db3073
	    g_ascii_strcasecmp (p + 1, "lib") == 0))
Packit db3073
    *p = '\0';
Packit db3073
Packit db3073
#ifdef G_WITH_CYGWIN
Packit db3073
  /* In Cygwin we need to have POSIX paths */
Packit db3073
  {
Packit db3073
    gchar tmp[MAX_PATH];
Packit db3073
Packit db3073
    cygwin_conv_to_posix_path (retval, tmp);
Packit db3073
    g_free (retval);
Packit db3073
    retval = g_strdup (tmp);
Packit db3073
  }
Packit db3073
#endif
Packit db3073
Packit db3073
  return retval;
Packit db3073
}
Packit db3073
Packit db3073
static gchar *
Packit db3073
get_package_directory_from_module (const gchar *module_name)
Packit db3073
{
Packit db3073
  static GHashTable *module_dirs = NULL;
Packit db3073
  G_LOCK_DEFINE_STATIC (module_dirs);
Packit db3073
  HMODULE hmodule = NULL;
Packit db3073
  gchar *fn;
Packit db3073
Packit db3073
  G_LOCK (module_dirs);
Packit db3073
Packit db3073
  if (module_dirs == NULL)
Packit db3073
    module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
Packit db3073
  
Packit db3073
  fn = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
Packit db3073
      
Packit db3073
  if (fn)
Packit db3073
    {
Packit db3073
      G_UNLOCK (module_dirs);
Packit db3073
      return g_strdup (fn);
Packit db3073
    }
Packit db3073
Packit db3073
  if (module_name)
Packit db3073
    {
Packit db3073
      wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
Packit db3073
      hmodule = GetModuleHandleW (wc_module_name);
Packit db3073
      g_free (wc_module_name);
Packit db3073
Packit db3073
      if (!hmodule)
Packit db3073
	{
Packit db3073
	  G_UNLOCK (module_dirs);
Packit db3073
	  return NULL;
Packit db3073
	}
Packit db3073
    }
Packit db3073
Packit db3073
  fn = g_win32_get_package_installation_directory_of_module (hmodule);
Packit db3073
Packit db3073
  if (fn == NULL)
Packit db3073
    {
Packit db3073
      G_UNLOCK (module_dirs);
Packit db3073
      return NULL;
Packit db3073
    }
Packit db3073
  
Packit db3073
  g_hash_table_insert (module_dirs, module_name ? g_strdup (module_name) : "", fn);
Packit db3073
Packit db3073
  G_UNLOCK (module_dirs);
Packit db3073
Packit db3073
  return g_strdup (fn);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_win32_get_package_installation_directory:
Packit db3073
 * @package: (allow-none): You should pass %NULL for this.
Packit db3073
 * @dll_name: (allow-none): The name of a DLL that a package provides in UTF-8, or %NULL.
Packit db3073
 *
Packit db3073
 * Try to determine the installation directory for a software package.
Packit db3073
 *
Packit db3073
 * This function is deprecated. Use
Packit db3073
 * g_win32_get_package_installation_directory_of_module() instead.
Packit db3073
 *
Packit db3073
 * The use of @package is deprecated. You should always pass %NULL. A
Packit db3073
 * warning is printed if non-NULL is passed as @package.
Packit db3073
 *
Packit db3073
 * The original intended use of @package was for a short identifier of
Packit db3073
 * the package, typically the same identifier as used for
Packit db3073
 * <literal>GETTEXT_PACKAGE</literal> in software configured using GNU
Packit db3073
 * autotools. The function first looks in the Windows Registry for the
Packit db3073
 * value <literal>#InstallationDirectory</literal> in the key
Packit db3073
 * <literal>#HKLM\Software\@package</literal>, and if that value
Packit db3073
 * exists and is a string, returns that.
Packit db3073
 *
Packit db3073
 * It is strongly recommended that packagers of GLib-using libraries
Packit db3073
 * for Windows do not store installation paths in the Registry to be
Packit db3073
 * used by this function as that interfers with having several
Packit db3073
 * parallel installations of the library. Enabling multiple
Packit db3073
 * installations of different versions of some GLib-using library, or
Packit db3073
 * GLib itself, is desirable for various reasons.
Packit db3073
 *
Packit db3073
 * For this reason it is recommeded to always pass %NULL as
Packit db3073
 * @package to this function, to avoid the temptation to use the
Packit db3073
 * Registry. In version 2.20 of GLib the @package parameter
Packit db3073
 * will be ignored and this function won't look in the Registry at all.
Packit db3073
 *
Packit db3073
 * If @package is %NULL, or the above value isn't found in the
Packit db3073
 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
Packit db3073
 * into the current process. Typically that would be the name of the
Packit db3073
 * DLL calling this function, looking for its installation
Packit db3073
 * directory. The function then asks Windows what directory that DLL
Packit db3073
 * was loaded from. If that directory's last component is "bin" or
Packit db3073
 * "lib", the parent directory is returned, otherwise the directory
Packit db3073
 * itself. If that DLL isn't loaded, the function proceeds as if
Packit db3073
 * @dll_name was %NULL.
Packit db3073
 *
Packit db3073
 * If both @package and @dll_name are %NULL, the directory from where
Packit db3073
 * the main executable of the process was loaded is used instead in
Packit db3073
 * the same way as above.
Packit db3073
 *
Packit db3073
 * Returns: a string containing the installation directory for
Packit db3073
 * @package. The string is in the GLib file name encoding,
Packit db3073
 * i.e. UTF-8. The return value should be freed with g_free() when not
Packit db3073
 * needed any longer. If the function fails %NULL is returned.
Packit db3073
 *
Packit db3073
 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
Packit db3073
 * g_win32_get_package_installation_directory_of_module() instead.
Packit db3073
 **/
Packit db3073
Packit db3073
 gchar *
Packit db3073
g_win32_get_package_installation_directory_utf8 (const gchar *package,
Packit db3073
						 const gchar *dll_name)
Packit db3073
{
Packit db3073
  gchar *result = NULL;
Packit db3073
Packit db3073
  if (package != NULL)
Packit db3073
      g_warning ("Passing a non-NULL package to g_win32_get_package_installation_directory() is deprecated and it is ignored.");
Packit db3073
Packit db3073
  if (dll_name != NULL)
Packit db3073
    result = get_package_directory_from_module (dll_name);
Packit db3073
Packit db3073
  if (result == NULL)
Packit db3073
    result = get_package_directory_from_module (NULL);
Packit db3073
Packit db3073
  return result;
Packit db3073
}
Packit db3073
Packit db3073
#if !defined (_WIN64)
Packit db3073
Packit db3073
/* DLL ABI binary compatibility version that uses system codepage file names */
Packit db3073
Packit db3073
gchar *
Packit db3073
g_win32_get_package_installation_directory (const gchar *package,
Packit db3073
					    const gchar *dll_name)
Packit db3073
{
Packit db3073
  gchar *utf8_package = NULL, *utf8_dll_name = NULL;
Packit db3073
  gchar *utf8_retval, *retval;
Packit db3073
Packit db3073
  if (package != NULL)
Packit db3073
    utf8_package = g_locale_to_utf8 (package, -1, NULL, NULL, NULL);
Packit db3073
Packit db3073
  if (dll_name != NULL)
Packit db3073
    utf8_dll_name = g_locale_to_utf8 (dll_name, -1, NULL, NULL, NULL);
Packit db3073
Packit db3073
  utf8_retval =
Packit db3073
    g_win32_get_package_installation_directory_utf8 (utf8_package,
Packit db3073
						     utf8_dll_name);
Packit db3073
Packit db3073
  retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
Packit db3073
Packit db3073
  g_free (utf8_package);
Packit db3073
  g_free (utf8_dll_name);
Packit db3073
  g_free (utf8_retval);
Packit db3073
Packit db3073
  return retval;
Packit db3073
}
Packit db3073
Packit db3073
#endif
Packit db3073
Packit db3073
/**
Packit db3073
 * g_win32_get_package_installation_subdirectory:
Packit db3073
 * @package: (allow-none): You should pass %NULL for this.
Packit db3073
 * @dll_name: (allow-none): The name of a DLL that a package provides, in UTF-8, or %NULL.
Packit db3073
 * @subdir: A subdirectory of the package installation directory, also in UTF-8
Packit db3073
 *
Packit db3073
 * This function is deprecated. Use
Packit db3073
 * g_win32_get_package_installation_directory_of_module() and
Packit db3073
 * g_build_filename() instead.
Packit db3073
 *
Packit db3073
 * Returns a newly-allocated string containing the path of the
Packit db3073
 * subdirectory @subdir in the return value from calling
Packit db3073
 * g_win32_get_package_installation_directory() with the @package and
Packit db3073
 * @dll_name parameters. See the documentation for
Packit db3073
 * g_win32_get_package_installation_directory() for more details. In
Packit db3073
 * particular, note that it is deprecated to pass anything except NULL
Packit db3073
 * as @package.
Packit db3073
 *
Packit db3073
 * Returns: a string containing the complete path to @subdir inside
Packit db3073
 * the installation directory of @package. The returned string is in
Packit db3073
 * the GLib file name encoding, i.e. UTF-8. The return value should be
Packit db3073
 * freed with g_free() when no longer needed. If something goes wrong,
Packit db3073
 * %NULL is returned.
Packit db3073
 *
Packit db3073
 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
Packit db3073
 * g_win32_get_package_installation_directory_of_module() instead, and
Packit db3073
 * then construct a subdirectory pathname with g_build_filename().
Packit db3073
 **/
Packit db3073
Packit db3073
gchar *
Packit db3073
g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
Packit db3073
						    const gchar *dll_name,
Packit db3073
						    const gchar *subdir)
Packit db3073
{
Packit db3073
  gchar *prefix;
Packit db3073
  gchar *dirname;
Packit db3073
Packit db3073
  prefix = g_win32_get_package_installation_directory_utf8 (package, dll_name);
Packit db3073
Packit db3073
  dirname = g_build_filename (prefix, subdir, NULL);
Packit db3073
  g_free (prefix);
Packit db3073
Packit db3073
  return dirname;
Packit db3073
}
Packit db3073
Packit db3073
#if !defined (_WIN64)
Packit db3073
Packit db3073
/* DLL ABI binary compatibility version that uses system codepage file names */
Packit db3073
Packit db3073
gchar *
Packit db3073
g_win32_get_package_installation_subdirectory (const gchar *package,
Packit db3073
					       const gchar *dll_name,
Packit db3073
					       const gchar *subdir)
Packit db3073
{
Packit db3073
  gchar *prefix;
Packit db3073
  gchar *dirname;
Packit db3073
Packit db3073
  prefix = g_win32_get_package_installation_directory (package, dll_name);
Packit db3073
Packit db3073
  dirname = g_build_filename (prefix, subdir, NULL);
Packit db3073
  g_free (prefix);
Packit db3073
Packit db3073
  return dirname;
Packit db3073
}
Packit db3073
Packit db3073
#endif
Packit db3073
Packit db3073
/**
Packit db3073
 * g_win32_get_windows_version:
Packit db3073
 *
Packit db3073
 * Returns version information for the Windows operating system the
Packit db3073
 * code is running on. See MSDN documentation for the GetVersion()
Packit db3073
 * function. To summarize, the most significant bit is one on Win9x,
Packit db3073
 * and zero on NT-based systems. Since version 2.14, GLib works only
Packit db3073
 * on NT-based systems, so checking whether your are running on Win9x
Packit db3073
 * in your own software is moot. The least significant byte is 4 on
Packit db3073
 * Windows NT 4, and 5 on Windows XP. Software that needs really
Packit db3073
 * detailed version and feature information should use Win32 API like
Packit db3073
 * GetVersionEx() and VerifyVersionInfo().
Packit db3073
 *
Packit db3073
 * Returns: The version information.
Packit db3073
 * 
Packit db3073
 * Since: 2.6
Packit db3073
 **/
Packit db3073
guint
Packit db3073
g_win32_get_windows_version (void)
Packit db3073
{
Packit db3073
  static gsize windows_version;
Packit db3073
Packit db3073
  if (g_once_init_enter (&windows_version))
Packit db3073
    g_once_init_leave (&windows_version, GetVersion ());
Packit db3073
Packit db3073
  return windows_version;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_win32_locale_filename_from_utf8:
Packit db3073
 * @utf8filename: a UTF-8 encoded filename.
Packit db3073
 *
Packit db3073
 * Converts a filename from UTF-8 to the system codepage.
Packit db3073
 *
Packit db3073
 * On NT-based Windows, on NTFS file systems, file names are in
Packit db3073
 * Unicode. It is quite possible that Unicode file names contain
Packit db3073
 * characters not representable in the system codepage. (For instance,
Packit db3073
 * Greek or Cyrillic characters on Western European or US Windows
Packit db3073
 * installations, or various less common CJK characters on CJK Windows
Packit db3073
 * installations.)
Packit db3073
 *
Packit db3073
 * In such a case, and if the filename refers to an existing file, and
Packit db3073
 * the file system stores alternate short (8.3) names for directory
Packit db3073
 * entries, the short form of the filename is returned. Note that the
Packit db3073
 * "short" name might in fact be longer than the Unicode name if the
Packit db3073
 * Unicode name has very short pathname components containing
Packit db3073
 * non-ASCII characters. If no system codepage name for the file is
Packit db3073
 * possible, %NULL is returned.
Packit db3073
 *
Packit db3073
 * The return value is dynamically allocated and should be freed with
Packit db3073
 * g_free() when no longer needed.
Packit db3073
 *
Packit db3073
 * Return value: The converted filename, or %NULL on conversion
Packit db3073
 * failure and lack of short names.
Packit db3073
 *
Packit db3073
 * Since: 2.8
Packit db3073
 */
Packit db3073
gchar *
Packit db3073
g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
Packit db3073
{
Packit db3073
  gchar *retval = g_locale_from_utf8 (utf8filename, -1, NULL, NULL, NULL);
Packit db3073
Packit db3073
  if (retval == NULL)
Packit db3073
    {
Packit db3073
      /* Conversion failed, so convert to wide chars, check if there
Packit db3073
       * is a 8.3 version, and use that.
Packit db3073
       */
Packit db3073
      wchar_t *wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL);
Packit db3073
      if (wname != NULL)
Packit db3073
	{
Packit db3073
	  wchar_t wshortname[MAX_PATH + 1];
Packit db3073
	  if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname)))
Packit db3073
	    {
Packit db3073
	      gchar *tem = g_utf16_to_utf8 (wshortname, -1, NULL, NULL, NULL);
Packit db3073
	      retval = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);
Packit db3073
	      g_free (tem);
Packit db3073
	    }
Packit db3073
	  g_free (wname);
Packit db3073
	}
Packit db3073
    }
Packit db3073
  return retval;
Packit db3073
}