Partly revert "gio: Add filename type annotations"
[glib.git] / glib / gwin32.c
blobe9081cc30d91a450bdcdbf10cfeb4b99fb072836
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, see <http://www.gnu.org/licenses/>.
20 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GLib Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GLib at ftp://ftp.gtk.org/pub/gtk/.
26 /*
27 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
30 #include "config.h"
32 #include "glibconfig.h"
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <wchar.h>
38 #include <errno.h>
40 #define STRICT /* Strict typing, please */
41 #include <windows.h>
42 #undef STRICT
43 #ifndef G_WITH_CYGWIN
44 #include <direct.h>
45 #endif
46 #include <errno.h>
47 #include <ctype.h>
48 #if defined(_MSC_VER) || defined(__DMC__)
49 # include <io.h>
50 #endif /* _MSC_VER || __DMC__ */
52 #define MODERN_API_FAMILY 2
54 #if WINAPI_FAMILY == MODERN_API_FAMILY
55 /* This is for modern UI Builds, where we can't use LoadLibraryW()/GetProcAddress() */
56 /* ntddk.h is found in the WDK, and MinGW */
57 #include <ntddk.h>
59 #ifdef _MSC_VER
60 #pragma comment (lib, "ntoskrnl.lib")
61 #endif
62 #elif defined (__MINGW32__)
63 /* mingw-w64, not MinGW, has winternl.h */
64 #include <ntdef.h>
65 #else
66 #include <winternl.h>
67 #endif
69 #include "glib.h"
70 #include "gthreadprivate.h"
72 #ifdef G_WITH_CYGWIN
73 #include <sys/cygwin.h>
74 #endif
76 #ifndef G_WITH_CYGWIN
78 gint
79 g_win32_ftruncate (gint fd,
80 guint size)
82 return _chsize (fd, size);
85 #endif
87 /**
88 * g_win32_getlocale:
90 * The setlocale() function in the Microsoft C library uses locale
91 * names of the form "English_United States.1252" etc. We want the
92 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
93 * current thread locale from Windows - without any encoding info -
94 * and returns it as a string of the above form for use in forming
95 * file names etc. The returned string should be deallocated with
96 * g_free().
98 * Returns: newly-allocated locale name.
99 **/
101 #ifndef SUBLANG_SERBIAN_LATIN_BA
102 #define SUBLANG_SERBIAN_LATIN_BA 0x06
103 #endif
105 gchar *
106 g_win32_getlocale (void)
108 LCID lcid;
109 LANGID langid;
110 gchar *ev;
111 gint primary, sub;
112 char iso639[10];
113 char iso3166[10];
114 const gchar *script = NULL;
116 /* Let the user override the system settings through environment
117 * variables, as on POSIX systems. Note that in GTK+ applications
118 * since GTK+ 2.10.7 setting either LC_ALL or LANG also sets the
119 * Win32 locale and C library locale through code in gtkmain.c.
121 if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
122 || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
123 || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
124 return g_strdup (ev);
126 lcid = GetThreadLocale ();
128 if (!GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) ||
129 !GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
130 return g_strdup ("C");
132 /* Strip off the sorting rules, keep only the language part. */
133 langid = LANGIDFROMLCID (lcid);
135 /* Split into language and territory part. */
136 primary = PRIMARYLANGID (langid);
137 sub = SUBLANGID (langid);
139 /* Handle special cases */
140 switch (primary)
142 case LANG_AZERI:
143 switch (sub)
145 case SUBLANG_AZERI_LATIN:
146 script = "@Latn";
147 break;
148 case SUBLANG_AZERI_CYRILLIC:
149 script = "@Cyrl";
150 break;
152 break;
153 case LANG_SERBIAN: /* LANG_CROATIAN == LANG_SERBIAN */
154 switch (sub)
156 case SUBLANG_SERBIAN_LATIN:
157 case 0x06: /* Serbian (Latin) - Bosnia and Herzegovina */
158 script = "@Latn";
159 break;
161 break;
162 case LANG_UZBEK:
163 switch (sub)
165 case SUBLANG_UZBEK_LATIN:
166 script = "@Latn";
167 break;
168 case SUBLANG_UZBEK_CYRILLIC:
169 script = "@Cyrl";
170 break;
172 break;
174 return g_strconcat (iso639, "_", iso3166, script, NULL);
178 * g_win32_error_message:
179 * @error: error code.
181 * Translate a Win32 error code (as returned by GetLastError() or
182 * WSAGetLastError()) into the corresponding message. The message is
183 * either language neutral, or in the thread's language, or the user's
184 * language, the system's language, or US English (see docs for
185 * FormatMessage()). The returned string is in UTF-8. It should be
186 * deallocated with g_free().
188 * Returns: newly-allocated error message
190 gchar *
191 g_win32_error_message (gint error)
193 gchar *retval;
194 wchar_t *msg = NULL;
195 size_t nchars;
197 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
198 |FORMAT_MESSAGE_IGNORE_INSERTS
199 |FORMAT_MESSAGE_FROM_SYSTEM,
200 NULL, error, 0,
201 (LPWSTR) &msg, 0, NULL);
202 if (msg != NULL)
204 nchars = wcslen (msg);
206 if (nchars >= 2 && msg[nchars-1] == L'\n' && msg[nchars-2] == L'\r')
207 msg[nchars-2] = L'\0';
209 retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
211 LocalFree (msg);
213 else
214 retval = g_strdup ("");
216 return retval;
220 * g_win32_get_package_installation_directory_of_module:
221 * @hmodule: (allow-none): The Win32 handle for a DLL loaded into the current process, or %NULL
223 * This function tries to determine the installation directory of a
224 * software package based on the location of a DLL of the software
225 * package.
227 * @hmodule should be the handle of a loaded DLL or %NULL. The
228 * function looks up the directory that DLL was loaded from. If
229 * @hmodule is NULL, the directory the main executable of the current
230 * process is looked up. If that directory's last component is "bin"
231 * or "lib", its parent directory is returned, otherwise the directory
232 * itself.
234 * It thus makes sense to pass only the handle to a "public" DLL of a
235 * software package to this function, as such DLLs typically are known
236 * to be installed in a "bin" or occasionally "lib" subfolder of the
237 * installation folder. DLLs that are of the dynamically loaded module
238 * or plugin variety are often located in more private locations
239 * deeper down in the tree, from which it is impossible for GLib to
240 * deduce the root of the package installation.
242 * The typical use case for this function is to have a DllMain() that
243 * saves the handle for the DLL. Then when code in the DLL needs to
244 * construct names of files in the installation tree it calls this
245 * function passing the DLL handle.
247 * Returns: a string containing the guessed installation directory for
248 * the software package @hmodule is from. The string is in the GLib
249 * file name encoding, i.e. UTF-8. The return value should be freed
250 * with g_free() when not needed any longer. If the function fails
251 * %NULL is returned.
253 * Since: 2.16
255 gchar *
256 g_win32_get_package_installation_directory_of_module (gpointer hmodule)
258 gchar *filename;
259 gchar *retval;
260 gchar *p;
261 wchar_t wc_fn[MAX_PATH];
263 /* NOTE: it relies that GetModuleFileNameW returns only canonical paths */
264 if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
265 return NULL;
267 filename = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
269 if ((p = strrchr (filename, G_DIR_SEPARATOR)) != NULL)
270 *p = '\0';
272 retval = g_strdup (filename);
276 p = strrchr (retval, G_DIR_SEPARATOR);
277 if (p == NULL)
278 break;
280 *p = '\0';
282 if (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
283 g_ascii_strcasecmp (p + 1, "lib") == 0)
284 break;
286 while (p != NULL);
288 if (p == NULL)
290 g_free (retval);
291 retval = filename;
293 else
294 g_free (filename);
296 #ifdef G_WITH_CYGWIN
297 /* In Cygwin we need to have POSIX paths */
299 gchar tmp[MAX_PATH];
301 cygwin_conv_to_posix_path (retval, tmp);
302 g_free (retval);
303 retval = g_strdup (tmp);
305 #endif
307 return retval;
310 static gchar *
311 get_package_directory_from_module (const gchar *module_name)
313 static GHashTable *module_dirs = NULL;
314 G_LOCK_DEFINE_STATIC (module_dirs);
315 HMODULE hmodule = NULL;
316 gchar *fn;
318 G_LOCK (module_dirs);
320 if (module_dirs == NULL)
321 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
323 fn = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
325 if (fn)
327 G_UNLOCK (module_dirs);
328 return g_strdup (fn);
331 if (module_name)
333 wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
334 hmodule = GetModuleHandleW (wc_module_name);
335 g_free (wc_module_name);
337 if (!hmodule)
339 G_UNLOCK (module_dirs);
340 return NULL;
344 fn = g_win32_get_package_installation_directory_of_module (hmodule);
346 if (fn == NULL)
348 G_UNLOCK (module_dirs);
349 return NULL;
352 g_hash_table_insert (module_dirs, module_name ? g_strdup (module_name) : "", fn);
354 G_UNLOCK (module_dirs);
356 return g_strdup (fn);
360 * g_win32_get_package_installation_directory:
361 * @package: (allow-none): You should pass %NULL for this.
362 * @dll_name: (allow-none): The name of a DLL that a package provides in UTF-8, or %NULL.
364 * Try to determine the installation directory for a software package.
366 * This function is deprecated. Use
367 * g_win32_get_package_installation_directory_of_module() instead.
369 * The use of @package is deprecated. You should always pass %NULL. A
370 * warning is printed if non-NULL is passed as @package.
372 * The original intended use of @package was for a short identifier of
373 * the package, typically the same identifier as used for
374 * `GETTEXT_PACKAGE` in software configured using GNU
375 * autotools. The function first looks in the Windows Registry for the
376 * value `#InstallationDirectory` in the key
377 * `#HKLM\Software\@package`, and if that value
378 * exists and is a string, returns that.
380 * It is strongly recommended that packagers of GLib-using libraries
381 * for Windows do not store installation paths in the Registry to be
382 * used by this function as that interfers with having several
383 * parallel installations of the library. Enabling multiple
384 * installations of different versions of some GLib-using library, or
385 * GLib itself, is desirable for various reasons.
387 * For this reason it is recommeded to always pass %NULL as
388 * @package to this function, to avoid the temptation to use the
389 * Registry. In version 2.20 of GLib the @package parameter
390 * will be ignored and this function won't look in the Registry at all.
392 * If @package is %NULL, or the above value isn't found in the
393 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
394 * into the current process. Typically that would be the name of the
395 * DLL calling this function, looking for its installation
396 * directory. The function then asks Windows what directory that DLL
397 * was loaded from. If that directory's last component is "bin" or
398 * "lib", the parent directory is returned, otherwise the directory
399 * itself. If that DLL isn't loaded, the function proceeds as if
400 * @dll_name was %NULL.
402 * If both @package and @dll_name are %NULL, the directory from where
403 * the main executable of the process was loaded is used instead in
404 * the same way as above.
406 * Returns: a string containing the installation directory for
407 * @package. The string is in the GLib file name encoding,
408 * i.e. UTF-8. The return value should be freed with g_free() when not
409 * needed any longer. If the function fails %NULL is returned.
411 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
412 * g_win32_get_package_installation_directory_of_module() instead.
415 gchar *
416 g_win32_get_package_installation_directory_utf8 (const gchar *package,
417 const gchar *dll_name)
419 gchar *result = NULL;
421 if (package != NULL)
422 g_warning ("Passing a non-NULL package to g_win32_get_package_installation_directory() is deprecated and it is ignored.");
424 if (dll_name != NULL)
425 result = get_package_directory_from_module (dll_name);
427 if (result == NULL)
428 result = get_package_directory_from_module (NULL);
430 return result;
433 #if !defined (_WIN64)
435 /* DLL ABI binary compatibility version that uses system codepage file names */
437 gchar *
438 g_win32_get_package_installation_directory (const gchar *package,
439 const gchar *dll_name)
441 gchar *utf8_package = NULL, *utf8_dll_name = NULL;
442 gchar *utf8_retval, *retval;
444 if (package != NULL)
445 utf8_package = g_locale_to_utf8 (package, -1, NULL, NULL, NULL);
447 if (dll_name != NULL)
448 utf8_dll_name = g_locale_to_utf8 (dll_name, -1, NULL, NULL, NULL);
450 utf8_retval =
451 g_win32_get_package_installation_directory_utf8 (utf8_package,
452 utf8_dll_name);
454 retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
456 g_free (utf8_package);
457 g_free (utf8_dll_name);
458 g_free (utf8_retval);
460 return retval;
463 #endif
466 * g_win32_get_package_installation_subdirectory:
467 * @package: (allow-none): You should pass %NULL for this.
468 * @dll_name: (allow-none): The name of a DLL that a package provides, in UTF-8, or %NULL.
469 * @subdir: A subdirectory of the package installation directory, also in UTF-8
471 * This function is deprecated. Use
472 * g_win32_get_package_installation_directory_of_module() and
473 * g_build_filename() instead.
475 * Returns a newly-allocated string containing the path of the
476 * subdirectory @subdir in the return value from calling
477 * g_win32_get_package_installation_directory() with the @package and
478 * @dll_name parameters. See the documentation for
479 * g_win32_get_package_installation_directory() for more details. In
480 * particular, note that it is deprecated to pass anything except NULL
481 * as @package.
483 * Returns: a string containing the complete path to @subdir inside
484 * the installation directory of @package. The returned string is in
485 * the GLib file name encoding, i.e. UTF-8. The return value should be
486 * freed with g_free() when no longer needed. If something goes wrong,
487 * %NULL is returned.
489 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
490 * g_win32_get_package_installation_directory_of_module() instead, and
491 * then construct a subdirectory pathname with g_build_filename().
494 gchar *
495 g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
496 const gchar *dll_name,
497 const gchar *subdir)
499 gchar *prefix;
500 gchar *dirname;
502 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
503 prefix = g_win32_get_package_installation_directory_utf8 (package, dll_name);
504 G_GNUC_END_IGNORE_DEPRECATIONS
506 dirname = g_build_filename (prefix, subdir, NULL);
507 g_free (prefix);
509 return dirname;
512 #if !defined (_WIN64)
514 /* DLL ABI binary compatibility version that uses system codepage file names */
516 gchar *
517 g_win32_get_package_installation_subdirectory (const gchar *package,
518 const gchar *dll_name,
519 const gchar *subdir)
521 gchar *prefix;
522 gchar *dirname;
524 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
525 prefix = g_win32_get_package_installation_directory (package, dll_name);
526 G_GNUC_END_IGNORE_DEPRECATIONS
528 dirname = g_build_filename (prefix, subdir, NULL);
529 g_free (prefix);
531 return dirname;
534 #endif
537 * g_win32_check_windows_version:
538 * @major: major version of Windows
539 * @minor: minor version of Windows
540 * @spver: Windows Service Pack Level, 0 if none
541 * @os_type: Type of Windows OS
543 * Returns whether the version of the Windows operating system the
544 * code is running on is at least the specified major, minor and
545 * service pack versions. See MSDN documentation for the Operating
546 * System Version. Software that needs even more detailed version and
547 * feature information should use the Win32 API VerifyVersionInfo()
548 * directly.
550 * Successive calls of this function can be used for enabling or
551 * disabling features at run-time for a range of Windows versions,
552 * as per the VerifyVersionInfo() API documentation.
554 * Returns: %TRUE if the Windows Version is the same or greater than
555 * the specified major, minor and service pack versions, and
556 * whether the running Windows is a workstation or server edition
557 * of Windows, if specifically specified.
559 * Since: 2.44
561 gboolean
562 g_win32_check_windows_version (const gint major,
563 const gint minor,
564 const gint spver,
565 const GWin32OSType os_type)
567 OSVERSIONINFOEXW osverinfo;
568 gboolean is_ver_checked = FALSE;
569 gboolean is_type_checked = FALSE;
571 #if WINAPI_FAMILY != MODERN_API_FAMILY
572 /* For non-modern UI Apps, use the LoadLibraryW()/GetProcAddress() thing */
573 typedef NTSTATUS (WINAPI fRtlGetVersion) (PRTL_OSVERSIONINFOEXW);
575 fRtlGetVersion *RtlGetVersion;
576 HMODULE hmodule;
577 #endif
578 /* We Only Support Checking for XP or later */
579 g_return_val_if_fail (major >= 5 && (major <=6 || major == 10), FALSE);
580 g_return_val_if_fail ((major >= 5 && minor >= 1) || major >= 6, FALSE);
582 /* Check for Service Pack Version >= 0 */
583 g_return_val_if_fail (spver >= 0, FALSE);
585 #if WINAPI_FAMILY != MODERN_API_FAMILY
586 hmodule = LoadLibraryW (L"ntdll.dll");
587 g_return_val_if_fail (hmodule != NULL, FALSE);
589 RtlGetVersion = (fRtlGetVersion *) GetProcAddress (hmodule, "RtlGetVersion");
590 g_return_val_if_fail (RtlGetVersion != NULL, FALSE);
591 #endif
593 memset (&osverinfo, 0, sizeof (OSVERSIONINFOEXW));
594 osverinfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEXW);
595 RtlGetVersion (&osverinfo);
597 /* check the OS and Service Pack Versions */
598 if (osverinfo.dwMajorVersion > major)
599 is_ver_checked = TRUE;
600 else if (osverinfo.dwMajorVersion == major)
602 if (osverinfo.dwMinorVersion > minor)
603 is_ver_checked = TRUE;
604 else if (osverinfo.dwMinorVersion == minor)
605 if (osverinfo.wServicePackMajor >= spver)
606 is_ver_checked = TRUE;
609 /* Check OS Type */
610 if (is_ver_checked)
612 switch (os_type)
614 case G_WIN32_OS_ANY:
615 is_type_checked = TRUE;
616 break;
617 case G_WIN32_OS_WORKSTATION:
618 if (osverinfo.wProductType == VER_NT_WORKSTATION)
619 is_type_checked = TRUE;
620 break;
621 case G_WIN32_OS_SERVER:
622 if (osverinfo.wProductType == VER_NT_SERVER ||
623 osverinfo.wProductType == VER_NT_DOMAIN_CONTROLLER)
624 is_type_checked = TRUE;
625 break;
626 default:
627 /* shouldn't get here normally */
628 g_warning ("Invalid os_type specified");
629 break;
633 #if WINAPI_FAMILY != MODERN_API_FAMILY
634 FreeLibrary (hmodule);
635 #endif
637 return is_ver_checked && is_type_checked;
641 * g_win32_get_windows_version:
643 * This function is deprecated. Use
644 * g_win32_check_windows_version() instead.
646 * Returns version information for the Windows operating system the
647 * code is running on. See MSDN documentation for the GetVersion()
648 * function. To summarize, the most significant bit is one on Win9x,
649 * and zero on NT-based systems. Since version 2.14, GLib works only
650 * on NT-based systems, so checking whether your are running on Win9x
651 * in your own software is moot. The least significant byte is 4 on
652 * Windows NT 4, and 5 on Windows XP. Software that needs really
653 * detailed version and feature information should use Win32 API like
654 * GetVersionEx() and VerifyVersionInfo().
656 * Returns: The version information.
658 * Deprecated: 2.44: Be aware that for Windows 8.1 and Windows Server
659 * 2012 R2 and later, this will return 62 unless the application is
660 * manifested for Windows 8.1/Windows Server 2012 R2, for example.
661 * MSDN stated that GetVersion(), which is used here, is subject to
662 * further change or removal after Windows 8.1.
664 guint
665 g_win32_get_windows_version (void)
667 static gsize windows_version;
669 if (g_once_init_enter (&windows_version))
670 g_once_init_leave (&windows_version, GetVersion ());
672 return windows_version;
676 * g_win32_locale_filename_from_utf8:
677 * @utf8filename: a UTF-8 encoded filename.
679 * Converts a filename from UTF-8 to the system codepage.
681 * On NT-based Windows, on NTFS file systems, file names are in
682 * Unicode. It is quite possible that Unicode file names contain
683 * characters not representable in the system codepage. (For instance,
684 * Greek or Cyrillic characters on Western European or US Windows
685 * installations, or various less common CJK characters on CJK Windows
686 * installations.)
688 * In such a case, and if the filename refers to an existing file, and
689 * the file system stores alternate short (8.3) names for directory
690 * entries, the short form of the filename is returned. Note that the
691 * "short" name might in fact be longer than the Unicode name if the
692 * Unicode name has very short pathname components containing
693 * non-ASCII characters. If no system codepage name for the file is
694 * possible, %NULL is returned.
696 * The return value is dynamically allocated and should be freed with
697 * g_free() when no longer needed.
699 * Returns: The converted filename, or %NULL on conversion
700 * failure and lack of short names.
702 * Since: 2.8
704 gchar *
705 g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
707 gchar *retval = g_locale_from_utf8 (utf8filename, -1, NULL, NULL, NULL);
709 if (retval == NULL)
711 /* Conversion failed, so convert to wide chars, check if there
712 * is a 8.3 version, and use that.
714 wchar_t *wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL);
715 if (wname != NULL)
717 wchar_t wshortname[MAX_PATH + 1];
718 if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname)))
720 gchar *tem = g_utf16_to_utf8 (wshortname, -1, NULL, NULL, NULL);
721 retval = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);
722 g_free (tem);
724 g_free (wname);
727 return retval;
731 * g_win32_get_command_line:
733 * Gets the command line arguments, on Windows, in the GLib filename
734 * encoding (ie: UTF-8).
736 * Normally, on Windows, the command line arguments are passed to main()
737 * in the system codepage encoding. This prevents passing filenames as
738 * arguments if the filenames contain characters that fall outside of
739 * this codepage. If such filenames are passed, then substitutions
740 * will occur (such as replacing some characters with '?').
742 * GLib's policy of using UTF-8 as a filename encoding on Windows was
743 * designed to localise the pain of dealing with filenames outside of
744 * the system codepage to one area: dealing with commandline arguments
745 * in main().
747 * As such, most GLib programs should ignore the value of argv passed to
748 * their main() function and call g_win32_get_command_line() instead.
749 * This will get the "full Unicode" commandline arguments using
750 * GetCommandLineW() and convert it to the GLib filename encoding (which
751 * is UTF-8 on Windows).
753 * The strings returned by this function are suitable for use with
754 * functions such as g_open() and g_file_new_for_commandline_arg() but
755 * are not suitable for use with g_option_context_parse(), which assumes
756 * that its input will be in the system codepage. The return value is
757 * suitable for use with g_option_context_parse_strv(), however, which
758 * is a better match anyway because it won't leak memory.
760 * Unlike argv, the returned value is a normal strv and can (and should)
761 * be freed with g_strfreev() when no longer needed.
763 * Returns: (transfer full): the commandline arguments in the GLib
764 * filename encoding (ie: UTF-8)
766 * Since: 2.40
768 gchar **
769 g_win32_get_command_line (void)
771 gchar **result;
772 LPWSTR *args;
773 gint i, n;
775 args = CommandLineToArgvW (GetCommandLineW(), &n);
777 result = g_new (gchar *, n + 1);
778 for (i = 0; i < n; i++)
779 result[i] = g_utf16_to_utf8 (args[i], -1, NULL, NULL, NULL);
780 result[i] = NULL;
782 LocalFree (args);
783 return result;