Add a couple of languages that have LANG_* codes in newest headers, just
[glib.git] / glib / gwin32.c
blob13fd2b8afdccb0b0618d6200833029afec907772
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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GLib Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GLib at ftp://ftp.gtk.org/pub/gtk/.
28 /*
29 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
36 #include "glibconfig.h"
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <errno.h>
43 #define STRICT /* Strict typing, please */
44 #include <windows.h>
45 #undef STRICT
46 #ifndef G_WITH_CYGWIN
47 #include <direct.h>
48 #endif
49 #include <errno.h>
50 #include <ctype.h>
51 #ifdef _MSC_VER
52 # include <io.h>
53 #endif /* _MSC_VER */
55 #include "glib.h"
57 #ifdef G_WITH_CYGWIN
58 #include <sys/cygwin.h>
59 #endif
61 #ifndef G_WITH_CYGWIN
63 gint
64 g_win32_ftruncate (gint fd,
65 guint size)
67 HANDLE hfile;
68 guint curpos;
70 g_return_val_if_fail (fd >= 0, -1);
72 hfile = (HANDLE) _get_osfhandle (fd);
73 curpos = SetFilePointer (hfile, 0, NULL, FILE_CURRENT);
74 if (curpos == 0xFFFFFFFF
75 || SetFilePointer (hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
76 || !SetEndOfFile (hfile))
78 gint error = GetLastError ();
80 switch (error)
82 case ERROR_INVALID_HANDLE:
83 errno = EBADF;
84 break;
85 default:
86 errno = EIO;
87 break;
90 return -1;
93 return 0;
96 #endif
98 /**
99 * g_win32_getlocale:
101 * The setlocale in the Microsoft C library uses locale names of the
102 * form "English_United States.1252" etc. We want the Unixish standard
103 * form "en_US", "zh_TW" etc. This function gets the current thread
104 * locale from Windows - without any encoding info - and returns it as
105 * a string of the above form for use in forming file names etc. The
106 * returned string should be deallocated with g_free().
108 * Returns: newly-allocated locale name.
111 gchar *
112 g_win32_getlocale (void)
114 LCID lcid;
115 LANGID langid;
116 gchar *ev;
117 gint primary, sub;
118 gchar *l = NULL, *sl = NULL;
119 gchar bfr[20];
121 /* Let the user override the system settings through environment
122 variables, as on POSIX systems. */
123 if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
124 || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
125 || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
126 return g_strdup (ev);
128 /* Use native Win32 API locale ID. */
129 lcid = GetThreadLocale ();
131 /* Strip off the sorting rules, keep only the language part. */
132 langid = LANGIDFROMLCID (lcid);
134 /* Split into language and territory part. */
135 primary = PRIMARYLANGID (langid);
136 sub = SUBLANGID (langid);
137 switch (primary)
139 case LANG_AFRIKAANS: l = "af"; sl = "ZA"; break;
140 case LANG_ALBANIAN: l = "sq"; sl = "AL"; break;
141 case LANG_ARABIC:
142 l = "ar";
143 switch (sub)
145 case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
146 case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
147 case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
148 case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
149 case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
150 case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
151 case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
152 case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
153 case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
154 case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
155 case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
156 case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
157 case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
158 case SUBLANG_ARABIC_UAE: sl = "AE"; break;
159 case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
160 case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
162 break;
163 #ifdef LANG_ARMENIAN
164 case LANG_ARMENIAN: l = "hy"; sl = "AM"; break;
165 #endif
166 #ifdef LANG_ASSAMESE
167 case LANG_ASSAMESE: l = "as"; sl = "IN"; break;
168 #endif
169 #ifdef LANG_AZERI
170 case LANG_AZERI:
171 l = "az";
172 #if defined (SUBLANG_AZERI_LATIN) && defined (SUBLANG_AZERI_CYRILLIC)
173 switch (sub)
175 /* FIXME: Adjust this when Azerbaijani locales appear on Unix. */
176 case SUBLANG_AZERI_LATIN: sl = "@latin"; break;
177 case SUBLANG_AZERI_CYRILLIC: sl = "@cyrillic"; break;
179 #endif
180 break;
181 #endif
182 case LANG_BASQUE:
183 l = "eu"; /* sl could be "ES" or "FR". */
184 break;
185 case LANG_BELARUSIAN: l = "be"; sl = "BY"; break;
186 #ifdef LANG_BENGALI
187 case LANG_BENGALI: l = "bn"; sl = "IN"; break;
188 #endif
189 case LANG_BULGARIAN: l = "bg"; sl = "BG"; break;
190 case LANG_CATALAN: l = "ca"; sl = "ES"; break;
191 case LANG_CHINESE:
192 l = "zh";
193 switch (sub)
195 case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
196 case SUBLANG_CHINESE_SIMPLIFIED: sl = "CN"; break;
197 case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
198 case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
199 #ifdef SUBLANG_CHINESE_MACAU
200 case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
201 #endif
203 break;
204 case LANG_CROATIAN: /* LANG_CROATIAN == LANG_SERBIAN
205 * What used to be called Serbo-Croatian
206 * should really now be two separate
207 * languages because of political reasons.
208 * (Says tml, who knows nothing about Serbian
209 * or Croatian.)
210 * (I can feel those flames coming already.)
212 switch (sub)
214 /* FIXME: How to distinguish Croatian and Latin Serbian locales? */
215 case SUBLANG_SERBIAN_LATIN: l = "sr"; break;
216 case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; sl = "@cyrillic"; break;
217 default: l = "hr"; sl = "HR";
219 break;
220 case LANG_CZECH: l = "cs"; sl = "CZ"; break;
221 case LANG_DANISH: l = "da"; sl = "DK"; break;
222 #ifdef LANG_DIVEHI
223 case LANG_DIVEHI: l = "div"; sl = "MV"; break;
224 #endif
225 case LANG_DUTCH:
226 l = "nl";
227 switch (sub)
229 case SUBLANG_DUTCH: sl = "NL"; break;
230 case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
232 break;
233 case LANG_ENGLISH:
234 l = "en";
235 switch (sub)
237 case SUBLANG_ENGLISH_US: sl = "US"; break;
238 case SUBLANG_ENGLISH_UK: sl = "GB"; break;
239 case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
240 case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
241 case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
242 case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
243 case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "ZA"; break;
244 case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
245 case SUBLANG_ENGLISH_CARIBBEAN: sl = "GD"; break; /* Grenada? */
246 case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
247 case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
248 #ifdef SUBLANG_ENGLISH_ZIMBABWE
249 case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
250 #endif
251 #ifdef SUBLANG_ENGLISH_PHILIPPINES
252 case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
253 #endif
255 break;
256 case LANG_ESTONIAN: l = "et"; sl = "EE"; break;
257 case LANG_FAEROESE: l = "fo"; sl = "FO"; break;
258 case LANG_FARSI: l = "fa"; sl = "IR"; break;
259 case LANG_FINNISH: l = "fi"; sl = "FI"; break;
260 case LANG_FRENCH:
261 l = "fr";
262 switch (sub)
264 case SUBLANG_FRENCH: sl = "FR"; break;
265 case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
266 case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
267 case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
268 case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
269 #ifdef SUBLANG_FRENCH_MONACO
270 case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
271 #endif
273 break;
274 /* FIXME: LANG_GALICIAN: What's the code for Galician? */
275 #ifdef LANG_GEORGIAN
276 case LANG_GEORGIAN: l = "ka"; sl = "GE"; break;
277 #endif
278 case LANG_GERMAN:
279 l = "de";
280 switch (sub)
282 case SUBLANG_GERMAN: sl = "DE"; break;
283 case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
284 case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
285 case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
286 case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
288 break;
289 case LANG_GREEK: l = "el"; sl = "GR"; break;
290 #ifdef LANG_GUJARATI
291 case LANG_GUJARATI: l = "gu"; sl = "IN"; break;
292 #endif
293 case LANG_HEBREW: l = "he"; sl = "IL"; break;
294 #ifdef LANG_HINDI
295 case LANG_HINDI: l = "hi"; sl = "IN"; break;
296 #endif
297 case LANG_HUNGARIAN: l = "hu"; sl = "HU"; break;
298 case LANG_ICELANDIC: l = "is"; sl = "IS"; break;
299 case LANG_INDONESIAN: l = "id"; sl = "ID"; break;
300 case LANG_ITALIAN:
301 l = "it";
302 switch (sub)
304 case SUBLANG_ITALIAN: sl = "IT"; break;
305 case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
307 break;
308 case LANG_JAPANESE: l = "ja"; sl = "JP"; break;
309 #ifdef LANG_KANNADA
310 case LANG_KANNADA: l = "kn"; sl = "IN"; break;
311 #endif
312 #ifdef LANG_KASHMIRI
313 case LANG_KASHMIRI:
314 l = "ks";
315 switch (sub)
317 case SUBLANG_DEFAULT: sl = "PK"; break;
318 #ifdef SUBLANG_KASHMIRI_INDIA
319 case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
320 #endif
322 break;
323 #endif
324 #ifdef LANG_KAZAK
325 case LANG_KAZAK: l = "kk"; sl = "KZ"; break;
326 #endif
327 #ifdef LANG_KONKANI
328 case LANG_KONKANI:
329 /* FIXME: Adjust this when such locales appear on Unix. */
330 l = "kok"; sl = "IN";
331 break;
332 #endif
333 case LANG_KOREAN: l = "ko"; sl = "KR"; break;
334 #ifdef LANG_KYRGYZ
335 case LANG_KYRGYZ: l = "ky"; sl = "KG"; /* ??? */ break;
336 #endif
337 case LANG_LATVIAN: l = "lv"; sl = "LV"; break;
338 case LANG_LITHUANIAN: l = "lt"; sl = "LT"; break;
339 #ifdef LANG_MACEDONIAN
340 case LANG_MACEDONIAN: l = "mk"; sl = "MK"; break;
341 #endif
342 #ifdef LANG_MALAY
343 case LANG_MALAY:
344 l = "ms";
345 switch (sub)
347 #ifdef SUBLANG_MALAY_MALAYSIA
348 case SUBLANG_MALAY_MALAYSIA: sl = "MY"; break;
349 #endif
350 #ifdef SUBLANG_MALAY_BRUNEI_DARUSSALAM
351 case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
352 #endif
354 break;
355 #endif
356 #ifdef LANG_MALAYALAM
357 case LANG_MALAYALAM: l = "ml"; sl = "IN"; break;
358 #endif
359 #ifdef LANG_MANIPURI
360 case LANG_MANIPURI:
361 /* FIXME: Adjust this when such locales appear on Unix. */
362 l = "mni"; sl = "IN";
363 break;
364 #endif
365 #ifdef LANG_MARATHI
366 case LANG_MARATHI: l = "mr"; sl = "IN"; break;
367 #endif
368 #ifdef LANG_MONGOLIAN
369 case LANG_MONGOLIAN: l = "mn"; sl = "MN"; break;
370 #endif
371 #ifdef LANG_NEPALI
372 case LANG_NEPALI:
373 l = "ne";
374 switch (sub)
376 case SUBLANG_DEFAULT: sl = "NP"; break;
377 #ifdef SUBLANG_NEPALI_INDIA
378 case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
379 #endif
381 break;
382 #endif
383 case LANG_NORWEGIAN:
384 l = "no";
385 switch (sub)
387 case SUBLANG_NORWEGIAN_BOKMAL: sl = "NO"; break;
388 case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; sl = "NO"; break;
390 break;
391 #ifdef LANG_ORIYA
392 case LANG_ORIYA: l = "or"; sl = "IN"; break;
393 #endif
394 case LANG_POLISH: l = "pl"; sl = "PL"; break;
395 case LANG_PORTUGUESE:
396 l = "pt";
397 switch (sub)
399 case SUBLANG_PORTUGUESE: sl = "PT"; break;
400 case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
402 break;
403 #ifdef LANG_PUNJABI
404 case LANG_PUNJABI: l = "pa"; sl = "IN"; break;
405 #endif
406 case LANG_ROMANIAN: l = "ro"; sl = "RO"; break;
407 case LANG_RUSSIAN:
408 l = "ru"; /* sl could be "RU" or "UA". */
409 break;
410 #ifdef LANG_SANSKRIT
411 case LANG_SANSKRIT: l = "sa"; sl = "IN"; break;
412 #endif
413 #ifdef LANG_SINDHI
414 case LANG_SINDHI: l = "sd"; break;
415 #endif
416 case LANG_SLOVAK: l = "sk"; sl = "SK"; break;
417 case LANG_SLOVENIAN: l = "sl"; sl = "SI"; break;
418 #ifdef LANG_SORBIAN
419 case LANG_SORBIAN:
420 /* FIXME: Adjust this when such locales appear on Unix. */
421 l = "wen"; sl = "DE";
422 break;
423 #endif
424 case LANG_SPANISH:
425 l = "es";
426 switch (sub)
428 case SUBLANG_SPANISH: sl = "ES"; break;
429 case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
430 case SUBLANG_SPANISH_MODERN:
431 sl = "ES@modern"; break; /* not seen on Unix */
432 case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
433 case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
434 case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
435 case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
436 case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
437 case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
438 case SUBLANG_SPANISH_PERU: sl = "PE"; break;
439 case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
440 case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
441 case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
442 case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
443 case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
444 case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
445 case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
446 case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
447 case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
448 case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
450 break;
451 #ifdef LANG_SWAHILI
452 case LANG_SWAHILI: l = "sw"; break;
453 #endif
454 case LANG_SWEDISH:
455 l = "sv";
456 switch (sub)
458 case SUBLANG_DEFAULT: sl = "SE"; break;
459 case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
461 break;
462 #ifdef LANG_SYRIAC
463 case LANG_SYRIAC: l = "syr"; break;
464 #endif
465 #ifdef LANG_TAMIL
466 case LANG_TAMIL:
467 l = "ta"; /* sl could be "IN" or "LK". */
468 break;
469 #endif
470 #ifdef LANG_TATAR
471 case LANG_TATAR: l = "tt"; break;
472 #endif
473 #ifdef LANG_TELUGU
474 case LANG_TELUGU: l = "te"; sl = "IN"; break;
475 #endif
476 case LANG_THAI: l = "th"; sl = "TH"; break;
477 case LANG_TURKISH: l = "tr"; sl = "TR"; break;
478 case LANG_UKRAINIAN: l = "uk"; sl = "UA"; break;
479 #ifdef LANG_URDU
480 case LANG_URDU:
481 l = "ur";
482 switch (sub)
484 #ifdef SUBLANG_URDU_PAKISTAN
485 case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
486 #endif
487 #ifdef SUBLANG_URDU_INDIA
488 case SUBLANG_URDU_INDIA: sl = "IN"; break;
489 #endif
491 break;
492 #endif
493 #ifdef LANG_UZBEK
494 case LANG_UZBEK:
495 l = "uz";
496 switch (sub)
498 /* FIXME: Adjust this when Uzbek locales appear on Unix. */
499 #ifdef SUBLANG_UZBEK_LATIN
500 case SUBLANG_UZBEK_LATIN: sl = "UZ@latin"; break;
501 #endif
502 #ifdef SUBLANG_UZBEK_CYRILLIC
503 case SUBLANG_UZBEK_CYRILLIC: sl = "UZ@cyrillic"; break;
504 #endif
506 break;
507 #endif
508 case LANG_VIETNAMESE: l = "vi"; sl = "VN"; break;
509 default: l = "xx"; break;
511 strcpy (bfr, l);
512 if (sl != NULL)
514 if (sl[0] != '@')
515 strcat (bfr, "_");
516 strcat (bfr, sl);
519 return g_strdup (bfr);
523 * g_win32_error_message:
524 * @error: error code.
526 * Translate a Win32 error code (as returned by GetLastError()) into
527 * the corresponding message. The message is either language neutral,
528 * or in the thread's language, or the user's language, the system's
529 * language, or US English (see docs for <function>FormatMessage()</function>). *
530 * The returned string should be deallocated with g_free().
532 * Returns: newly-allocated error message
534 gchar *
535 g_win32_error_message (gint error)
537 gchar *msg;
538 gchar *retval;
539 int nbytes;
541 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
542 |FORMAT_MESSAGE_IGNORE_INSERTS
543 |FORMAT_MESSAGE_FROM_SYSTEM,
544 NULL, error, 0,
545 (LPTSTR) &msg, 0, NULL);
546 nbytes = strlen (msg);
548 if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
549 msg[nbytes-2] = '\0';
551 retval = g_strdup (msg);
553 if (msg != NULL)
554 LocalFree (msg);
556 return retval;
559 static gchar *
560 get_package_directory_from_module (gchar *module_name)
562 static GHashTable *module_dirs = NULL;
563 G_LOCK_DEFINE_STATIC (module_dirs);
564 HMODULE hmodule = NULL;
565 gchar *fn;
566 gchar *p;
567 gchar *result;
569 G_LOCK (module_dirs);
571 if (module_dirs == NULL)
572 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
574 result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
576 if (result)
578 G_UNLOCK (module_dirs);
579 return g_strdup (result);
582 if (module_name)
584 hmodule = GetModuleHandle (module_name);
585 if (!hmodule)
586 return NULL;
589 fn = g_malloc (MAX_PATH);
590 if (!GetModuleFileName (hmodule, fn, MAX_PATH))
592 G_UNLOCK (module_dirs);
593 g_free (fn);
594 return NULL;
597 if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
598 *p = '\0';
600 p = strrchr (fn, G_DIR_SEPARATOR);
601 if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
602 g_ascii_strcasecmp (p + 1, "lib") == 0))
603 *p = '\0';
605 #ifdef G_WITH_CYGWIN
606 /* In Cygwin we need to have POSIX paths */
608 gchar tmp[MAX_PATH];
610 cygwin_conv_to_posix_path(fn, tmp);
611 g_free(fn);
612 fn = g_strdup(tmp);
614 #endif
616 g_hash_table_insert (module_dirs, module_name ? module_name : "", fn);
618 G_UNLOCK (module_dirs);
620 return g_strdup (fn);
624 * g_win32_get_package_installation_directory:
625 * @package: An identifier for a software package, or %NULL
626 * @dll_name: The name of a DLL that a package provides, or %NULL.
628 * Try to determine the installation directory for a software package.
629 * Typically used by GNU software packages.
631 * @package should be a short identifier for the package. Typically it
632 * is the same identifier as used for
633 * <literal>GETTEXT_PACKAGE</literal> in software configured according
634 * to GNU standards. The function first looks in the Windows Registry
635 * for the value <literal>#InstallationDirectory</literal> in the key
636 * <literal>#HKLM\Software\@package</literal>, and if that value
637 * exists and is a string, returns that.
639 * If @package is %NULL, or the above value isn't found in the
640 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
641 * into the current process. Typically that would be the name of the
642 * DLL calling this function, looking for its installation
643 * directory. The function then asks Windows what directory that DLL
644 * was loaded from. If that directory's last component is "bin" or
645 * "lib", the parent directory is returned, otherwise the directory
646 * itself. If that DLL isn't loaded, the function proceeds as if
647 * @dll_name was %NULL.
649 * If both @package and @dll_name are %NULL, the directory from where
650 * the main executable of the process was loaded is uses instead in
651 * the same way as above.
653 * Returns: a string containing the installation directory for @package.
654 * The return value should be freed with g_free() when not needed any longer.
657 gchar *
658 g_win32_get_package_installation_directory (gchar *package,
659 gchar *dll_name)
661 static GHashTable *package_dirs = NULL;
662 G_LOCK_DEFINE_STATIC (package_dirs);
663 gchar *result = NULL;
664 gchar *key;
665 HKEY reg_key = NULL;
666 DWORD type;
667 DWORD nbytes;
669 if (package != NULL)
671 G_LOCK (package_dirs);
673 if (package_dirs == NULL)
674 package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
676 result = g_hash_table_lookup (package_dirs, package);
678 if (result && result[0])
680 G_UNLOCK (package_dirs);
681 return g_strdup (result);
684 key = g_strconcat ("Software\\", package, NULL);
686 nbytes = 0;
687 if ((RegOpenKeyEx (HKEY_CURRENT_USER, key, 0,
688 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
689 && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
690 &type, NULL, &nbytes) == ERROR_SUCCESS)
692 ((RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
693 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
694 && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
695 &type, NULL, &nbytes) == ERROR_SUCCESS)
696 && type == REG_SZ))
698 result = g_malloc (nbytes + 1);
699 RegQueryValueEx (reg_key, "InstallationDirectory", 0,
700 &type, result, &nbytes);
701 result[nbytes] = '\0';
704 if (reg_key != NULL)
705 RegCloseKey (reg_key);
707 g_free (key);
709 if (result)
711 g_hash_table_insert (package_dirs, package, result);
712 G_UNLOCK (package_dirs);
713 return g_strdup (result);
715 G_UNLOCK (package_dirs);
718 if (dll_name != NULL)
719 result = get_package_directory_from_module (dll_name);
721 if (result == NULL)
722 result = get_package_directory_from_module (NULL);
724 return result;
728 * g_win32_get_package_installation_subdirectory:
729 * @package: An identifier for a software package, or %NULL.
730 * @dll_name: The name of a DLL that a package provides, or %NULL.
731 * @subdir: A subdirectory of the package installation directory.
733 * Returns a newly-allocated string containing the path of the
734 * subdirectory @subdir in the return value from calling
735 * g_win32_get_package_installation_directory() with the @package and
736 * @dll_name parameters.
738 * Returns: a string containing the complete path to @subdir inside the
739 * installation directory of @package. The return value should be freed with
740 * g_free() when no longer needed.
743 gchar *
744 g_win32_get_package_installation_subdirectory (gchar *package,
745 gchar *dll_name,
746 gchar *subdir)
748 gchar *prefix;
750 prefix = g_win32_get_package_installation_directory (package, dll_name);
752 return g_build_filename (prefix, subdir, NULL);