Blame glib/gwin32.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 * Copyright (C) 1998-1999  Tor Lillqvist
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* 
Packit ae235b
 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "glibconfig.h"
Packit ae235b
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <wchar.h>
Packit ae235b
#include <errno.h>
Packit ae235b
Packit ae235b
#define STRICT			/* Strict typing, please */
Packit ae235b
#include <windows.h>
Packit ae235b
#undef STRICT
Packit ae235b
#ifndef G_WITH_CYGWIN
Packit ae235b
#include <direct.h>
Packit ae235b
#endif
Packit ae235b
#include <errno.h>
Packit ae235b
#include <ctype.h>
Packit ae235b
#if defined(_MSC_VER) || defined(__DMC__)
Packit ae235b
#  include <io.h>
Packit ae235b
#endif /* _MSC_VER || __DMC__ */
Packit ae235b
Packit ae235b
#define MODERN_API_FAMILY 2
Packit ae235b
Packit ae235b
#if WINAPI_FAMILY == MODERN_API_FAMILY
Packit ae235b
/* This is for modern UI Builds, where we can't use LoadLibraryW()/GetProcAddress() */
Packit ae235b
/* ntddk.h is found in the WDK, and MinGW */
Packit ae235b
#include <ntddk.h>
Packit ae235b
Packit ae235b
#ifdef _MSC_VER
Packit ae235b
#pragma comment (lib, "ntoskrnl.lib")
Packit ae235b
#endif
Packit ae235b
#elif defined (__MINGW32__)
Packit ae235b
/* mingw-w64, not MinGW, has winternl.h */
Packit ae235b
#include <ntdef.h>
Packit ae235b
#else
Packit ae235b
#include <winternl.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "glib.h"
Packit ae235b
#include "gthreadprivate.h"
Packit ae235b
Packit ae235b
#ifdef G_WITH_CYGWIN
Packit ae235b
#include <sys/cygwin.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef G_WITH_CYGWIN
Packit ae235b
Packit ae235b
gint
Packit ae235b
g_win32_ftruncate (gint  fd,
Packit ae235b
		   guint size)
