Use FormatMessage to translate system error codes into textual messages.
[platform/upstream/glib.git] / gmodule / gmodule-win32.c
1 /* GMODULE - GLIB wrapper code for dynamic module loading
2  * Copyright (C) 1998 Tim Janik
3  * Copyright (C) 1998 Tor Lillqvist
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library 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  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library 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-1999.  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
30  */
31
32 #include <stdio.h>
33 #include <windows.h>
34
35 /* --- functions --- */
36 static gpointer
37 _g_module_open (const gchar    *file_name,
38                 gboolean        bind_lazy)
39 {
40   HINSTANCE handle;
41   
42   handle = LoadLibrary (file_name);
43   if (!handle)
44     {
45       char error[100];
46       FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError (),
47                      0, error, sizeof (error), NULL);
48       g_module_set_error (error);
49     }
50   
51   return handle;
52 }
53
54 static gpointer
55 _g_module_self (void)
56 {
57   HMODULE handle;
58   
59   handle = GetModuleHandle (NULL);
60   if (!handle)
61     {
62       char error[100];
63       FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError (),
64                      0, error, sizeof (error), NULL);
65       g_module_set_error (error);
66     }
67   
68   return handle;
69 }
70
71 static void
72 _g_module_close (gpointer         handle,
73                  gboolean         is_unref)
74 {
75   if (!FreeLibrary (handle))
76     {
77       char error[100];
78       FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError (),
79                      0, error, sizeof (error), NULL);
80       g_module_set_error (error);
81     }
82 }
83
84 static gpointer
85 _g_module_symbol (gpointer        handle,
86                   const gchar    *symbol_name)
87 {
88   gpointer p;
89   
90   p = GetProcAddress (handle, symbol_name);
91   if (!p)
92     {
93       char error[100];
94       FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError (),
95                      0, error, sizeof (error), NULL);
96       g_module_set_error (error);
97     }
98   return p;
99 }
100
101 static gchar*
102 _g_module_build_path (const gchar *directory,
103                       const gchar *module_name)
104 {
105   gint k;
106
107   k = strlen (module_name);
108   if (directory && *directory)
109     if (k > 4 && g_strcasecmp (module_name + k - 4, ".dll") == 0)
110       return g_strconcat (directory, "\\", module_name, NULL);
111     else
112       return g_strconcat (directory, "\\", module_name, ".dll", NULL);
113   else if (k > 4 && g_strcasecmp (module_name + k - 4, ".dll") == 0)
114     return g_strdup (module_name);
115   else
116     return g_strconcat (module_name, ".dll", NULL);
117 }