* 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/>.
*/
/*
* 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
**/
gchar *
g_win32_get_package_installation_directory_of_module (gpointer hmodule)
{
+ gchar *filename;
gchar *retval;
gchar *p;
wchar_t wc_fn[MAX_PATH];
+ /* NOTE: it relies that GetModuleFileNameW returns only canonical paths */
if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
return NULL;
- retval = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
+ filename = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
- if ((p = strrchr (retval, G_DIR_SEPARATOR)) != NULL)
+ if ((p = strrchr (filename, 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';
+ retval = g_strdup (filename);
+
+ do
+ {
+ p = strrchr (retval, G_DIR_SEPARATOR);
+ if (p == NULL)
+ break;
+
+ *p = '\0';
+
+ if (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
+ g_ascii_strcasecmp (p + 1, "lib") == 0)
+ break;
+ }
+ while (p != NULL);
+
+ if (p == NULL)
+ {
+ g_free (retval);
+ retval = filename;
+ }
+ else
+ g_free (filename);
#ifdef G_WITH_CYGWIN
/* In Cygwin we need to have POSIX paths */
*
* The original intended use of @package was for a short identifier of
* the package, typically the same identifier as used for
- * <literal>GETTEXT_PACKAGE</literal> in software configured using GNU
+ * `GETTEXT_PACKAGE` in software configured using GNU
* autotools. 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
+ * 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
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);
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);
* 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;
}
+
+/**
+ * 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;
+}