Packit ae235b
{
Packit ae235b
  return _chsize (fd, size);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_win32_getlocale:
Packit ae235b
 *
Packit ae235b
 * The setlocale() function in the Microsoft C library uses locale
Packit ae235b
 * names of the form "English_United States.1252" etc. We want the
Packit ae235b
 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
Packit ae235b
 * current thread locale from Windows - without any encoding info -
Packit ae235b
 * and returns it as a string of the above form for use in forming
Packit ae235b
 * file names etc. The returned string should be deallocated with
Packit ae235b
 * g_free().
Packit ae235b
 *
Packit ae235b
 * Returns: newly-allocated locale name.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
#ifndef SUBLANG_SERBIAN_LATIN_BA
Packit ae235b
#define SUBLANG_SERBIAN_LATIN_BA 0x06
Packit ae235b
#endif
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_win32_getlocale (void)
Packit ae235b
{
Packit ae235b
  LCID lcid;
Packit ae235b
  LANGID langid;
Packit ae235b
  gchar *ev;
Packit ae235b
  gint primary, sub;
Packit ae235b
  char iso639[10];
Packit ae235b
  char iso3166[10];
Packit ae235b
  const gchar *script = NULL;
Packit ae235b
Packit ae235b
  /* Let the user override the system settings through environment
Packit ae235b
   * variables, as on POSIX systems. Note that in GTK+ applications
Packit ae235b
   * since GTK+ 2.10.7 setting either LC_ALL or LANG also sets the
Packit ae235b
   * Win32 locale and C library locale through code in gtkmain.c.
Packit ae235b
   */
Packit ae235b
  if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
Packit ae235b
      || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
Packit ae235b
      || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
Packit ae235b
    return g_strdup (ev);
Packit ae235b
Packit ae235b
  lcid = GetThreadLocale ();
Packit ae235b
Packit ae235b
  if (!GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) ||
Packit ae235b
      !GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
Packit ae235b
    return g_strdup ("C");
Packit ae235b
  
Packit ae235b
  /* Strip off the sorting rules, keep only the language part.  */
Packit ae235b
  langid = LANGIDFROMLCID (lcid);
Packit ae235b
Packit ae235b
  /* Split into language and territory part.  */
Packit ae235b
  primary = PRIMARYLANGID (langid);
Packit ae235b
  sub = SUBLANGID (langid);
Packit ae235b
Packit ae235b
  /* Handle special cases */
Packit ae235b
  switch (primary)
Packit ae235b
    {
Packit ae235b
    case LANG_AZERI:
Packit ae235b
      switch (sub)
Packit ae235b
	{
Packit ae235b
	case SUBLANG_AZERI_LATIN:
Packit ae235b
	  script = "@Latn";
Packit ae235b
	  break;
Packit ae235b
	case SUBLANG_AZERI_CYRILLIC:
Packit ae235b
	  script = "@Cyrl";
Packit ae235b
	  break;
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
    case LANG_SERBIAN:		/* LANG_CROATIAN == LANG_SERBIAN */
Packit ae235b
      switch (sub)
Packit ae235b
	{
Packit ae235b
	case SUBLANG_SERBIAN_LATIN:
Packit ae235b
	case 0x06: /* Serbian (Latin) - Bosnia and Herzegovina */
Packit ae235b
	  script = "@Latn";
Packit ae235b
	  break;
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
    case LANG_UZBEK:
Packit ae235b
      switch (sub)
Packit ae235b
	{
Packit ae235b
	case SUBLANG_UZBEK_LATIN:
Packit ae235b
	  script = "@Latn";
Packit ae235b
	  break;
Packit ae235b
	case SUBLANG_UZBEK_CYRILLIC:
Packit ae235b
	  script = "@Cyrl";
Packit ae235b
	  break;
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
  return g_strconcat (iso639, "_", iso3166, script, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_win32_error_message:
Packit ae235b
 * @error: error code.
Packit ae235b
 *
Packit ae235b
 * Translate a Win32 error code (as returned by GetLastError() or
Packit ae235b
 * WSAGetLastError()) into the corresponding message. The message is
Packit ae235b
 * either language neutral, or in the thread's language, or the user's
Packit ae235b
 * language, the system's language, or US English (see docs for
Packit ae235b
 * FormatMessage()). The returned string is in UTF-8. It should be
Packit ae235b
 * deallocated with g_free().
Packit ae235b
 *
Packit ae235b
 * Returns: newly-allocated error message
Packit ae235b
 **/
Packit ae235b
gchar *
Packit ae235b
g_win32_error_message (gint error)
Packit ae235b
{
Packit ae235b
  gchar *retval;
Packit ae235b
  wchar_t *msg = NULL;
Packit ae235b
  size_t nchars;
Packit ae235b
Packit ae235b
  FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
Packit ae235b
		  |FORMAT_MESSAGE_IGNORE_INSERTS
Packit ae235b
		  |FORMAT_MESSAGE_FROM_SYSTEM,
Packit ae235b
		  NULL, error, 0,
Packit ae235b
		  (LPWSTR) &msg, 0, NULL);
Packit ae235b
  if (msg != NULL)
Packit ae235b
    {
Packit ae235b
      nchars = wcslen (msg);
Packit ae235b
Packit ae235b
      if (nchars >= 2 && msg[nchars-1] == L'\n' && msg[nchars-2] == L'\r')
Packit ae235b
        msg[nchars-2] = L'\0';
Packit ae235b
Packit ae235b
      retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
      LocalFree (msg);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    retval = g_strdup ("");
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_win32_get_package_installation_directory_of_module:
Packit ae235b
 * @hmodule: (nullable): The Win32 handle for a DLL loaded into the current process, or %NULL
Packit ae235b
 *
Packit ae235b
 * This function tries to determine the installation directory of a
Packit ae235b
 * software package based on the location of a DLL of the software
Packit ae235b
 * package.
Packit ae235b
 *
Packit ae235b
 * @hmodule should be the handle of a loaded DLL or %NULL. The
Packit ae235b
 * function looks up the directory that DLL was loaded from. If
Packit ae235b
 * @hmodule is NULL, the directory the main executable of the current
Packit ae235b
 * process is looked up. If that directory's last component is "bin"
Packit ae235b
 * or "lib", its parent directory is returned, otherwise the directory
Packit ae235b
 * itself.
Packit ae235b
 *
Packit ae235b
 * It thus makes sense to pass only the handle to a "public" DLL of a
Packit ae235b
 * software package to this function, as such DLLs typically are known
Packit ae235b
 * to be installed in a "bin" or occasionally "lib" subfolder of the
Packit ae235b
 * installation folder. DLLs that are of the dynamically loaded module
Packit ae235b
 * or plugin variety are often located in more private locations
Packit ae235b
 * deeper down in the tree, from which it is impossible for GLib to
Packit ae235b
 * deduce the root of the package installation.
Packit ae235b
 *
Packit ae235b
 * The typical use case for this function is to have a DllMain() that
Packit ae235b
 * saves the handle for the DLL. Then when code in the DLL needs to
Packit ae235b
 * construct names of files in the installation tree it calls this
Packit ae235b
 * function passing the DLL handle.
Packit ae235b
 *
Packit ae235b
 * Returns: a string containing the guessed installation directory for
Packit ae235b
 * the software package @hmodule is from. The string is in the GLib
Packit ae235b
 * file name encoding, i.e. UTF-8. The return value should be freed
Packit ae235b
 * with g_free() when not needed any longer. If the function fails
Packit ae235b
 * %NULL is returned.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_win32_get_package_installation_directory_of_module (gpointer hmodule)
Packit ae235b
{
Packit ae235b
  gchar *filename;
Packit ae235b
  gchar *retval;
Packit ae235b
  gchar *p;
Packit ae235b
  wchar_t wc_fn[MAX_PATH];
Packit ae235b
Packit ae235b
  /* NOTE: it relies that GetModuleFileNameW returns only canonical paths */
Packit ae235b
  if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  filename = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  if ((p = strrchr (filename, G_DIR_SEPARATOR)) != NULL)
Packit ae235b
    *p = '\0';
Packit ae235b
Packit ae235b
  retval = g_strdup (filename);
Packit ae235b
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      p = strrchr (retval, G_DIR_SEPARATOR);
Packit ae235b
      if (p == NULL)
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      *p = '\0';
Packit ae235b
Packit ae235b
      if (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
Packit ae235b
          g_ascii_strcasecmp (p + 1, "lib") == 0)
Packit ae235b
        break;
Packit ae235b
    }
Packit ae235b
  while (p != NULL);
Packit ae235b
Packit ae235b
  if (p == NULL)
Packit ae235b
    {
Packit ae235b
      g_free (retval);
Packit ae235b
      retval = filename;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_free (filename);
Packit ae235b
Packit ae235b
#ifdef G_WITH_CYGWIN
Packit ae235b
  /* In Cygwin we need to have POSIX paths */
Packit ae235b
  {
Packit ae235b
    gchar tmp[MAX_PATH];
Packit ae235b
Packit ae235b
    cygwin_conv_to_posix_path (retval, tmp);
Packit ae235b
    g_free (retval);
Packit ae235b
    retval = g_strdup (tmp);
Packit ae235b
  }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
get_package_directory_from_module (const gchar *module_name)
Packit ae235b
{
Packit ae235b
  static GHashTable *module_dirs = NULL;
Packit ae235b
  G_LOCK_DEFINE_STATIC (module_dirs);
Packit ae235b
  HMODULE hmodule = NULL;
Packit ae235b
  gchar *fn;
Packit ae235b
Packit ae235b
  G_LOCK (module_dirs);
Packit ae235b
Packit ae235b
  if (module_dirs == NULL)
Packit ae235b
    module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
Packit ae235b
  
Packit ae235b
  fn = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
Packit ae235b
      
Packit ae235b
  if (fn)
Packit ae235b
    {
Packit ae235b
      G_UNLOCK (module_dirs);
Packit ae235b
      return g_strdup (fn);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (module_name)
Packit ae235b
    {
Packit ae235b
      wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
Packit ae235b
      hmodule = GetModuleHandleW (wc_module_name);
Packit ae235b
      g_free (wc_module_name);
Packit ae235b
Packit ae235b
      if (!hmodule)
Packit ae235b
	{
Packit ae235b
	  G_UNLOCK (module_dirs);
Packit ae235b
	  return NULL;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  fn = g_win32_get_package_installation_directory_of_module (hmodule);
Packit ae235b
Packit ae235b
  if (fn == NULL)
Packit ae235b
    {
Packit ae235b
      G_UNLOCK (module_dirs);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  g_hash_table_insert (module_dirs, module_name ? g_strdup (module_name) : "", fn);
Packit ae235b
Packit ae235b
  G_UNLOCK (module_dirs);
Packit ae235b
Packit ae235b
  return g_strdup (fn);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_win32_get_package_installation_directory:
Packit ae235b
 * @package: (nullable): You should pass %NULL for this.
Packit ae235b
 * @dll_name: (nullable): The name of a DLL that a package provides in UTF-8, or %NULL.
Packit ae235b
 *
Packit ae235b
 * Try to determine the installation directory for a software package.
Packit ae235b
 *
Packit ae235b
 * This function is deprecated. Use
Packit ae235b
 * g_win32_get_package_installation_directory_of_module() instead.
Packit ae235b
 *
Packit ae235b
 * The use of @package is deprecated. You should always pass %NULL. A
Packit ae235b
 * warning is printed if non-NULL is passed as @package.
Packit ae235b
 *
Packit ae235b
 * The original intended use of @package was for a short identifier of
Packit ae235b
 * the package, typically the same identifier as used for
Packit ae235b
 * `GETTEXT_PACKAGE` in software configured using GNU
Packit ae235b
 * autotools. The function first looks in the Windows Registry for the
Packit ae235b
 * value `#InstallationDirectory` in the key
Packit ae235b
 * `#HKLM\Software\@package`, and if that value
Packit ae235b
 * exists and is a string, returns that.
Packit ae235b
 *
Packit ae235b
 * It is strongly recommended that packagers of GLib-using libraries
Packit ae235b
 * for Windows do not store installation paths in the Registry to be
Packit ae235b
 * used by this function as that interfers with having several
Packit ae235b
 * parallel installations of the library. Enabling multiple
Packit ae235b
 * installations of different versions of some GLib-using library, or
Packit ae235b
 * GLib itself, is desirable for various reasons.
Packit ae235b
 *
Packit ae235b
 * For this reason it is recommeded to always pass %NULL as
Packit ae235b
 * @package to this function, to avoid the temptation to use the
Packit ae235b
 * Registry. In version 2.20 of GLib the @package parameter
Packit ae235b
 * will be ignored and this function won't look in the Registry at all.
Packit ae235b
 *
Packit ae235b
 * If @package is %NULL, or the above value isn't found in the
Packit ae235b
 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
Packit ae235b
 * into the current process. Typically that would be the name of the
Packit ae235b
 * DLL calling this function, looking for its installation
Packit ae235b
 * directory. The function then asks Windows what directory that DLL
Packit ae235b
 * was loaded from. If that directory's last component is "bin" or
Packit ae235b
 * "lib", the parent directory is returned, otherwise the directory
Packit ae235b
 * itself. If that DLL isn't loaded, the function proceeds as if
Packit ae235b
 * @dll_name was %NULL.
Packit ae235b
 *
Packit ae235b
 * If both @package and @dll_name are %NULL, the directory from where
Packit ae235b
 * the main executable of the process was loaded is used instead in
Packit ae235b
 * the same way as above.
Packit ae235b
 *
Packit ae235b
 * Returns: a string containing the installation directory for
Packit ae235b
 * @package. The string is in the GLib file name encoding,
Packit ae235b
 * i.e. UTF-8. The return value should be freed with g_free() when not
Packit ae235b
 * needed any longer. If the function fails %NULL is returned.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
Packit ae235b
 * g_win32_get_package_installation_directory_of_module() instead.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_win32_get_package_installation_directory (const gchar *package,
Packit ae235b
                                            const gchar *dll_name)
Packit ae235b
{
Packit ae235b
  gchar *result = NULL;
Packit ae235b
Packit ae235b
  if (package != NULL)
Packit ae235b
      g_warning ("Passing a non-NULL package to g_win32_get_package_installation_directory() is deprecated and it is ignored.");
Packit ae235b
Packit ae235b
  if (dll_name != NULL)
Packit ae235b
    result = get_package_directory_from_module (dll_name);
Packit ae235b
Packit ae235b
  if (result == NULL)
Packit ae235b
    result = get_package_directory_from_module (NULL);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_win32_get_package_installation_subdirectory:
Packit ae235b
 * @package: (nullable): You should pass %NULL for this.
Packit ae235b
 * @dll_name: (nullable): The name of a DLL that a package provides, in UTF-8, or %NULL.
Packit ae235b
 * @subdir: A subdirectory of the package installation directory, also in UTF-8
Packit ae235b
 *
Packit ae235b
 * This function is deprecated. Use
Packit ae235b
 * g_win32_get_package_installation_directory_of_module() and
Packit ae235b
 * g_build_filename() instead.
Packit ae235b
 *
Packit ae235b
 * Returns a newly-allocated string containing the path of the
Packit ae235b
 * subdirectory @subdir in the return value from calling
Packit ae235b
 * g_win32_get_package_installation_directory() with the @package and
Packit ae235b
 * @dll_name parameters. See the documentation for
Packit ae235b
 * g_win32_get_package_installation_directory() for more details. In
Packit ae235b
 * particular, note that it is deprecated to pass anything except NULL
Packit ae235b
 * as @package.
Packit ae235b
 *
Packit ae235b
 * Returns: a string containing the complete path to @subdir inside
Packit ae235b
 * the installation directory of @package. The returned string is in
Packit ae235b
 * the GLib file name encoding, i.e. UTF-8. The return value should be
Packit ae235b
 * freed with g_free() when no longer needed. If something goes wrong,
Packit ae235b
 * %NULL is returned.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
Packit ae235b
 * g_win32_get_package_installation_directory_of_module() instead, and
Packit ae235b
 * then construct a subdirectory pathname with g_build_filename().
Packit ae235b
 **/
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_win32_get_package_installation_subdirectory (const gchar *package,
Packit ae235b
                                               const gchar *dll_name,
Packit ae235b
                                               const gchar *subdir)
Packit ae235b
{
Packit ae235b
  gchar *prefix;
Packit ae235b
  gchar *dirname;
Packit ae235b
Packit ae235b
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Packit ae235b
  prefix = g_win32_get_package_installation_directory (package, dll_name);
Packit ae235b
G_GNUC_END_IGNORE_DEPRECATIONS
Packit ae235b
Packit ae235b
  dirname = g_build_filename (prefix, subdir, NULL);
Packit ae235b
  g_free (prefix);
Packit ae235b
Packit ae235b
  return dirname;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_win32_check_windows_version:
Packit ae235b
 * @major: major version of Windows
Packit ae235b
 * @minor: minor version of Windows
Packit ae235b
 * @spver: Windows Service Pack Level, 0 if none
Packit ae235b
 * @os_type: Type of Windows OS
Packit ae235b
 *
Packit ae235b
 * Returns whether the version of the Windows operating system the
Packit ae235b
 * code is running on is at least the specified major, minor and
Packit ae235b
 * service pack versions.  See MSDN documentation for the Operating
Packit ae235b
 * System Version.  Software that needs even more detailed version and
Packit ae235b
 * feature information should use the Win32 API VerifyVersionInfo()
Packit ae235b
 * directly.
Packit ae235b
 *
Packit ae235b
 * Successive calls of this function can be used for enabling or
Packit ae235b
 * disabling features at run-time for a range of Windows versions,
Packit ae235b
 * as per the VerifyVersionInfo() API documentation.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the Windows Version is the same or greater than
Packit ae235b
 *          the specified major, minor and service pack versions, and
Packit ae235b
 *          whether the running Windows is a workstation or server edition
Packit ae235b
 *          of Windows, if specifically specified.
Packit ae235b
 *
Packit ae235b
 * Since: 2.44
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_win32_check_windows_version (const gint major,
Packit ae235b
                               const gint minor,
Packit ae235b
                               const gint spver,
Packit ae235b
                               const GWin32OSType os_type)
Packit ae235b
{
Packit ae235b
  OSVERSIONINFOEXW osverinfo;
Packit ae235b
  gboolean is_ver_checked = FALSE;
Packit ae235b
  gboolean is_type_checked = FALSE;
Packit ae235b
Packit ae235b
#if WINAPI_FAMILY != MODERN_API_FAMILY
Packit ae235b
  /* For non-modern UI Apps, use the LoadLibraryW()/GetProcAddress() thing */
Packit ae235b
  typedef NTSTATUS (WINAPI fRtlGetVersion) (PRTL_OSVERSIONINFOEXW);
Packit ae235b
Packit ae235b
  fRtlGetVersion *RtlGetVersion;
Packit ae235b
  HMODULE hmodule;
Packit ae235b
#endif
Packit ae235b
  /* We Only Support Checking for XP or later */
Packit ae235b
  g_return_val_if_fail (major >= 5 && (major <=6 || major == 10), FALSE);
Packit ae235b
  g_return_val_if_fail ((major >= 5 && minor >= 1) || major >= 6, FALSE);
Packit ae235b
Packit ae235b
  /* Check for Service Pack Version >= 0 */
Packit ae235b
  g_return_val_if_fail (spver >= 0, FALSE);
Packit ae235b
Packit ae235b
#if WINAPI_FAMILY != MODERN_API_FAMILY
Packit ae235b
  hmodule = LoadLibraryW (L"ntdll.dll");
Packit ae235b
  g_return_val_if_fail (hmodule != NULL, FALSE);
Packit ae235b
Packit ae235b
  RtlGetVersion = (fRtlGetVersion *) GetProcAddress (hmodule, "RtlGetVersion");
Packit ae235b
  g_return_val_if_fail (RtlGetVersion != NULL, FALSE);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  memset (&osverinfo, 0, sizeof (OSVERSIONINFOEXW));
Packit ae235b
  osverinfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEXW);
Packit ae235b
  RtlGetVersion (&osverinfo);
Packit ae235b
Packit ae235b
  /* check the OS and Service Pack Versions */
Packit ae235b
  if (osverinfo.dwMajorVersion > major)
Packit ae235b
    is_ver_checked = TRUE;
Packit ae235b
  else if (osverinfo.dwMajorVersion == major)
Packit ae235b
    {
Packit ae235b
      if (osverinfo.dwMinorVersion > minor)
Packit ae235b
        is_ver_checked = TRUE;
Packit ae235b
      else if (osverinfo.dwMinorVersion == minor)
Packit ae235b
        if (osverinfo.wServicePackMajor >= spver)
Packit ae235b
          is_ver_checked = TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Check OS Type */
Packit ae235b
  if (is_ver_checked)
Packit ae235b
    {
Packit ae235b
      switch (os_type)
Packit ae235b
        {
Packit ae235b
          case G_WIN32_OS_ANY:
Packit ae235b
            is_type_checked = TRUE;
Packit ae235b
            break;
Packit ae235b
          case G_WIN32_OS_WORKSTATION:
Packit ae235b
            if (osverinfo.wProductType == VER_NT_WORKSTATION)
Packit ae235b
              is_type_checked = TRUE;
Packit ae235b
            break;
Packit ae235b
          case G_WIN32_OS_SERVER:
Packit ae235b
            if (osverinfo.wProductType == VER_NT_SERVER ||
Packit ae235b
                osverinfo.wProductType == VER_NT_DOMAIN_CONTROLLER)
Packit ae235b
              is_type_checked = TRUE;
Packit ae235b
            break;
Packit ae235b
          default:
Packit ae235b
            /* shouldn't get here normally */
Packit ae235b
            g_warning ("Invalid os_type specified");
Packit ae235b
            break;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
#if WINAPI_FAMILY != MODERN_API_FAMILY
Packit ae235b
  FreeLibrary (hmodule);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return is_ver_checked && is_type_checked;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_win32_get_windows_version:
Packit ae235b
 *
Packit ae235b
 * This function is deprecated. Use
Packit ae235b
 * g_win32_check_windows_version() instead.
Packit ae235b
 *
Packit ae235b
 * Returns version information for the Windows operating system the
Packit ae235b
 * code is running on. See MSDN documentation for the GetVersion()
Packit ae235b
 * function. To summarize, the most significant bit is one on Win9x,
Packit ae235b
 * and zero on NT-based systems. Since version 2.14, GLib works only
Packit ae235b
 * on NT-based systems, so checking whether your are running on Win9x
Packit ae235b
 * in your own software is moot. The least significant byte is 4 on
Packit ae235b
 * Windows NT 4, and 5 on Windows XP. Software that needs really
Packit ae235b
 * detailed version and feature information should use Win32 API like
Packit ae235b
 * GetVersionEx() and VerifyVersionInfo().
Packit ae235b
 *
Packit ae235b
 * Returns: The version information.
Packit ae235b
 *
Packit ae235b
 * Deprecated: 2.44: Be aware that for Windows 8.1 and Windows Server
Packit ae235b
 * 2012 R2 and later, this will return 62 unless the application is
Packit ae235b
 * manifested for Windows 8.1/Windows Server 2012 R2, for example.
Packit ae235b
 * MSDN stated that GetVersion(), which is used here, is subject to
Packit ae235b
 * further change or removal after Windows 8.1.
Packit ae235b
 **/
Packit ae235b
guint
Packit ae235b
g_win32_get_windows_version (void)
Packit ae235b
{
Packit ae235b
  static gsize windows_version;
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&windows_version))
Packit ae235b
    g_once_init_leave (&windows_version, GetVersion ());
Packit ae235b
Packit ae235b
  return windows_version;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Doesn't use gettext (and gconv), preventing recursive calls when
Packit ae235b
 * g_win32_locale_filename_from_utf8() is called during
Packit ae235b
 * gettext initialization.
Packit ae235b
 */
Packit ae235b
static gchar *
Packit ae235b
special_wchar_to_locale_enoding (wchar_t *wstring)
Packit ae235b
{
Packit ae235b
  int sizeof_output;
Packit ae235b
  int wctmb_result;
Packit ae235b
  char *result;
Packit ae235b
  BOOL not_representable = FALSE;
Packit ae235b
Packit ae235b
  sizeof_output = WideCharToMultiByte (CP_ACP,
Packit ae235b
                                       WC_NO_BEST_FIT_CHARS,
Packit ae235b
                                       wstring, -1,
Packit ae235b
                                       NULL, 0,
Packit ae235b
                                       NULL,
Packit ae235b
                                       &not_representable);
Packit ae235b
Packit ae235b
  if (not_representable ||
Packit ae235b
      sizeof_output == 0 ||
Packit ae235b
      sizeof_output > MAX_PATH)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  result = g_malloc0 (sizeof_output + 1);
Packit ae235b
Packit ae235b
  wctmb_result = WideCharToMultiByte (CP_ACP,
Packit ae235b
                                      WC_NO_BEST_FIT_CHARS,
Packit ae235b
                                      wstring, -1,
Packit ae235b
                                      result, sizeof_output + 1,
Packit ae235b
                                      NULL,
Packit ae235b
                                      &not_representable);
Packit ae235b
Packit ae235b
  if (wctmb_result == sizeof_output &&
Packit ae235b
      not_representable == FALSE)
Packit ae235b
    return result;
Packit ae235b
Packit ae235b
  g_free (result);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_win32_locale_filename_from_utf8:
Packit ae235b
 * @utf8filename: a UTF-8 encoded filename.
Packit ae235b
 *
Packit ae235b
 * Converts a filename from UTF-8 to the system codepage.
Packit ae235b
 *
Packit ae235b
 * On NT-based Windows, on NTFS file systems, file names are in
Packit ae235b
 * Unicode. It is quite possible that Unicode file names contain
Packit ae235b
 * characters not representable in the system codepage. (For instance,
Packit ae235b
 * Greek or Cyrillic characters on Western European or US Windows
Packit ae235b
 * installations, or various less common CJK characters on CJK Windows
Packit ae235b
 * installations.)
Packit ae235b
 *
Packit ae235b
 * In such a case, and if the filename refers to an existing file, and
Packit ae235b
 * the file system stores alternate short (8.3) names for directory
Packit ae235b
 * entries, the short form of the filename is returned. Note that the
Packit ae235b
 * "short" name might in fact be longer than the Unicode name if the
Packit ae235b
 * Unicode name has very short pathname components containing
Packit ae235b
 * non-ASCII characters. If no system codepage name for the file is
Packit ae235b
 * possible, %NULL is returned.
Packit ae235b
 *
Packit ae235b
 * The return value is dynamically allocated and should be freed with
Packit ae235b
 * g_free() when no longer needed.
Packit ae235b
 *
Packit ae235b
 * Returns: The converted filename, or %NULL on conversion
Packit ae235b
 * failure and lack of short names.
Packit ae235b
 *
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
Packit ae235b
{
Packit ae235b
  gchar *retval;
Packit ae235b
  wchar_t *wname;
Packit ae235b
Packit ae235b
  wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  if (wname == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  retval = special_wchar_to_locale_enoding (wname);
Packit ae235b
Packit ae235b
  if (retval == NULL)
Packit ae235b
    {
Packit ae235b
      /* Conversion failed, so check if there is a 8.3 version, and use that. */
Packit ae235b
      wchar_t wshortname[MAX_PATH + 1];
Packit ae235b
Packit ae235b
      if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname)))
Packit ae235b
        retval = special_wchar_to_locale_enoding (wshortname);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (wname);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_win32_get_command_line:
Packit ae235b
 *
Packit ae235b
 * Gets the command line arguments, on Windows, in the GLib filename
Packit ae235b
 * encoding (ie: UTF-8).
Packit ae235b
 *
Packit ae235b
 * Normally, on Windows, the command line arguments are passed to main()
Packit ae235b
 * in the system codepage encoding.  This prevents passing filenames as
Packit ae235b
 * arguments if the filenames contain characters that fall outside of
Packit ae235b
 * this codepage.  If such filenames are passed, then substitutions
Packit ae235b
 * will occur (such as replacing some characters with '?').
Packit ae235b
 *
Packit ae235b
 * GLib's policy of using UTF-8 as a filename encoding on Windows was
Packit ae235b
 * designed to localise the pain of dealing with filenames outside of
Packit ae235b
 * the system codepage to one area: dealing with commandline arguments
Packit ae235b
 * in main().
Packit ae235b
 *
Packit ae235b
 * As such, most GLib programs should ignore the value of argv passed to
Packit ae235b
 * their main() function and call g_win32_get_command_line() instead.
Packit ae235b
 * This will get the "full Unicode" commandline arguments using
Packit ae235b
 * GetCommandLineW() and convert it to the GLib filename encoding (which
Packit ae235b
 * is UTF-8 on Windows).
Packit ae235b
 *
Packit ae235b
 * The strings returned by this function are suitable for use with
Packit ae235b
 * functions such as g_open() and g_file_new_for_commandline_arg() but
Packit ae235b
 * are not suitable for use with g_option_context_parse(), which assumes
Packit ae235b
 * that its input will be in the system codepage.  The return value is
Packit ae235b
 * suitable for use with g_option_context_parse_strv(), however, which
Packit ae235b
 * is a better match anyway because it won't leak memory.
Packit ae235b
 *
Packit ae235b
 * Unlike argv, the returned value is a normal strv and can (and should)
Packit ae235b
 * be freed with g_strfreev() when no longer needed.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): the commandline arguments in the GLib
Packit ae235b
 *   filename encoding (ie: UTF-8)
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 **/
Packit ae235b
gchar **
Packit ae235b
g_win32_get_command_line (void)
Packit ae235b
{
Packit ae235b
  gchar **result;
Packit ae235b
  LPWSTR *args;
Packit ae235b
  gint i, n;
Packit ae235b
Packit ae235b
  args = CommandLineToArgvW (GetCommandLineW(), &n);
Packit ae235b
Packit ae235b
  result = g_new (gchar *, n + 1);
Packit ae235b
  for (i = 0; i < n; i++)
Packit ae235b
    result[i] = g_utf16_to_utf8 (args[i], -1, NULL, NULL, NULL);
Packit ae235b
  result[i] = NULL;
Packit ae235b
Packit ae235b
  LocalFree (args);
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
/* Binary compatibility versions. Not for newly compiled code. */
Packit ae235b
Packit ae235b
_GLIB_EXTERN gchar *g_win32_get_package_installation_directory_utf8    (const gchar *package,
Packit ae235b
                                                                        const gchar *dll_name);
Packit ae235b
Packit ae235b
_GLIB_EXTERN gchar *g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
Packit ae235b
                                                                        const gchar *dll_name,
Packit ae235b
                                                                        const gchar *subdir);
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_win32_get_package_installation_directory_utf8 (const gchar *package,
Packit ae235b
                                                 const gchar *dll_name)
Packit ae235b
{
Packit ae235b
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Packit ae235b
  return g_win32_get_package_installation_directory (package, dll_name);
Packit ae235b
G_GNUC_END_IGNORE_DEPRECATIONS
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
Packit ae235b
                                                    const gchar *dll_name,
Packit ae235b
                                                    const gchar *subdir)
Packit ae235b
{
Packit ae235b
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Packit ae235b
  return g_win32_get_package_installation_subdirectory (package,
Packit ae235b
                                                        dll_name,
Packit ae235b
                                                        subdir);
Packit ae235b
G_GNUC_END_IGNORE_DEPRECATIONS
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif