fc1c0ee6fccd098d2dc2b2288adaff918e12a7d1
[platform/upstream/glib.git] / glib / gwin32.c
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 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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
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/. 
26  */
27
28 /* 
29  * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include "glibconfig.h"
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <errno.h>
42
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 */
54
55 #include "glib.h"
56
57 #ifdef G_WITH_CYGWIN
58 #include <sys/cygwin.h>
59 #endif
60
61 #ifndef G_WITH_CYGWIN
62
63 gint
64 g_win32_ftruncate (gint  fd,
65                    guint size)
66 {
67   HANDLE hfile;
68   guint curpos;
69
70   g_return_val_if_fail (fd >= 0, -1);
71   
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))
77     {
78       gint error = GetLastError ();
79
80       switch (error)
81         {
82         case ERROR_INVALID_HANDLE:
83           errno = EBADF;
84           break;
85         default:
86           errno = EIO;
87           break;
88         }
89
90       return -1;
91     }
92
93   return 0;
94 }
95
96 DIR*
97 g_win32_opendir (const char *dirname)
98 {
99   DIR *result;
100   gchar *mask;
101   guint k;
102
103   g_return_val_if_fail (dirname != NULL, NULL);
104
105   result = g_new0 (DIR, 1);
106   result->find_file_data = g_new0 (WIN32_FIND_DATA, 1);
107   result->dir_name = g_strdup (dirname);
108   
109   k = strlen (result->dir_name);
110   if (k && result->dir_name[k - 1] == '\\')
111     {
112       result->dir_name[k - 1] = '\0';
113     }
114   mask = g_strdup_printf ("%s\\*", result->dir_name);
115
116   result->find_file_handle = (guint) FindFirstFile (mask,
117                                              (LPWIN32_FIND_DATA) result->find_file_data);
118   g_free (mask);
119
120   if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
121     {
122       int error = GetLastError ();
123
124       g_free (result->dir_name);
125       g_free (result->find_file_data);
126       g_free (result);
127       switch (error)
128         {
129         default:
130           errno = EIO;
131           return NULL;
132         }
133     }
134   result->just_opened = TRUE;
135
136   return result;
137 }
138
139 struct dirent*
140 g_win32_readdir (DIR *dir)
141 {
142   gchar *basename;
143
144   g_return_val_if_fail (dir != NULL, NULL);
145
146   if (dir->just_opened)
147     dir->just_opened = FALSE;
148   else
149     {
150       if (!FindNextFile ((HANDLE) dir->find_file_handle,
151                          (LPWIN32_FIND_DATA) dir->find_file_data))
152         {
153           int error = GetLastError ();
154
155           switch (error)
156             {
157             case ERROR_NO_MORE_FILES:
158               return NULL;
159             default:
160               errno = EIO;
161               return NULL;
162             }
163         }
164     }
165   
166   basename = g_path_get_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName);
167
168   strcpy (dir->readdir_result.d_name, basename);
169
170   g_free (basename);
171       
172   return &dir->readdir_result;
173 }
174
175 void
176 g_win32_rewinddir (DIR *dir)
177 {
178   gchar *mask;
179
180   g_return_if_fail (dir != NULL);
181
182   if (!FindClose ((HANDLE) dir->find_file_handle))
183     g_warning ("gwin_rewinddir(): FindClose() failed\n");
184
185   mask = g_strdup_printf ("%s\\*", dir->dir_name);
186   dir->find_file_handle = (guint) FindFirstFile (mask,
187                                           (LPWIN32_FIND_DATA) dir->find_file_data);
188   g_free (mask);
189
190   if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
191     {
192       int error = GetLastError ();
193
194       switch (error)
195         {
196         default:
197           errno = EIO;
198           return;
199         }
200     }
201   dir->just_opened = TRUE;
202 }  
203
204 gint
205 g_win32_closedir (DIR *dir)
206 {
207   g_return_val_if_fail (dir != NULL, -1);
208
209   if (!FindClose ((HANDLE) dir->find_file_handle))
210     {
211       int error = GetLastError ();
212
213       switch (error)
214         {
215         default:
216           errno = EIO; return -1;
217         }
218     }
219
220   g_free (dir->dir_name);
221   g_free (dir->find_file_data);
222   g_free (dir);
223
224   return 0;
225 }
226 #endif
227
228 /**
229  * g_win32_getlocale:
230  *
231  * The setlocale in the Microsoft C library uses locale names of the
232  * form "English_United States.1252" etc. We want the Unixish standard
233  * form "en", "zh_TW" etc. This function gets the current thread
234  * locale from Windows and returns it as a string of the above form
235  * for use in forming file names etc. The returned string should be
236  * deallocated with g_free().
237  *
238  * Returns: allocated locale name
239  */
240
241 gchar *
242 g_win32_getlocale (void)
243 {
244   LCID lcid;
245   gchar *ev;
246   gint primary, sub;
247   gchar *l = NULL, *sl = NULL;
248   gchar bfr[20];
249
250   if ((ev = getenv ("LC_ALL")) != NULL
251       || (ev = getenv ("LC_CTYPE")) != NULL
252       || (ev = getenv ("LANG")) != NULL)
253     return g_strdup (ev);
254
255   lcid = GetThreadLocale ();
256   primary = PRIMARYLANGID (LANGIDFROMLCID (lcid));
257   sub = SUBLANGID (LANGIDFROMLCID (lcid));
258   switch (primary)
259     {
260     case LANG_AFRIKAANS: l = "af"; break;
261     case LANG_ALBANIAN: l = "sq"; break;
262     case LANG_ARABIC:
263       l = "ar";
264       switch (sub)
265         {
266         case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
267         case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
268         case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
269         case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
270         case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
271         case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
272         case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
273         case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
274         case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
275         case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
276         case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
277         case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
278         case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
279         case SUBLANG_ARABIC_UAE: sl = "AE"; break;
280         case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
281         case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
282         }
283       break;
284     case LANG_ARMENIAN: l = "hy"; break;
285     case LANG_ASSAMESE: l = "as"; break;
286     case LANG_AZERI: l = "az"; break;
287     case LANG_BASQUE: l = "eu"; break;
288     case LANG_BELARUSIAN: l = "be"; break;
289     case LANG_BENGALI: l = "bn"; break;
290     case LANG_BULGARIAN: l = "bg"; break;
291     case LANG_CATALAN: l = "ca"; break;
292     case LANG_CHINESE:
293       l = "zh";
294       switch (sub)
295         {
296         case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
297         case SUBLANG_CHINESE_SIMPLIFIED: sl = "CH"; break;
298         case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
299         case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
300         case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
301         }
302       break;
303     case LANG_CROATIAN:         /* LANG_CROATIAN == LANG_SERBIAN
304                                  * What used to be called Serbo-Croatian
305                                  * should really now be two separate
306                                  * languages because of political reasons.
307                                  * (Says tml, who knows nothing about Serbian
308                                  * or Croatian.)
309                                  * (I can feel those flames coming already.)
310                                  */
311       switch (sub)
312         {
313         case SUBLANG_SERBIAN_LATIN: l = "sp"; break;
314         case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; break;
315         default: l = "hr";      /* ??? */
316         }
317       break;
318     case LANG_CZECH: l = "cs"; break;
319     case LANG_DANISH: l = "da"; break;
320     case LANG_DUTCH:
321       l = "nl";
322       switch (sub)
323         {
324         case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
325         }
326       break;
327     case LANG_ENGLISH:
328       l = "en";
329       switch (sub)
330         {
331         /* SUBLANG_ENGLISH_US == SUBLANG_DEFAULT. Heh. I thought
332          * English was the language spoken in England.
333          * Oh well.
334          */
335         case SUBLANG_ENGLISH_UK: sl = "GB"; break;
336         case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
337         case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
338         case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
339         case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
340         case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "ZA"; break;
341         case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
342         case SUBLANG_ENGLISH_CARIBBEAN: sl = "@caribbean"; break; /* ??? */
343         case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
344         case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
345         case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
346         case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
347         }
348       break;
349     case LANG_ESTONIAN: l = "et"; break;
350     case LANG_FAEROESE: l = "fo"; break;
351     case LANG_FARSI: l = "fa"; break;
352     case LANG_FINNISH: l = "fi"; break;
353     case LANG_FRENCH:
354       l = "fr";
355       switch (sub)
356         {
357         case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
358         case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
359         case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
360         case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
361         case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
362         }
363       break;
364     case LANG_GEORGIAN: l = "ka"; break;
365     case LANG_GERMAN:
366       l = "de";
367       switch (sub)
368         {
369         case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
370         case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
371         case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
372         case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
373         }
374       break;
375     case LANG_GREEK: l = "el"; break;
376     case LANG_GUJARATI: l = "gu"; break;
377     case LANG_HEBREW: l = "he"; break;
378     case LANG_HINDI: l = "hi"; break;
379     case LANG_HUNGARIAN: l = "hu"; break;
380     case LANG_ICELANDIC: l = "is"; break;
381     case LANG_INDONESIAN: l = "id"; break;
382     case LANG_ITALIAN:
383       l = "it";
384       switch (sub)
385         {
386         case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
387         }
388       break;
389     case LANG_JAPANESE: l = "ja"; break;
390     case LANG_KANNADA: l = "kn"; break;
391     case LANG_KASHMIRI:
392       l = "ks";
393       switch (sub)
394         {
395         case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
396         }
397       break;
398     case LANG_KAZAK: l = "kk"; break;
399     case LANG_KONKANI: l = "kok"; break; /* ??? */
400     case LANG_KOREAN: l = "ko"; break;
401     case LANG_LATVIAN: l = "lv"; break;
402     case LANG_LITHUANIAN: l = "lt"; break;
403     case LANG_MACEDONIAN: l = "mk"; break;
404     case LANG_MALAY:
405       l = "ms";
406       switch (sub)
407         {
408         case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
409         }
410       break;
411     case LANG_MALAYALAM: l = "ml"; break;
412     case LANG_MANIPURI: l = "mni"; break;
413     case LANG_MARATHI: l = "mr"; break;
414     case LANG_NEPALI:
415       l = "ne";
416       switch (sub)
417         {
418         case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
419         }
420       break;
421     case LANG_NORWEGIAN:
422       l = "no";
423       switch (sub)
424         {
425         /* SUBLANG_NORWEGIAN_BOKMAL == SUBLANG_DEFAULT */
426         case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; break;
427         }
428       break;
429     case LANG_ORIYA: l = "or"; break;
430     case LANG_POLISH: l = "pl"; break;
431     case LANG_PORTUGUESE:
432       l = "pt";
433       switch (sub)
434         {
435         /* Hmm. SUBLANG_PORTUGUESE_BRAZILIAN == SUBLANG_DEFAULT */
436         case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
437         }
438       break;
439     case LANG_PUNJABI: l = "pa"; break;
440     case LANG_ROMANIAN: l = "ro"; break;
441     case LANG_RUSSIAN: l = "ru"; break;
442     case LANG_SANSKRIT: l = "sa"; break;
443     case LANG_SINDHI: l = "sd"; break;
444     case LANG_SLOVAK: l = "sk"; break;
445     case LANG_SLOVENIAN: l = "sl"; break;
446     case LANG_SPANISH:
447       l = "es";
448       switch (sub)
449         {
450         case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
451         case SUBLANG_SPANISH_MODERN: sl = "@modern"; break;     /* ??? */
452         case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
453         case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
454         case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
455         case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
456         case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
457         case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
458         case SUBLANG_SPANISH_PERU: sl = "PE"; break;
459         case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
460         case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
461         case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
462         case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
463         case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
464         case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
465         case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
466         case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
467         case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
468         case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
469         }
470       break;
471     case LANG_SWAHILI: l = "sw"; break;
472     case LANG_SWEDISH:
473       l = "sv";
474       switch (sub)
475         {
476         case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
477         }
478       break;
479     case LANG_TAMIL: l = "ta"; break;
480     case LANG_TATAR: l = "tt"; break;
481     case LANG_TELUGU: l = "te"; break;
482     case LANG_THAI: l = "th"; break;
483     case LANG_TURKISH: l = "tr"; break;
484     case LANG_UKRAINIAN: l = "uk"; break;
485     case LANG_URDU:
486       l = "ur";
487       switch (sub)
488         {
489         case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
490         case SUBLANG_URDU_INDIA: sl = "IN"; break;
491         }
492       break;
493     case LANG_UZBEK:
494       l = "uz";
495       switch (sub)
496         {
497         case SUBLANG_UZBEK_CYRILLIC: sl = "@cyrillic"; break;
498         }
499       break;
500     case LANG_VIETNAMESE: l = "vi"; break;
501     default: l = "xx"; break;
502     }
503   strcpy (bfr, l);
504   if (sl != NULL)
505     {
506       if (sl[0] != '@')
507         strcat (bfr, "_");
508       strcat (bfr, sl);
509     }
510
511   return g_strdup (bfr);
512 }
513
514 /**
515  * g_win32_error_message:
516  * @error: error code
517  *
518  * Translate a Win32 error code (as returned by GetLastError()) into
519  * the corresponding message. The message is either language neutral,
520  * or in the thread's language, or the user's language, the system's
521  * langauge, or US English (see docs for FormatMessage). The returned
522  * string should be deallocated with g_free().
523  *
524  * Returns: allocated error message
525  */
526 gchar *
527 g_win32_error_message (gint error)
528 {
529   gchar *msg;
530   gchar *retval;
531   int nbytes;
532
533   FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
534                  |FORMAT_MESSAGE_IGNORE_INSERTS
535                  |FORMAT_MESSAGE_FROM_SYSTEM,
536                  NULL, error, 0,
537                  (LPTSTR) &msg, 0, NULL);
538   nbytes = strlen (msg);
539
540   if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
541     msg[nbytes-2] = '\0';
542   
543   retval = g_strdup (msg);
544
545   if (msg != NULL)
546     LocalFree (msg);
547
548   return retval;
549 }
550
551 static gchar *
552 get_package_directory_from_module (gchar *module_name)
553 {
554   static GHashTable *module_dirs = NULL;
555   HMODULE hmodule = NULL;
556   gchar *fn;
557   gchar *p;
558   gchar *result;
559
560   if (module_dirs == NULL)
561     module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
562   
563   result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
564       
565   if (result)
566     return g_strdup (result);
567
568   if (module_name)
569     {
570       hmodule = GetModuleHandle (module_name);
571       if (!hmodule)
572         return NULL;
573     }
574
575   fn = g_malloc (MAX_PATH);
576   if (!GetModuleFileName (hmodule, fn, MAX_PATH))
577     return NULL;
578
579 #ifdef G_WITH_CYGWIN
580   /* In Cygwin we need to have POSIX paths */
581   {
582     gchar tmp[MAX_PATH];
583
584     cygwin_conv_to_posix_path(fn, tmp);
585     g_free(fn);
586     fn = g_strdup(tmp);
587   }
588 #endif
589
590   if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
591     *p = '\0';
592
593   p = strrchr (fn, G_DIR_SEPARATOR);
594   if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
595             g_ascii_strcasecmp (p + 1, "lib") == 0))
596     *p = '\0';
597
598   g_hash_table_insert (module_dirs, module_name ? module_name : "", fn);
599
600   return g_strdup (fn);
601 }
602
603 /**
604  * g_win32_get_package_installation_directory:
605  * @package: An identifier for a software package, or NULL
606  * @dll_name: The name of a DLL that a package provides, or NULL
607  *
608  * Try to determine the installation directory for a software package.
609  * Typically used by GNU software packages.
610  *
611  * @package should be a short identifier for the package. Typically it
612  * is the same identifier as used for GETTEXT_PACKAGE in software
613  * configured accoring to GNU standards. The function first looks in
614  * the Windows Registry for the value #InstallationDirectory in the
615  * key #HKLM\Software\@package, and if that value exists and is a
616  * string, returns that.
617  *
618  * If @package is NULL, or the above value isn't found in the
619  * Registry, but @dll_name is non-NULL, it should name a DLL loaded
620  * into the current process. Typically that would be the name of the
621  * DLL calling this function, looking for its installation
622  * directory. The function then asks Windows what directory that DLL
623  * was loaded from. If that directory's last component is "bin" or
624  * "lib", the parent directory is returned, otherwise the directory
625  * itself. If that DLL isn't loaded, the function proceeds as if
626  * @dll_name was NULL.
627  *
628  * If both @package and @dll_name are NULL, the directory from where
629  * the main executable of the process was loaded is uses instead in
630  * the same way as above.
631  *
632  * The return value should be freed with g_free() when not needed any longer.  */
633
634 gchar *
635 g_win32_get_package_installation_directory (gchar *package,
636                                             gchar *dll_name)
637 {
638   static GHashTable *package_dirs = NULL;
639   gchar *result = NULL;
640   gchar *key;
641   HKEY reg_key = NULL;
642   DWORD type;
643   DWORD nbytes;
644
645   if (package != NULL)
646     {
647       if (package_dirs == NULL)
648         package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
649       
650       result = g_hash_table_lookup (package_dirs, package);
651       
652       if (result && result[0])
653         return g_strdup (result);
654       
655       key = g_strconcat ("Software\\", package, NULL);
656       
657       nbytes = 0;
658       if ((RegOpenKeyEx (HKEY_CURRENT_USER, key, 0,
659                          KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
660            && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
661                                &type, NULL, &nbytes) == ERROR_SUCCESS)
662           ||
663           ((RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
664                          KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
665            && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
666                                &type, NULL, &nbytes) == ERROR_SUCCESS)
667            && type == REG_SZ))
668         {
669           result = g_malloc (nbytes + 1);
670           RegQueryValueEx (reg_key, "InstallationDirectory", 0,
671                            &type, result, &nbytes);
672           result[nbytes] = '\0';
673         }
674
675       if (reg_key != NULL)
676         RegCloseKey (reg_key);
677       
678       g_free (key);
679       
680     }
681   if (result)
682     {
683       g_hash_table_insert (package_dirs, package, result);
684       return g_strdup (result);
685     }
686
687   if (dll_name != NULL)
688     result = get_package_directory_from_module (dll_name);
689
690   if (result == NULL)
691     result = get_package_directory_from_module (NULL);
692
693   return result;
694 }
695
696 /**
697  * g_win32_get_package_installation_subdirectory:
698  * @package: An identifier for a software package, or NULL
699  * @dll_name: The name of a DLL that a package provides, or NULL
700  * @subdir: A subdirectory of the package installation directory.
701  *
702  * Returns a string containg the path of the subdirectory @subdir in
703  * the return value from calling
704  * g_win32_get_package_installation_directory() with the @package and
705  * @dll_name parameters. The return value should be freed with
706  * g_free() when no longer needed.
707  */
708
709 gchar *
710 g_win32_get_package_installation_subdirectory (gchar *package,
711                                                gchar *dll_name,
712                                                gchar *subdir)
713 {
714   gchar *prefix;
715   gchar *sep;
716
717   prefix = g_win32_get_package_installation_directory (package, dll_name);
718
719   sep = (prefix[strlen (prefix) - 1] == G_DIR_SEPARATOR ?
720          "" : G_DIR_SEPARATOR_S);
721
722   return g_strconcat (prefix, sep, subdir, NULL);
723 }