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
4 *
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.1 of the License, or (at your option) any later version.
9 *
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.
14 *
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/>.
17 */
18
19 /*
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/.
24 */
25
26 /*
27 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
28 */
29
30 #include "config.h"
31
32 #include "glibconfig.h"
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <wchar.h>
38 #include <errno.h>
39 #include <fcntl.h>
40
41 #define STRICT /* Strict typing, please */
42 #include <windows.h>
43 #undef STRICT
44 #ifndef G_WITH_CYGWIN
45 #include <direct.h>
46 #endif
47 #include <errno.h>
48 #include <ctype.h>
49 #if defined(_MSC_VER) || defined(__DMC__)
50 # include <io.h>
51 #endif /* _MSC_VER || __DMC__ */
52
53 #define MODERN_API_FAMILY 2
54
55 #if WINAPI_FAMILY == MODERN_API_FAMILY
56 /* This is for modern UI Builds, where we can't use LoadLibraryW()/GetProcAddress() */
57 /* ntddk.h is found in the WDK, and MinGW */
58 #include <ntddk.h>
59
60 #ifdef _MSC_VER
61 #pragma comment (lib, "ntoskrnl.lib")
62 #endif
63 #elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
64 /* mingw-w64 must use winternl.h, but not MinGW */
65 #include <ntdef.h>
66 #else
67 #include <winternl.h>
68 #endif
69
70 #include "glib.h"
71 #include "gthreadprivate.h"
72 #include "glib-init.h"
73
74 #ifdef G_WITH_CYGWIN
75 #include <sys/cygwin.h>
76 #endif
77
78 #ifndef G_WITH_CYGWIN
79
80 gint
g_win32_ftruncate(gint fd,guint size)81 g_win32_ftruncate (gint fd,
82 guint size)
83 {
84 return _chsize (fd, size);
85 }
86
87 #endif
88
89 /**
90 * g_win32_getlocale:
91 *
92 * The setlocale() function in the Microsoft C library uses locale
93 * names of the form "English_United States.1252" etc. We want the
94 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
95 * current thread locale from Windows - without any encoding info -
96 * and returns it as a string of the above form for use in forming
97 * file names etc. The returned string should be deallocated with
98 * g_free().
99 *
100 * Returns: newly-allocated locale name.
101 **/
102
103 #ifndef SUBLANG_SERBIAN_LATIN_BA
104 #define SUBLANG_SERBIAN_LATIN_BA 0x06
105 #endif
106
107 gchar *
g_win32_getlocale(void)108 g_win32_getlocale (void)
109 {
110 gchar *result;
111 LCID lcid;
112 LANGID langid;
113 const gchar *ev;
114 gint primary, sub;
115 WCHAR iso639[10];
116 gchar *iso639_utf8;
117 WCHAR iso3166[10];
118 gchar *iso3166_utf8;
119 const gchar *script = NULL;
120
121 /* Let the user override the system settings through environment
122 * variables, as on POSIX systems. Note that in GTK+ applications
123 * since GTK+ 2.10.7 setting either LC_ALL or LANG also sets the
124 * Win32 locale and C library locale through code in gtkmain.c.
125 */
126 if (((ev = g_getenv ("LC_ALL")) != NULL && ev[0] != '\0')
127 || ((ev = g_getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
128 || ((ev = g_getenv ("LANG")) != NULL && ev[0] != '\0'))
129 return g_strdup (ev);
130
131 lcid = GetThreadLocale ();
132
133 if (!GetLocaleInfoW (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) ||
134 !GetLocaleInfoW (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
135 return g_strdup ("C");
136
137 /* Strip off the sorting rules, keep only the language part. */
138 langid = LANGIDFROMLCID (lcid);
139
140 /* Split into language and territory part. */
141 primary = PRIMARYLANGID (langid);
142 sub = SUBLANGID (langid);
143
144 /* Handle special cases */
145 switch (primary)
146 {
147 case LANG_AZERI:
148 switch (sub)
149 {
150 case SUBLANG_AZERI_LATIN:
151 script = "@Latn";
152 break;
153 case SUBLANG_AZERI_CYRILLIC:
154 script = "@Cyrl";
155 break;
156 }
157 break;
158 case LANG_SERBIAN: /* LANG_CROATIAN == LANG_SERBIAN */
159 switch (sub)
160 {
161 case SUBLANG_SERBIAN_LATIN:
162 case 0x06: /* Serbian (Latin) - Bosnia and Herzegovina */
163 script = "@Latn";
164 break;
165 }
166 break;
167 case LANG_UZBEK:
168 switch (sub)
169 {
170 case SUBLANG_UZBEK_LATIN:
171 script = "@Latn";
172 break;
173 case SUBLANG_UZBEK_CYRILLIC:
174 script = "@Cyrl";
175 break;
176 }
177 break;
178 }
179
180 iso639_utf8 = g_utf16_to_utf8 (iso639, -1, NULL, NULL, NULL);
181 iso3166_utf8 = g_utf16_to_utf8 (iso3166, -1, NULL, NULL, NULL);
182
183 result = g_strconcat (iso639_utf8, "_", iso3166_utf8, script, NULL);
184
185 g_free (iso3166_utf8);
186 g_free (iso639_utf8);
187
188 return result;
189 }
190
191 /**
192 * g_win32_error_message:
193 * @error: error code.
194 *
195 * Translate a Win32 error code (as returned by GetLastError() or
196 * WSAGetLastError()) into the corresponding message. The message is
197 * either language neutral, or in the thread's language, or the user's
198 * language, the system's language, or US English (see docs for
199 * FormatMessage()). The returned string is in UTF-8. It should be
200 * deallocated with g_free().
201 *
202 * Returns: newly-allocated error message
203 **/
204 gchar *
g_win32_error_message(gint error)205 g_win32_error_message (gint error)
206 {
207 gchar *retval;
208 wchar_t *msg = NULL;
209 size_t nchars;
210
211 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
212 |FORMAT_MESSAGE_IGNORE_INSERTS
213 |FORMAT_MESSAGE_FROM_SYSTEM,
214 NULL, error, 0,
215 (LPWSTR) &msg, 0, NULL);
216 if (msg != NULL)
217 {
218 nchars = wcslen (msg);
219
220 if (nchars >= 2 && msg[nchars-1] == L'\n' && msg[nchars-2] == L'\r')
221 msg[nchars-2] = L'\0';
222
223 retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
224
225 LocalFree (msg);
226 }
227 else
228 retval = g_strdup ("");
229
230 return retval;
231 }
232
233 /**
234 * g_win32_get_package_installation_directory_of_module:
235 * @hmodule: (nullable): The Win32 handle for a DLL loaded into the current process, or %NULL
236 *
237 * This function tries to determine the installation directory of a
238 * software package based on the location of a DLL of the software
239 * package.
240 *
241 * @hmodule should be the handle of a loaded DLL or %NULL. The
242 * function looks up the directory that DLL was loaded from. If
243 * @hmodule is NULL, the directory the main executable of the current
244 * process is looked up. If that directory's last component is "bin"
245 * or "lib", its parent directory is returned, otherwise the directory
246 * itself.
247 *
248 * It thus makes sense to pass only the handle to a "public" DLL of a
249 * software package to this function, as such DLLs typically are known
250 * to be installed in a "bin" or occasionally "lib" subfolder of the
251 * installation folder. DLLs that are of the dynamically loaded module
252 * or plugin variety are often located in more private locations
253 * deeper down in the tree, from which it is impossible for GLib to
254 * deduce the root of the package installation.
255 *
256 * The typical use case for this function is to have a DllMain() that
257 * saves the handle for the DLL. Then when code in the DLL needs to
258 * construct names of files in the installation tree it calls this
259 * function passing the DLL handle.
260 *
261 * Returns: a string containing the guessed installation directory for
262 * the software package @hmodule is from. The string is in the GLib
263 * file name encoding, i.e. UTF-8. The return value should be freed
264 * with g_free() when not needed any longer. If the function fails
265 * %NULL is returned.
266 *
267 * Since: 2.16
268 */
269 gchar *
g_win32_get_package_installation_directory_of_module(gpointer hmodule)270 g_win32_get_package_installation_directory_of_module (gpointer hmodule)
271 {
272 gchar *filename;
273 gchar *retval;
274 gchar *p;
275 wchar_t wc_fn[MAX_PATH];
276
277 /* NOTE: it relies that GetModuleFileNameW returns only canonical paths */
278 if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
279 return NULL;
280
281 filename = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
282
283 if ((p = strrchr (filename, G_DIR_SEPARATOR)) != NULL)
284 *p = '\0';
285
286 retval = g_strdup (filename);
287
288 do
289 {
290 p = strrchr (retval, G_DIR_SEPARATOR);
291 if (p == NULL)
292 break;
293
294 *p = '\0';
295
296 if (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
297 g_ascii_strcasecmp (p + 1, "lib") == 0)
298 break;
299 }
300 while (p != NULL);
301
302 if (p == NULL)
303 {
304 g_free (retval);
305 retval = filename;
306 }
307 else
308 g_free (filename);
309
310 #ifdef G_WITH_CYGWIN
311 /* In Cygwin we need to have POSIX paths */
312 {
313 gchar tmp[MAX_PATH];
314
315 cygwin_conv_to_posix_path (retval, tmp);
316 g_free (retval);
317 retval = g_strdup (tmp);
318 }
319 #endif
320
321 return retval;
322 }
323
324 static gchar *
get_package_directory_from_module(const gchar * module_name)325 get_package_directory_from_module (const gchar *module_name)
326 {
327 static GHashTable *module_dirs = NULL;
328 G_LOCK_DEFINE_STATIC (module_dirs);
329 HMODULE hmodule = NULL;
330 gchar *fn;
331
332 G_LOCK (module_dirs);
333
334 if (module_dirs == NULL)
335 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
336
337 fn = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
338
339 if (fn)
340 {
341 G_UNLOCK (module_dirs);
342 return g_strdup (fn);
343 }
344
345 if (module_name)
346 {
347 wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
348 hmodule = GetModuleHandleW (wc_module_name);
349 g_free (wc_module_name);
350
351 if (!hmodule)
352 {
353 G_UNLOCK (module_dirs);
354 return NULL;
355 }
356 }
357
358 fn = g_win32_get_package_installation_directory_of_module (hmodule);
359
360 if (fn == NULL)
361 {
362 G_UNLOCK (module_dirs);
363 return NULL;
364 }
365
366 g_hash_table_insert (module_dirs, module_name ? g_strdup (module_name) : "", fn);
367
368 G_UNLOCK (module_dirs);
369
370 return g_strdup (fn);
371 }
372
373 /**
374 * g_win32_get_package_installation_directory:
375 * @package: (nullable): You should pass %NULL for this.
376 * @dll_name: (nullable): The name of a DLL that a package provides in UTF-8, or %NULL.
377 *
378 * Try to determine the installation directory for a software package.
379 *
380 * This function is deprecated. Use
381 * g_win32_get_package_installation_directory_of_module() instead.
382 *
383 * The use of @package is deprecated. You should always pass %NULL. A
384 * warning is printed if non-NULL is passed as @package.
385 *
386 * The original intended use of @package was for a short identifier of
387 * the package, typically the same identifier as used for
388 * `GETTEXT_PACKAGE` in software configured using GNU
389 * autotools. The function first looks in the Windows Registry for the
390 * value `#InstallationDirectory` in the key
391 * `#HKLM\Software\@package`, and if that value
392 * exists and is a string, returns that.
393 *
394 * It is strongly recommended that packagers of GLib-using libraries
395 * for Windows do not store installation paths in the Registry to be
396 * used by this function as that interfers with having several
397 * parallel installations of the library. Enabling multiple
398 * installations of different versions of some GLib-using library, or
399 * GLib itself, is desirable for various reasons.
400 *
401 * For this reason it is recommended to always pass %NULL as
402 * @package to this function, to avoid the temptation to use the
403 * Registry. In version 2.20 of GLib the @package parameter
404 * will be ignored and this function won't look in the Registry at all.
405 *
406 * If @package is %NULL, or the above value isn't found in the
407 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
408 * into the current process. Typically that would be the name of the
409 * DLL calling this function, looking for its installation
410 * directory. The function then asks Windows what directory that DLL
411 * was loaded from. If that directory's last component is "bin" or
412 * "lib", the parent directory is returned, otherwise the directory
413 * itself. If that DLL isn't loaded, the function proceeds as if
414 * @dll_name was %NULL.
415 *
416 * If both @package and @dll_name are %NULL, the directory from where
417 * the main executable of the process was loaded is used instead in
418 * the same way as above.
419 *
420 * Returns: a string containing the installation directory for
421 * @package. The string is in the GLib file name encoding,
422 * i.e. UTF-8. The return value should be freed with g_free() when not
423 * needed any longer. If the function fails %NULL is returned.
424 *
425 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
426 * g_win32_get_package_installation_directory_of_module() instead.
427 **/
428
429 gchar *
g_win32_get_package_installation_directory(const gchar * package,const gchar * dll_name)430 g_win32_get_package_installation_directory (const gchar *package,
431 const gchar *dll_name)
432 {
433 gchar *result = NULL;
434
435 if (package != NULL)
436 g_warning ("Passing a non-NULL package to g_win32_get_package_installation_directory() is deprecated and it is ignored.");
437
438 if (dll_name != NULL)
439 result = get_package_directory_from_module (dll_name);
440
441 if (result == NULL)
442 result = get_package_directory_from_module (NULL);
443
444 return result;
445 }
446
447 /**
448 * g_win32_get_package_installation_subdirectory:
449 * @package: (nullable): You should pass %NULL for this.
450 * @dll_name: (nullable): The name of a DLL that a package provides, in UTF-8, or %NULL.
451 * @subdir: A subdirectory of the package installation directory, also in UTF-8
452 *
453 * This function is deprecated. Use
454 * g_win32_get_package_installation_directory_of_module() and
455 * g_build_filename() instead.
456 *
457 * Returns a newly-allocated string containing the path of the
458 * subdirectory @subdir in the return value from calling
459 * g_win32_get_package_installation_directory() with the @package and
460 * @dll_name parameters. See the documentation for
461 * g_win32_get_package_installation_directory() for more details. In
462 * particular, note that it is deprecated to pass anything except NULL
463 * as @package.
464 *
465 * Returns: a string containing the complete path to @subdir inside
466 * the installation directory of @package. The returned string is in
467 * the GLib file name encoding, i.e. UTF-8. The return value should be
468 * freed with g_free() when no longer needed. If something goes wrong,
469 * %NULL is returned.
470 *
471 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
472 * g_win32_get_package_installation_directory_of_module() instead, and
473 * then construct a subdirectory pathname with g_build_filename().
474 **/
475
476 gchar *
g_win32_get_package_installation_subdirectory(const gchar * package,const gchar * dll_name,const gchar * subdir)477 g_win32_get_package_installation_subdirectory (const gchar *package,
478 const gchar *dll_name,
479 const gchar *subdir)
480 {
481 gchar *prefix;
482 gchar *dirname;
483
484 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
485 prefix = g_win32_get_package_installation_directory (package, dll_name);
486 G_GNUC_END_IGNORE_DEPRECATIONS
487
488 dirname = g_build_filename (prefix, subdir, NULL);
489 g_free (prefix);
490
491 return dirname;
492 }
493
494 /*
495 * private API to call Windows's RtlGetVersion(), which may need to be called
496 * via GetProcAddress()
497 */
498 gboolean
_g_win32_call_rtl_version(OSVERSIONINFOEXW * info)499 _g_win32_call_rtl_version (OSVERSIONINFOEXW *info)
500 {
501 static OSVERSIONINFOEXW result;
502 static gsize inited = 0;
503
504 g_return_val_if_fail (info != NULL, FALSE);
505
506 if (g_once_init_enter (&inited))
507 {
508 #if WINAPI_FAMILY != MODERN_API_FAMILY
509 /* For non-modern UI Apps, use the LoadLibraryW()/GetProcAddress() thing */
510 typedef NTSTATUS (WINAPI fRtlGetVersion) (PRTL_OSVERSIONINFOEXW);
511
512 fRtlGetVersion *RtlGetVersion;
513 HMODULE hmodule = LoadLibraryW (L"ntdll.dll");
514 g_return_val_if_fail (hmodule != NULL, FALSE);
515
516 RtlGetVersion = (fRtlGetVersion *) GetProcAddress (hmodule, "RtlGetVersion");
517 g_return_val_if_fail (RtlGetVersion != NULL, FALSE);
518 #endif
519
520 memset (&result, 0, sizeof (OSVERSIONINFOEXW));
521 result.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEXW);
522
523 RtlGetVersion (&result);
524
525 #if WINAPI_FAMILY != MODERN_API_FAMILY
526 FreeLibrary (hmodule);
527 #endif
528 g_once_init_leave (&inited, TRUE);
529 }
530
531 *info = result;
532
533 return TRUE;
534 }
535
536 /**
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
542 *
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.
549 *
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.
553 *
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.
558 *
559 * Since: 2.44
560 **/
561 gboolean
g_win32_check_windows_version(const gint major,const gint minor,const gint spver,const GWin32OSType os_type)562 g_win32_check_windows_version (const gint major,
563 const gint minor,
564 const gint spver,
565 const GWin32OSType os_type)
566 {
567 OSVERSIONINFOEXW osverinfo;
568 gboolean is_ver_checked = FALSE;
569 gboolean is_type_checked = FALSE;
570
571 /* We Only Support Checking for XP or later */
572 g_return_val_if_fail (major >= 5 && (major <=6 || major == 10), FALSE);
573 g_return_val_if_fail ((major >= 5 && minor >= 1) || major >= 6, FALSE);
574
575 /* Check for Service Pack Version >= 0 */
576 g_return_val_if_fail (spver >= 0, FALSE);
577 g_return_val_if_fail (_g_win32_call_rtl_version (&osverinfo), FALSE);
578
579 /* check the OS and Service Pack Versions */
580 if (osverinfo.dwMajorVersion > major)
581 is_ver_checked = TRUE;
582 else if (osverinfo.dwMajorVersion == major)
583 {
584 if (osverinfo.dwMinorVersion > minor)
585 is_ver_checked = TRUE;
586 else if (osverinfo.dwMinorVersion == minor)
587 if (osverinfo.wServicePackMajor >= spver)
588 is_ver_checked = TRUE;
589 }
590
591 /* Check OS Type */
592 if (is_ver_checked)
593 {
594 switch (os_type)
595 {
596 case G_WIN32_OS_ANY:
597 is_type_checked = TRUE;
598 break;
599 case G_WIN32_OS_WORKSTATION:
600 if (osverinfo.wProductType == VER_NT_WORKSTATION)
601 is_type_checked = TRUE;
602 break;
603 case G_WIN32_OS_SERVER:
604 if (osverinfo.wProductType == VER_NT_SERVER ||
605 osverinfo.wProductType == VER_NT_DOMAIN_CONTROLLER)
606 is_type_checked = TRUE;
607 break;
608 default:
609 /* shouldn't get here normally */
610 g_warning ("Invalid os_type specified");
611 break;
612 }
613 }
614
615 return is_ver_checked && is_type_checked;
616 }
617
618 /**
619 * g_win32_get_windows_version:
620 *
621 * This function is deprecated. Use
622 * g_win32_check_windows_version() instead.
623 *
624 * Returns version information for the Windows operating system the
625 * code is running on. See MSDN documentation for the GetVersion()
626 * function. To summarize, the most significant bit is one on Win9x,
627 * and zero on NT-based systems. Since version 2.14, GLib works only
628 * on NT-based systems, so checking whether your are running on Win9x
629 * in your own software is moot. The least significant byte is 4 on
630 * Windows NT 4, and 5 on Windows XP. Software that needs really
631 * detailed version and feature information should use Win32 API like
632 * GetVersionEx() and VerifyVersionInfo().
633 *
634 * Returns: The version information.
635 *
636 * Deprecated: 2.44: Be aware that for Windows 8.1 and Windows Server
637 * 2012 R2 and later, this will return 62 unless the application is
638 * manifested for Windows 8.1/Windows Server 2012 R2, for example.
639 * MSDN stated that GetVersion(), which is used here, is subject to
640 * further change or removal after Windows 8.1.
641 **/
642 guint
g_win32_get_windows_version(void)643 g_win32_get_windows_version (void)
644 {
645 static gsize windows_version;
646
647 if (g_once_init_enter (&windows_version))
648 g_once_init_leave (&windows_version, GetVersion ());
649
650 return windows_version;
651 }
652
653 /*
654 * Doesn't use gettext (and gconv), preventing recursive calls when
655 * g_win32_locale_filename_from_utf8() is called during
656 * gettext initialization.
657 */
658 static gchar *
special_wchar_to_locale_encoding(wchar_t * wstring)659 special_wchar_to_locale_encoding (wchar_t *wstring)
660 {
661 int sizeof_output;
662 int wctmb_result;
663 char *result;
664 BOOL not_representable = FALSE;
665
666 sizeof_output = WideCharToMultiByte (CP_ACP,
667 WC_NO_BEST_FIT_CHARS,
668 wstring, -1,
669 NULL, 0,
670 NULL,
671 ¬_representable);
672
673 if (not_representable ||
674 sizeof_output == 0 ||
675 sizeof_output > MAX_PATH)
676 return NULL;
677
678 result = g_malloc0 (sizeof_output + 1);
679
680 wctmb_result = WideCharToMultiByte (CP_ACP,
681 WC_NO_BEST_FIT_CHARS,
682 wstring, -1,
683 result, sizeof_output + 1,
684 NULL,
685 ¬_representable);
686
687 if (wctmb_result == sizeof_output &&
688 not_representable == FALSE)
689 return result;
690
691 g_free (result);
692
693 return NULL;
694 }
695
696 /**
697 * g_win32_locale_filename_from_utf8:
698 * @utf8filename: a UTF-8 encoded filename.
699 *
700 * Converts a filename from UTF-8 to the system codepage.
701 *
702 * On NT-based Windows, on NTFS file systems, file names are in
703 * Unicode. It is quite possible that Unicode file names contain
704 * characters not representable in the system codepage. (For instance,
705 * Greek or Cyrillic characters on Western European or US Windows
706 * installations, or various less common CJK characters on CJK Windows
707 * installations.)
708 *
709 * In such a case, and if the filename refers to an existing file, and
710 * the file system stores alternate short (8.3) names for directory
711 * entries, the short form of the filename is returned. Note that the
712 * "short" name might in fact be longer than the Unicode name if the
713 * Unicode name has very short pathname components containing
714 * non-ASCII characters. If no system codepage name for the file is
715 * possible, %NULL is returned.
716 *
717 * The return value is dynamically allocated and should be freed with
718 * g_free() when no longer needed.
719 *
720 * Returns: The converted filename, or %NULL on conversion
721 * failure and lack of short names.
722 *
723 * Since: 2.8
724 */
725 gchar *
g_win32_locale_filename_from_utf8(const gchar * utf8filename)726 g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
727 {
728 gchar *retval;
729 wchar_t *wname;
730
731 wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL);
732
733 if (wname == NULL)
734 return NULL;
735
736 retval = special_wchar_to_locale_encoding (wname);
737
738 if (retval == NULL)
739 {
740 /* Conversion failed, so check if there is a 8.3 version, and use that. */
741 wchar_t wshortname[MAX_PATH + 1];
742
743 if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname)))
744 retval = special_wchar_to_locale_encoding (wshortname);
745 }
746
747 g_free (wname);
748
749 return retval;
750 }
751
752 /**
753 * g_win32_get_command_line:
754 *
755 * Gets the command line arguments, on Windows, in the GLib filename
756 * encoding (ie: UTF-8).
757 *
758 * Normally, on Windows, the command line arguments are passed to main()
759 * in the system codepage encoding. This prevents passing filenames as
760 * arguments if the filenames contain characters that fall outside of
761 * this codepage. If such filenames are passed, then substitutions
762 * will occur (such as replacing some characters with '?').
763 *
764 * GLib's policy of using UTF-8 as a filename encoding on Windows was
765 * designed to localise the pain of dealing with filenames outside of
766 * the system codepage to one area: dealing with commandline arguments
767 * in main().
768 *
769 * As such, most GLib programs should ignore the value of argv passed to
770 * their main() function and call g_win32_get_command_line() instead.
771 * This will get the "full Unicode" commandline arguments using
772 * GetCommandLineW() and convert it to the GLib filename encoding (which
773 * is UTF-8 on Windows).
774 *
775 * The strings returned by this function are suitable for use with
776 * functions such as g_open() and g_file_new_for_commandline_arg() but
777 * are not suitable for use with g_option_context_parse(), which assumes
778 * that its input will be in the system codepage. The return value is
779 * suitable for use with g_option_context_parse_strv(), however, which
780 * is a better match anyway because it won't leak memory.
781 *
782 * Unlike argv, the returned value is a normal strv and can (and should)
783 * be freed with g_strfreev() when no longer needed.
784 *
785 * Returns: (transfer full): the commandline arguments in the GLib
786 * filename encoding (ie: UTF-8)
787 *
788 * Since: 2.40
789 **/
790 gchar **
g_win32_get_command_line(void)791 g_win32_get_command_line (void)
792 {
793 gchar **result;
794 LPWSTR *args;
795 gint i, n;
796
797 args = CommandLineToArgvW (GetCommandLineW(), &n);
798
799 result = g_new (gchar *, n + 1);
800 for (i = 0; i < n; i++)
801 result[i] = g_utf16_to_utf8 (args[i], -1, NULL, NULL, NULL);
802 result[i] = NULL;
803
804 LocalFree (args);
805 return result;
806 }
807
808 #ifdef G_OS_WIN32
809
810 /* Binary compatibility versions. Not for newly compiled code. */
811
812 _GLIB_EXTERN gchar *g_win32_get_package_installation_directory_utf8 (const gchar *package,
813 const gchar *dll_name);
814
815 _GLIB_EXTERN gchar *g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
816 const gchar *dll_name,
817 const gchar *subdir);
818
819 gchar *
g_win32_get_package_installation_directory_utf8(const gchar * package,const gchar * dll_name)820 g_win32_get_package_installation_directory_utf8 (const gchar *package,
821 const gchar *dll_name)
822 {
823 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
824 return g_win32_get_package_installation_directory (package, dll_name);
825 G_GNUC_END_IGNORE_DEPRECATIONS
826 }
827
828 gchar *
g_win32_get_package_installation_subdirectory_utf8(const gchar * package,const gchar * dll_name,const gchar * subdir)829 g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
830 const gchar *dll_name,
831 const gchar *subdir)
832 {
833 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
834 return g_win32_get_package_installation_subdirectory (package,
835 dll_name,
836 subdir);
837 G_GNUC_END_IGNORE_DEPRECATIONS
838 }
839
840 #endif
841
842 #ifdef G_OS_WIN32
843
844 /* This function looks up two environment
845 * variables, G_WIN32_ALLOC_CONSOLE and G_WIN32_ATTACH_CONSOLE.
846 * G_WIN32_ALLOC_CONSOLE, if set to 1, makes the process
847 * call AllocConsole(). This is useful for binaries that
848 * are compiled to run without automatically-allocated console
849 * (like most GUI applications).
850 * G_WIN32_ATTACH_CONSOLE, if set to a comma-separated list
851 * of one or more strings "stdout", "stdin" and "stderr",
852 * makes the process reopen the corresponding standard streams
853 * to ensure that they are attached to the files that
854 * GetStdHandle() returns, which, hopefully, would be
855 * either a file handle or a console handle.
856 *
857 * This function is called automatically when glib DLL is
858 * attached to a process, from DllMain().
859 */
860 void
g_console_win32_init(void)861 g_console_win32_init (void)
862 {
863 struct
864 {
865 gboolean redirect;
866 FILE *stream;
867 const gchar *stream_name;
868 DWORD std_handle_type;
869 int flags;
870 const gchar *mode;
871 }
872 streams[] =
873 {
874 { FALSE, stdin, "stdin", STD_INPUT_HANDLE, _O_RDONLY, "rb" },
875 { FALSE, stdout, "stdout", STD_OUTPUT_HANDLE, 0, "wb" },
876 { FALSE, stderr, "stderr", STD_ERROR_HANDLE, 0, "wb" },
877 };
878
879 const gchar *attach_envvar;
880 guint i;
881 gchar **attach_strs;
882
883 /* Note: it's not a very good practice to use DllMain()
884 * to call any functions not in Kernel32.dll.
885 * The following only works if there are no weird
886 * circular DLL dependencies that could cause glib DllMain()
887 * to be called before CRT DllMain().
888 */
889
890 if (g_strcmp0 (g_getenv ("G_WIN32_ALLOC_CONSOLE"), "1") == 0)
891 AllocConsole (); /* no error handling, fails if console already exists */
892
893 attach_envvar = g_getenv ("G_WIN32_ATTACH_CONSOLE");
894
895 if (attach_envvar == NULL)
896 return;
897
898 /* Re-use parent console, if we don't have our own.
899 * If we do, it will fail, so just ignore the error.
900 */
901 AttachConsole (ATTACH_PARENT_PROCESS);
902
903 attach_strs = g_strsplit (attach_envvar, ",", -1);
904
905 for (i = 0; attach_strs[i]; i++)
906 {
907 if (g_strcmp0 (attach_strs[i], "stdout") == 0)
908 streams[1].redirect = TRUE;
909 else if (g_strcmp0 (attach_strs[i], "stderr") == 0)
910 streams[2].redirect = TRUE;
911 else if (g_strcmp0 (attach_strs[i], "stdin") == 0)
912 streams[0].redirect = TRUE;
913 else
914 g_warning ("Unrecognized stream name %s", attach_strs[i]);
915 }
916
917 g_strfreev (attach_strs);
918
919 for (i = 0; i < G_N_ELEMENTS (streams); i++)
920 {
921 int old_fd;
922 int backup_fd;
923 int new_fd;
924 int preferred_fd = i;
925 HANDLE std_handle;
926 errno_t errsv = 0;
927
928 if (!streams[i].redirect)
929 continue;
930
931 if (ferror (streams[i].stream) != 0)
932 {
933 g_warning ("Stream %s is in error state", streams[i].stream_name);
934 continue;
935 }
936
937 std_handle = GetStdHandle (streams[i].std_handle_type);
938
939 if (std_handle == INVALID_HANDLE_VALUE)
940 {
941 DWORD gle = GetLastError ();
942 g_warning ("Standard handle for %s can't be obtained: %lu",
943 streams[i].stream_name, gle);
944 continue;
945 }
946
947 old_fd = fileno (streams[i].stream);
948
949 /* We need the stream object to be associated with
950 * any valid integer fd for the code to work.
951 * If it isn't, reopen it with NUL (/dev/null) to
952 * ensure that it is.
953 */
954 if (old_fd < 0)
955 {
956 if (freopen ("NUL", streams[i].mode, streams[i].stream) == NULL)
957 {
958 errsv = errno;
959 g_warning ("Failed to redirect %s: %d - %s",
960 streams[i].stream_name,
961 errsv,
962 strerror (errsv));
963 continue;
964 }
965
966 old_fd = fileno (streams[i].stream);
967
968 if (old_fd < 0)
969 {
970 g_warning ("Stream %s does not have a valid fd",
971 streams[i].stream_name);
972 continue;
973 }
974 }
975
976 new_fd = _open_osfhandle ((intptr_t) std_handle, streams[i].flags);
977
978 if (new_fd < 0)
979 {
980 g_warning ("Failed to create new fd for stream %s",
981 streams[i].stream_name);
982 continue;
983 }
984
985 backup_fd = dup (old_fd);
986
987 if (backup_fd < 0)
988 g_warning ("Failed to backup old fd %d for stream %s",
989 old_fd, streams[i].stream_name);
990
991 errno = 0;
992
993 /* Force old_fd to be associated with the same file
994 * as new_fd, i.e with the standard handle we need
995 * (or, rather, with the same kernel object; handle
996 * value will be different, but the kernel object
997 * won't be).
998 */
999 /* NOTE: MSDN claims that _dup2() returns 0 on success and -1 on error,
1000 * POSIX claims that dup2() reurns new FD on success and -1 on error.
1001 * The "< 0" check satisfies the error condition for either implementation.
1002 */
1003 if (_dup2 (new_fd, old_fd) < 0)
1004 {
1005 errsv = errno;
1006 g_warning ("Failed to substitute fd %d for stream %s: %d : %s",
1007 old_fd, streams[i].stream_name, errsv, strerror (errsv));
1008
1009 _close (new_fd);
1010
1011 if (backup_fd < 0)
1012 continue;
1013
1014 errno = 0;
1015
1016 /* Try to restore old_fd back to its previous
1017 * handle, in case the _dup2() call above succeeded partially.
1018 */
1019 if (_dup2 (backup_fd, old_fd) < 0)
1020 {
1021 errsv = errno;
1022 g_warning ("Failed to restore fd %d for stream %s: %d : %s",
1023 old_fd, streams[i].stream_name, errsv, strerror (errsv));
1024 }
1025
1026 _close (backup_fd);
1027
1028 continue;
1029 }
1030
1031 /* Success, drop the backup */
1032 if (backup_fd >= 0)
1033 _close (backup_fd);
1034
1035 /* Sadly, there's no way to check that preferred_fd
1036 * is currently valid, so we can't back it up.
1037 * Doing operations on invalid FDs invokes invalid
1038 * parameter handler, which is bad for us.
1039 */
1040 if (old_fd != preferred_fd)
1041 /* This extra code will also try to ensure that
1042 * the expected file descriptors 0, 1 and 2 are
1043 * associated with the appropriate standard
1044 * handles.
1045 */
1046 if (_dup2 (new_fd, preferred_fd) < 0)
1047 g_warning ("Failed to dup fd %d into fd %d", new_fd, preferred_fd);
1048
1049 _close (new_fd);
1050 }
1051 }
1052
1053 /* This is a handle to the Vectored Exception Handler that
1054 * we install on library initialization. If installed correctly,
1055 * it will be non-NULL. Only used to later de-install the handler
1056 * on library de-initialization.
1057 */
1058 static void *WinVEH_handle = NULL;
1059
1060 #define DEBUGGER_BUFFER_SIZE (MAX_PATH + 1)
1061 /* This is the debugger that we'll run on crash */
1062 static wchar_t debugger[DEBUGGER_BUFFER_SIZE];
1063
1064 static gsize number_of_exceptions_to_catch = 0;
1065 static DWORD *exceptions_to_catch = NULL;
1066
1067 static HANDLE debugger_wakeup_event = 0;
1068 static DWORD debugger_spawn_flags = 0;
1069
1070 #include "gwin32-private.c"
1071
1072 static char *
copy_chars(char * buffer,gsize * buffer_size,const char * to_copy)1073 copy_chars (char *buffer,
1074 gsize *buffer_size,
1075 const char *to_copy)
1076 {
1077 gsize copy_count = MIN (strlen (to_copy), *buffer_size - 1);
1078 memset (buffer, 0x20, copy_count);
1079 strncpy_s (buffer, *buffer_size, to_copy, _TRUNCATE);
1080 *buffer_size -= copy_count;
1081 return &buffer[copy_count];
1082 }
1083
1084 /* Handles exceptions (useful for debugging).
1085 * Issues a DebugBreak() call if the process is being debugged (not really
1086 * useful - if the process is being debugged, this handler won't be invoked
1087 * anyway). If it is not, runs a debugger from G_DEBUGGER env var,
1088 * substituting first %p in it for PID, and the first %e for the event handle -
1089 * that event should be set once the debugger attaches itself (otherwise the
1090 * only way out of WaitForSingleObject() is to time out after 1 minute).
1091 * For example, G_DEBUGGER can be set to the following command:
1092 * ```
1093 * gdb.exe -ex "attach %p" -ex "signal-event %e" -ex "bt" -ex "c"
1094 * ```
1095 * This will make GDB attach to the process, signal the event (GDB must be
1096 * recent enough for the signal-event command to be available),
1097 * show the backtrace and resume execution, which should make it catch
1098 * the exception when Windows re-raises it again.
1099 * The command line can't be longer than MAX_PATH (260 characters).
1100 *
1101 * This function will only stop (and run a debugger) on the following exceptions:
1102 * * EXCEPTION_ACCESS_VIOLATION
1103 * * EXCEPTION_STACK_OVERFLOW
1104 * * EXCEPTION_ILLEGAL_INSTRUCTION
1105 * To make it stop at other exceptions one should set the G_VEH_CATCH
1106 * environment variable to a list of comma-separated hexadecimal numbers,
1107 * where each number is the code of an exception that should be caught.
1108 * This is done to prevent GLib from breaking when Windows uses
1109 * exceptions to shuttle information (SetThreadName(), OutputDebugString())
1110 * or for control flow.
1111 *
1112 * This function deliberately avoids calling any GLib code.
1113 * This is done on purpose. This function can be called when the program
1114 * is in a bad state (crashing). It can also be called very early, as soon
1115 * as the handler is installed. Therefore, it's imperative that
1116 * it does as little as possible. Preferably, all the work that can be
1117 * done in advance (when the program is not crashing yet) should be done
1118 * in advance.
1119 */
1120 static LONG __stdcall
g_win32_veh_handler(PEXCEPTION_POINTERS ExceptionInfo)1121 g_win32_veh_handler (PEXCEPTION_POINTERS ExceptionInfo)
1122 {
1123 EXCEPTION_RECORD *er;
1124 gsize i;
1125 STARTUPINFOW si;
1126 PROCESS_INFORMATION pi;
1127 #define ITOA_BUFFER_SIZE 100
1128 char itoa_buffer[ITOA_BUFFER_SIZE];
1129 #define DEBUG_STRING_SIZE 1024
1130 gsize dbgs = DEBUG_STRING_SIZE;
1131 char debug_string[DEBUG_STRING_SIZE];
1132 char *dbgp;
1133
1134 if (ExceptionInfo == NULL ||
1135 ExceptionInfo->ExceptionRecord == NULL ||
1136 IsDebuggerPresent () ||
1137 debugger[0] == 0)
1138 return EXCEPTION_CONTINUE_SEARCH;
1139
1140 er = ExceptionInfo->ExceptionRecord;
1141
1142 switch (er->ExceptionCode)
1143 {
1144 case EXCEPTION_ACCESS_VIOLATION:
1145 case EXCEPTION_STACK_OVERFLOW:
1146 case EXCEPTION_ILLEGAL_INSTRUCTION:
1147 break;
1148 default:
1149 for (i = 0; i < number_of_exceptions_to_catch; i++)
1150 if (exceptions_to_catch[i] == er->ExceptionCode)
1151 break;
1152
1153 if (i == number_of_exceptions_to_catch)
1154 return EXCEPTION_CONTINUE_SEARCH;
1155
1156 break;
1157 }
1158
1159 memset (&si, 0, sizeof (si));
1160 memset (&pi, 0, sizeof (pi));
1161 si.cb = sizeof (si);
1162
1163 /* Run the debugger */
1164 if (0 != CreateProcessW (NULL, debugger, NULL, NULL, TRUE, debugger_spawn_flags, NULL, NULL, &si, &pi))
1165 {
1166 CloseHandle (pi.hProcess);
1167 CloseHandle (pi.hThread);
1168 /* If successful, wait for 60 seconds on the event
1169 * we passed. The debugger should signal that event.
1170 * 60 second limit is here to prevent us from hanging
1171 * up forever in case the debugger does not support
1172 * event signalling.
1173 */
1174 WaitForSingleObject (debugger_wakeup_event, 60000);
1175
1176 dbgp = &debug_string[0];
1177
1178 dbgp = copy_chars (dbgp, &dbgs, "Exception code=0x");
1179 itoa_buffer[0] = 0;
1180 _ui64toa_s (er->ExceptionCode, itoa_buffer, ITOA_BUFFER_SIZE, 16);
1181 dbgp = copy_chars (dbgp, &dbgs, itoa_buffer);
1182 dbgp = copy_chars (dbgp, &dbgs, " flags=0x");
1183 itoa_buffer[0] = 0;
1184 _ui64toa_s (er->ExceptionFlags, itoa_buffer, ITOA_BUFFER_SIZE, 16);
1185 dbgp = copy_chars (dbgp, &dbgs, itoa_buffer);
1186 dbgp = copy_chars (dbgp, &dbgs, " at 0x");
1187 itoa_buffer[0] = 0;
1188 _ui64toa_s ((guintptr) er->ExceptionAddress, itoa_buffer, ITOA_BUFFER_SIZE, 16);
1189 dbgp = copy_chars (dbgp, &dbgs, itoa_buffer);
1190
1191 switch (er->ExceptionCode)
1192 {
1193 case EXCEPTION_ACCESS_VIOLATION:
1194 dbgp = copy_chars (dbgp, &dbgs, ". Access violation - attempting to ");
1195 if (er->ExceptionInformation[0] == 0)
1196 dbgp = copy_chars (dbgp, &dbgs, "read data");
1197 else if (er->ExceptionInformation[0] == 1)
1198 dbgp = copy_chars (dbgp, &dbgs, "write data");
1199 else if (er->ExceptionInformation[0] == 8)
1200 dbgp = copy_chars (dbgp, &dbgs, "execute data");
1201 else
1202 dbgp = copy_chars (dbgp, &dbgs, "do something bad");
1203 dbgp = copy_chars (dbgp, &dbgs, " at address 0x");
1204 itoa_buffer[0] = 0;
1205 _ui64toa_s (er->ExceptionInformation[1], itoa_buffer, ITOA_BUFFER_SIZE, 16);
1206 dbgp = copy_chars (dbgp, &dbgs, itoa_buffer);
1207 break;
1208 case EXCEPTION_IN_PAGE_ERROR:
1209 dbgp = copy_chars (dbgp, &dbgs, ". Page access violation - attempting to ");
1210 if (er->ExceptionInformation[0] == 0)
1211 dbgp = copy_chars (dbgp, &dbgs, "read from an inaccessible page");
1212 else if (er->ExceptionInformation[0] == 1)
1213 dbgp = copy_chars (dbgp, &dbgs, "write to an inaccessible page");
1214 else if (er->ExceptionInformation[0] == 8)
1215 dbgp = copy_chars (dbgp, &dbgs, "execute data in page");
1216 else
1217 dbgp = copy_chars (dbgp, &dbgs, "do something bad with a page");
1218 dbgp = copy_chars (dbgp, &dbgs, " at address 0x");
1219 itoa_buffer[0] = 0;
1220 _ui64toa_s (er->ExceptionInformation[1], itoa_buffer, ITOA_BUFFER_SIZE, 16);
1221 dbgp = copy_chars (dbgp, &dbgs, itoa_buffer);
1222 dbgp = copy_chars (dbgp, &dbgs, " with status ");
1223 itoa_buffer[0] = 0;
1224 _ui64toa_s (er->ExceptionInformation[2], itoa_buffer, ITOA_BUFFER_SIZE, 16);
1225 dbgp = copy_chars (dbgp, &dbgs, itoa_buffer);
1226 break;
1227 default:
1228 break;
1229 }
1230
1231 dbgp = copy_chars (dbgp, &dbgs, "\n");
1232 OutputDebugStringA (debug_string);
1233 }
1234
1235 /* Now the debugger is present, and we can try
1236 * resuming execution, re-triggering the exception,
1237 * which will be caught by debugger this time around.
1238 */
1239 if (IsDebuggerPresent ())
1240 return EXCEPTION_CONTINUE_EXECUTION;
1241
1242 return EXCEPTION_CONTINUE_SEARCH;
1243 }
1244
1245 static gsize
parse_catch_list(const wchar_t * catch_buffer,DWORD * exceptions,gsize num_exceptions)1246 parse_catch_list (const wchar_t *catch_buffer,
1247 DWORD *exceptions,
1248 gsize num_exceptions)
1249 {
1250 const wchar_t *catch_list = catch_buffer;
1251 gsize result = 0;
1252 gsize i = 0;
1253
1254 while (catch_list != NULL &&
1255 catch_list[0] != 0)
1256 {
1257 unsigned long catch_code;
1258 wchar_t *end;
1259 errno = 0;
1260 catch_code = wcstoul (catch_list, &end, 16);
1261 if (errno != NO_ERROR)
1262 break;
1263 catch_list = end;
1264 if (catch_list != NULL && catch_list[0] == L',')
1265 catch_list++;
1266 if (exceptions && i < num_exceptions)
1267 exceptions[i++] = catch_code;
1268 }
1269
1270 return result;
1271 }
1272
1273 void
g_crash_handler_win32_init(void)1274 g_crash_handler_win32_init (void)
1275 {
1276 wchar_t debugger_env[DEBUGGER_BUFFER_SIZE];
1277 #define CATCH_BUFFER_SIZE 1024
1278 wchar_t catch_buffer[CATCH_BUFFER_SIZE];
1279 SECURITY_ATTRIBUTES sa;
1280
1281 if (WinVEH_handle != NULL)
1282 return;
1283
1284 /* Do not register an exception handler if we're not supposed to catch any
1285 * exceptions. Exception handlers are considered dangerous to use, and can
1286 * break advanced exception handling such as in CLRs like C# or other managed
1287 * code. See: http://www.windows-tech.info/13/785f590867bd6316.php
1288 */
1289 debugger_env[0] = 0;
1290 if (!GetEnvironmentVariableW (L"G_DEBUGGER", debugger_env, DEBUGGER_BUFFER_SIZE))
1291 return;
1292
1293 /* Create an inheritable event */
1294 memset (&sa, 0, sizeof (sa));
1295 sa.nLength = sizeof (sa);
1296 sa.bInheritHandle = TRUE;
1297 debugger_wakeup_event = CreateEvent (&sa, FALSE, FALSE, NULL);
1298
1299 /* Put process ID and event handle into debugger commandline */
1300 if (!_g_win32_subst_pid_and_event_w (debugger, G_N_ELEMENTS (debugger),
1301 debugger_env, GetCurrentProcessId (),
1302 (guintptr) debugger_wakeup_event))
1303 {
1304 CloseHandle (debugger_wakeup_event);
1305 debugger_wakeup_event = 0;
1306 debugger[0] = 0;
1307 return;
1308 }
1309 debugger[MAX_PATH] = L'\0';
1310
1311 catch_buffer[0] = 0;
1312 if (GetEnvironmentVariableW (L"G_VEH_CATCH", catch_buffer, CATCH_BUFFER_SIZE))
1313 {
1314 number_of_exceptions_to_catch = parse_catch_list (catch_buffer, NULL, 0);
1315 if (number_of_exceptions_to_catch > 0)
1316 {
1317 exceptions_to_catch = g_new0 (DWORD, number_of_exceptions_to_catch);
1318 parse_catch_list (catch_buffer, exceptions_to_catch, number_of_exceptions_to_catch);
1319 }
1320 }
1321
1322 if (GetEnvironmentVariableW (L"G_DEBUGGER_OLD_CONSOLE", (wchar_t *) &debugger_spawn_flags, 1))
1323 debugger_spawn_flags = 0;
1324 else
1325 debugger_spawn_flags = CREATE_NEW_CONSOLE;
1326
1327 WinVEH_handle = AddVectoredExceptionHandler (0, &g_win32_veh_handler);
1328 }
1329
1330 void
g_crash_handler_win32_deinit(void)1331 g_crash_handler_win32_deinit (void)
1332 {
1333 if (WinVEH_handle != NULL)
1334 RemoveVectoredExceptionHandler (WinVEH_handle);
1335
1336 WinVEH_handle = NULL;
1337 }
1338
1339 #endif
1340