* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/*
#endif
#include <errno.h>
#include <ctype.h>
-#ifdef _MSC_VER
+#if defined(_MSC_VER) || defined(__DMC__)
# include <io.h>
-#endif /* _MSC_VER */
+#endif /* _MSC_VER || __DMC__ */
#include "glib.h"
-#include "galias.h"
+#include "gthreadprivate.h"
#ifdef G_WITH_CYGWIN
#include <sys/cygwin.h>
* g_win32_error_message:
* @error: error code.
*
- * Translate a Win32 error code (as returned by GetLastError()) into
- * the corresponding message. The message is either language neutral,
- * or in the thread's language, or the user's language, the system's
- * language, or US English (see docs for FormatMessage()). The
- * returned string is in UTF-8. It should be deallocated with
- * g_free().
+ * Translate a Win32 error code (as returned by GetLastError() or
+ * WSAGetLastError()) into the corresponding message. The message is
+ * either language neutral, or in the thread's language, or the user's
+ * language, the system's language, or US English (see docs for
+ * FormatMessage()). The returned string is in UTF-8. It should be
+ * deallocated with g_free().
*
* Returns: newly-allocated error message
**/
return retval;
}
+/**
+ * g_win32_get_package_installation_directory_of_module:
+ * @hmodule: (allow-none): The Win32 handle for a DLL loaded into the current process, or %NULL
+ *
+ * This function tries to determine the installation directory of a
+ * software package based on the location of a DLL of the software
+ * package.
+ *
+ * @hmodule should be the handle of a loaded DLL or %NULL. The
+ * function looks up the directory that DLL was loaded from. If
+ * @hmodule is NULL, the directory the main executable of the current
+ * process is looked up. If that directory's last component is "bin"
+ * or "lib", its parent directory is returned, otherwise the directory
+ * itself.
+ *
+ * It thus makes sense to pass only the handle to a "public" DLL of a
+ * software package to this function, as such DLLs typically are known
+ * to be installed in a "bin" or occasionally "lib" subfolder of the
+ * installation folder. DLLs that are of the dynamically loaded module
+ * or plugin variety are often located in more private locations
+ * deeper down in the tree, from which it is impossible for GLib to
+ * deduce the root of the package installation.
+ *
+ * The typical use case for this function is to have a DllMain() that
+ * saves the handle for the DLL. Then when code in the DLL needs to
+ * construct names of files in the installation tree it calls this
+ * function passing the DLL handle.
+ *
+ * Returns: a string containing the guessed installation directory for
+ * the software package @hmodule is from. The string is in the GLib
+ * file name encoding, i.e. UTF-8. The return value should be freed
+ * with g_free() when not needed any longer. If the function fails
+ * %NULL is returned.
+ *
+ * Since: 2.16
+ */
+gchar *
+g_win32_get_package_installation_directory_of_module (gpointer hmodule)
+{
+ gchar *retval;
+ gchar *p;
+ wchar_t wc_fn[MAX_PATH];
+
+ if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
+ return NULL;
+
+ retval = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
+
+ if ((p = strrchr (retval, G_DIR_SEPARATOR)) != NULL)
+ *p = '\0';
+
+ p = strrchr (retval, G_DIR_SEPARATOR);
+ if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
+ g_ascii_strcasecmp (p + 1, "lib") == 0))
+ *p = '\0';
+
+#ifdef G_WITH_CYGWIN
+ /* In Cygwin we need to have POSIX paths */
+ {
+ gchar tmp[MAX_PATH];
+
+ cygwin_conv_to_posix_path (retval, tmp);
+ g_free (retval);
+ retval = g_strdup (tmp);
+ }
+#endif
+
+ return retval;
+}
+
static gchar *
get_package_directory_from_module (const gchar *module_name)
{
G_LOCK_DEFINE_STATIC (module_dirs);
HMODULE hmodule = NULL;
gchar *fn;
- gchar *p;
- gchar *result;
- wchar_t wc_fn[MAX_PATH];
G_LOCK (module_dirs);
if (module_dirs == NULL)
module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
- result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
+ fn = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
- if (result)
+ if (fn)
{
G_UNLOCK (module_dirs);
- return g_strdup (result);
+ return g_strdup (fn);
}
if (module_name)
g_free (wc_module_name);
if (!hmodule)
- return NULL;
+ {
+ G_UNLOCK (module_dirs);
+ return NULL;
+ }
}
- if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
+ fn = g_win32_get_package_installation_directory_of_module (hmodule);
+
+ if (fn == NULL)
{
G_UNLOCK (module_dirs);
return NULL;
}
- fn = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
-
- if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
- *p = '\0';
-
- p = strrchr (fn, G_DIR_SEPARATOR);
- if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
- g_ascii_strcasecmp (p + 1, "lib") == 0))
- *p = '\0';
-
-#ifdef G_WITH_CYGWIN
- /* In Cygwin we need to have POSIX paths */
- {
- gchar tmp[MAX_PATH];
-
- cygwin_conv_to_posix_path(fn, tmp);
- g_free(fn);
- fn = g_strdup(tmp);
- }
-#endif
-
+
g_hash_table_insert (module_dirs, module_name ? g_strdup (module_name) : "", fn);
G_UNLOCK (module_dirs);
/**
* g_win32_get_package_installation_directory:
- * @package: An identifier for a software package, or %NULL, in UTF-8
- * @dll_name: The name of a DLL that a package provides, or %NULL, in UTF-8
+ * @package: (allow-none): You should pass %NULL for this.
+ * @dll_name: (allow-none): The name of a DLL that a package provides in UTF-8, or %NULL.
*
* Try to determine the installation directory for a software package.
- * Typically used by GNU software packages.
- *
- * @package should be a short identifier for the package. Typically it
- * is the same identifier as used for
- * <literal>GETTEXT_PACKAGE</literal> in software configured according
- * to GNU standards. The function first looks in the Windows Registry
- * for the value <literal>#InstallationDirectory</literal> in the key
- * <literal>#HKLM\Software\@package</literal>, and if that value
+ *
+ * This function is deprecated. Use
+ * g_win32_get_package_installation_directory_of_module() instead.
+ *
+ * The use of @package is deprecated. You should always pass %NULL. A
+ * warning is printed if non-NULL is passed as @package.
+ *
+ * The original intended use of @package was for a short identifier of
+ * the package, typically the same identifier as used for
+ * `GETTEXT_PACKAGE` in software configured using GNU
+ * autotools. The function first looks in the Windows Registry for the
+ * value `#InstallationDirectory` in the key
+ * `#HKLM\Software\@package`, and if that value
* exists and is a string, returns that.
*
+ * It is strongly recommended that packagers of GLib-using libraries
+ * for Windows do not store installation paths in the Registry to be
+ * used by this function as that interfers with having several
+ * parallel installations of the library. Enabling multiple
+ * installations of different versions of some GLib-using library, or
+ * GLib itself, is desirable for various reasons.
+ *
+ * For this reason it is recommeded to always pass %NULL as
+ * @package to this function, to avoid the temptation to use the
+ * Registry. In version 2.20 of GLib the @package parameter
+ * will be ignored and this function won't look in the Registry at all.
+ *
* If @package is %NULL, or the above value isn't found in the
* Registry, but @dll_name is non-%NULL, it should name a DLL loaded
* into the current process. Typically that would be the name of the
* the same way as above.
*
* Returns: a string containing the installation directory for
- * @package. The string is in the GLib file name encoding, i.e. UTF-8
- * on Windows. The return value should be freed with g_free() when not
- * needed any longer.
+ * @package. The string is in the GLib file name encoding,
+ * i.e. UTF-8. The return value should be freed with g_free() when not
+ * needed any longer. If the function fails %NULL is returned.
+ *
+ * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
+ * g_win32_get_package_installation_directory_of_module() instead.
**/
-gchar *
-g_win32_get_package_installation_directory (const gchar *package,
- const gchar *dll_name)
+ gchar *
+g_win32_get_package_installation_directory_utf8 (const gchar *package,
+ const gchar *dll_name)
{
- static GHashTable *package_dirs = NULL;
- G_LOCK_DEFINE_STATIC (package_dirs);
gchar *result = NULL;
- gchar *key;
- wchar_t *wc_key;
- HKEY reg_key = NULL;
- DWORD type;
- DWORD nbytes;
if (package != NULL)
- {
- G_LOCK (package_dirs);
-
- if (package_dirs == NULL)
- package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
-
- result = g_hash_table_lookup (package_dirs, package);
-
- if (result && result[0])
- {
- G_UNLOCK (package_dirs);
- return g_strdup (result);
- }
-
- key = g_strconcat ("Software\\", package, NULL);
-
- nbytes = 0;
-
- wc_key = g_utf8_to_utf16 (key, -1, NULL, NULL, NULL);
- if (((RegOpenKeyExW (HKEY_CURRENT_USER, wc_key, 0,
- KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
- && RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
- &type, NULL, &nbytes) == ERROR_SUCCESS)
- ||
- (RegOpenKeyExW (HKEY_LOCAL_MACHINE, wc_key, 0,
- KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
- && RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
- &type, NULL, &nbytes) == ERROR_SUCCESS))
- && type == REG_SZ)
- {
- wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
- RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
- &type, (LPBYTE) wc_temp, &nbytes);
- wc_temp[nbytes/2] = '\0';
- result = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
- g_free (wc_temp);
- }
- g_free (wc_key);
-
- if (reg_key != NULL)
- RegCloseKey (reg_key);
-
- g_free (key);
-
- if (result)
- {
- g_hash_table_insert (package_dirs, g_strdup (package), result);
- G_UNLOCK (package_dirs);
- return g_strdup (result);
- }
- G_UNLOCK (package_dirs);
- }
+ g_warning ("Passing a non-NULL package to g_win32_get_package_installation_directory() is deprecated and it is ignored.");
if (dll_name != NULL)
result = get_package_directory_from_module (dll_name);
return result;
}
-#undef g_win32_get_package_installation_directory
+#if !defined (_WIN64)
/* DLL ABI binary compatibility version that uses system codepage file names */
return retval;
}
+#endif
+
/**
* g_win32_get_package_installation_subdirectory:
- * @package: An identifier for a software package, in UTF-8, or %NULL
- * @dll_name: The name of a DLL that a package provides, in UTF-8, or %NULL
+ * @package: (allow-none): You should pass %NULL for this.
+ * @dll_name: (allow-none): The name of a DLL that a package provides, in UTF-8, or %NULL.
* @subdir: A subdirectory of the package installation directory, also in UTF-8
*
+ * This function is deprecated. Use
+ * g_win32_get_package_installation_directory_of_module() and
+ * g_build_filename() instead.
+ *
* Returns a newly-allocated string containing the path of the
* subdirectory @subdir in the return value from calling
* g_win32_get_package_installation_directory() with the @package and
- * @dll_name parameters.
+ * @dll_name parameters. See the documentation for
+ * g_win32_get_package_installation_directory() for more details. In
+ * particular, note that it is deprecated to pass anything except NULL
+ * as @package.
*
* Returns: a string containing the complete path to @subdir inside
* the installation directory of @package. The returned string is in
- * the GLib file name encoding, i.e. UTF-8 on Windows. The return
- * value should be freed with g_free() when no longer needed.
+ * the GLib file name encoding, i.e. UTF-8. The return value should be
+ * freed with g_free() when no longer needed. If something goes wrong,
+ * %NULL is returned.
+ *
+ * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
+ * g_win32_get_package_installation_directory_of_module() instead, and
+ * then construct a subdirectory pathname with g_build_filename().
**/
gchar *
-g_win32_get_package_installation_subdirectory (const gchar *package,
- const gchar *dll_name,
- const gchar *subdir)
+g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
+ const gchar *dll_name,
+ const gchar *subdir)
{
gchar *prefix;
gchar *dirname;
+G_GNUC_BEGIN_IGNORE_DEPRECATIONS
prefix = g_win32_get_package_installation_directory_utf8 (package, dll_name);
+G_GNUC_END_IGNORE_DEPRECATIONS
dirname = g_build_filename (prefix, subdir, NULL);
g_free (prefix);
return dirname;
}
-#undef g_win32_get_package_installation_subdirectory
+#if !defined (_WIN64)
/* DLL ABI binary compatibility version that uses system codepage file names */
gchar *prefix;
gchar *dirname;
+ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
prefix = g_win32_get_package_installation_directory (package, dll_name);
+ G_GNUC_END_IGNORE_DEPRECATIONS
dirname = g_build_filename (prefix, subdir, NULL);
g_free (prefix);
return dirname;
}
-static guint windows_version;
-
-static void
-g_win32_windows_version_init (void)
-{
- static gboolean beenhere = FALSE;
-
- if (!beenhere)
- {
- beenhere = TRUE;
- windows_version = GetVersion ();
-
- if (windows_version & 0x80000000)
- g_error ("This version of GLib requires NT-based Windows.");
- }
-}
-
-void
-_g_win32_thread_init (void)
-{
- g_win32_windows_version_init ();
-}
+#endif
/**
* g_win32_get_windows_version:
* on NT-based systems, so checking whether your are running on Win9x
* in your own software is moot. The least significant byte is 4 on
* Windows NT 4, and 5 on Windows XP. Software that needs really
- * detailled version and feature information should use Win32 API like
+ * detailed version and feature information should use Win32 API like
* GetVersionEx() and VerifyVersionInfo().
*
* Returns: The version information.
guint
g_win32_get_windows_version (void)
{
- g_win32_windows_version_init ();
-
+ static gsize windows_version;
+
+ if (g_once_init_enter (&windows_version))
+ g_once_init_leave (&windows_version, GetVersion ());
+
return windows_version;
}
* The return value is dynamically allocated and should be freed with
* g_free() when no longer needed.
*
- * Return value: The converted filename, or %NULL on conversion
+ * Returns: The converted filename, or %NULL on conversion
* failure and lack of short names.
*
* Since: 2.8
return retval;
}
-#define __G_WIN32_C__
-#include "galiasdef.c"
+/**
+ * g_win32_get_command_line:
+ *
+ * Gets the command line arguments, on Windows, in the GLib filename
+ * encoding (ie: UTF-8).
+ *
+ * Normally, on Windows, the command line arguments are passed to main()
+ * in the system codepage encoding. This prevents passing filenames as
+ * arguments if the filenames contain characters that fall outside of
+ * this codepage. If such filenames are passed, then substitutions
+ * will occur (such as replacing some characters with '?').
+ *
+ * GLib's policy of using UTF-8 as a filename encoding on Windows was
+ * designed to localise the pain of dealing with filenames outside of
+ * the system codepage to one area: dealing with commandline arguments
+ * in main().
+ *
+ * As such, most GLib programs should ignore the value of argv passed to
+ * their main() function and call g_win32_get_command_line() instead.
+ * This will get the "full Unicode" commandline arguments using
+ * GetCommandLineW() and convert it to the GLib filename encoding (which
+ * is UTF-8 on Windows).
+ *
+ * The strings returned by this function are suitable for use with
+ * functions such as g_open() and g_file_new_for_commandline_arg() but
+ * are not suitable for use with g_option_context_parse(), which assumes
+ * that its input will be in the system codepage. The return value is
+ * suitable for use with g_option_context_parse_strv(), however, which
+ * is a better match anyway because it won't leak memory.
+ *
+ * Unlike argv, the returned value is a normal strv and can (and should)
+ * be freed with g_strfreev() when no longer needed.
+ *
+ * Returns: (transfer full): the commandline arguments in the GLib
+ * filename encoding (ie: UTF-8)
+ *
+ * Since: 2.40
+ **/
+gchar **
+g_win32_get_command_line (void)
+{
+ gchar **result;
+ LPWSTR *args;
+ gint i, n;
+
+ args = CommandLineToArgvW (GetCommandLineW(), &n);
+
+ result = g_new (gchar *, n + 1);
+ for (i = 0; i < n; i++)
+ result[i] = g_utf16_to_utf8 (args[i], -1, NULL, NULL, NULL);
+ result[i] = NULL;
+
+ return result;
+}