Merge in current Win32 version. Almost no Unix code touched.
[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  * MT safe
23  */
24
25 #include <stdio.h>
26 #include <windows.h>
27
28 /* --- functions --- */
29 static gpointer
30 _g_module_open (const gchar    *file_name,
31                 gboolean        bind_lazy)
32 {
33   HINSTANCE handle;
34   
35   handle = LoadLibrary (file_name);
36   if (!handle)
37     {
38       char error[100];
39       sprintf (error, "Error code %d", GetLastError ());
40       g_module_set_error (error);
41     }
42   
43   return handle;
44 }
45
46 static gpointer
47 _g_module_self (void)
48 {
49   HMODULE handle;
50   
51   handle = GetModuleHandle (NULL);
52   if (!handle)
53     {
54       char error[100];
55       sprintf (error, "Error code %d", GetLastError ());
56       g_module_set_error (error);
57     }
58   
59   return handle;
60 }
61
62 static void
63 _g_module_close (gpointer         handle,
64                  gboolean         is_unref)
65 {
66   if (!FreeLibrary (handle))
67     {
68       char error[100];
69       sprintf (error, "Error code %d", GetLastError ());
70       g_module_set_error (error);
71     }
72 }
73
74 static gpointer
75 _g_module_symbol (gpointer        handle,
76                   const gchar    *symbol_name)
77 {
78   gpointer p;
79   
80   p = GetProcAddress (handle, symbol_name);
81   if (!p)
82     {
83       char error[100];
84       sprintf (error, "Error code %d", GetLastError ());
85       g_module_set_error (error);
86     }
87   return p;
88 }
89
90 static gchar*
91 _g_module_build_path (const gchar *directory,
92                       const gchar *module_name)
93 {
94   gint k;
95
96   k = strlen (module_name);
97   if (directory && *directory)
98     if (k > 4 && g_strcasecmp (module_name + k - 4, ".dll") == 0)
99       return g_strconcat (directory, "\\", module_name, NULL);
100     else
101       return g_strconcat (directory, "\\", module_name, ".dll", NULL);
102   else if (k > 4 && g_strcasecmp (module_name + k - 4, ".dll") == 0)
103     return g_strdup (module_name);
104   else
105     return g_strconcat (module_name, ".dll", NULL);
106 }