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