]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynlib.cpp
Additional makefiles; changes for compilation with BC++ and GnuWin32
[wxWidgets.git] / src / common / dynlib.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dynlib.cpp
3 // Purpose: Dynamic library management
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 20/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "dynlib.h"
14 #endif
15
16 #include <wx/dynlib.h>
17 #include <wx/filefn.h>
18 #include <wx/list.h>
19 #include <wx/string.h>
20
21 // ---------------------------------------------------------------------------
22 // System dependent include
23 // ---------------------------------------------------------------------------
24
25 #ifdef __UNIX__
26 #include <dlfcn.h>
27 #endif
28
29 #ifdef __WINDOWS__
30 #include <windows.h>
31 #endif
32
33 #ifdef LoadLibrary
34 #undef LoadLibrary
35 #endif
36
37 // ---------------------------------------------------------------------------
38 // Global variables
39 // ---------------------------------------------------------------------------
40
41 wxLibraries wxTheLibraries;
42
43 // ---------------------------------------------------------------------------
44 // wxLibrary (one instance per dynamic library
45 // ---------------------------------------------------------------------------
46
47 wxLibrary::wxLibrary(void *handle)
48 {
49 typedef wxClassInfo **(*t_get_first)(void);
50 t_get_first get_first;
51
52 m_handle = handle;
53 m_destroy = TRUE;
54
55 // Some system may use a local heap for library.
56 get_first = (t_get_first)GetSymbol("wxGetClassFirst");
57 // It is a wxWindows DLL.
58 if (get_first)
59 PrepareClasses(get_first());
60 }
61
62 wxLibrary::~wxLibrary()
63 {
64 if (m_handle && m_destroy) {
65 #ifdef __UNIX__
66 dlclose(m_handle);
67 #endif
68 #ifdef __WINDOWS__
69 FreeLibrary((HMODULE)m_handle);
70 #endif
71 }
72 }
73
74 wxObject *wxLibrary::CreateObject(const wxString& name)
75 {
76 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
77
78 if (!info)
79 return NULL;
80
81 return info->CreateObject();
82 }
83
84 void wxLibrary::PrepareClasses(wxClassInfo **first)
85 {
86 // Index all class infos by their class name
87 wxClassInfo *info = *first;
88 while (info)
89 {
90 if (info->className)
91 classTable.Put(info->className, (wxObject *)info);
92 info = info->next;
93 }
94
95 // Set base pointers for each wxClassInfo
96 info = *first;
97 while (info)
98 {
99 if (info->GetBaseClassName1())
100 info->baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
101 if (info->GetBaseClassName2())
102 info->baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
103 info = info->next;
104 }
105 *first = NULL;
106 }
107
108 void *wxLibrary::GetSymbol(const wxString& symbname)
109 {
110 #ifdef __UNIX__
111 return dlsym(m_handle, WXSTRINGCAST symbname);
112 #endif
113 #ifdef __WINDOWS__
114 return GetProcAddress(m_handle, WXSTRINGCAST symbname);
115 #endif
116 return NULL;
117 }
118
119 // ---------------------------------------------------------------------------
120 // wxLibraries (only one instance should normally exist)
121 // ---------------------------------------------------------------------------
122
123 wxLibraries::wxLibraries()
124 {
125 }
126
127 wxLibraries::~wxLibraries()
128 {
129 wxNode *node = m_loaded.First();
130
131 while (node) {
132 wxLibrary *lib = (wxLibrary *)node->Data();
133 delete lib;
134
135 node = node->Next();
136 }
137 }
138
139 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
140 {
141 wxString lib_name = name;
142 wxNode *node;
143 wxLibrary *lib;
144
145 if ( (node = m_loaded.Find(name.GetData())) )
146 return ((wxLibrary *)node->Data());
147
148 #if defined(__UNIX__)
149 lib_name.Prepend("./lib");
150 lib_name += ".so";
151
152 printf("lib_name = %s\n", WXSTRINGCAST lib_name);
153
154 void *handle = dlopen(WXSTRINGCAST lib_name, RTLD_LAZY);
155
156 if (!handle)
157 return NULL;
158 #elif defined(__WINDOWS__)
159 lib_name += ".dll";
160
161 #ifdef UNICODE
162 HMODULE handle = LoadLibraryW(lib_name);
163 #else
164 HMODULE handle = LoadLibraryA(lib_name);
165 #endif
166 if (!handle)
167 return NULL;
168 #else
169 return NULL;
170 #endif
171
172 lib = new wxLibrary((void *)handle);
173
174 m_loaded.Append(name.GetData(), lib);
175 return lib;
176 }
177
178 wxObject *wxLibraries::CreateObject(const wxString& path)
179 {
180 wxNode *node = m_loaded.First();
181 wxObject *obj;
182
183 while (node) {
184 obj = ((wxLibrary *)node->Data())->CreateObject(path);
185 if (obj)
186 return obj;
187
188 node = node->Next();
189 }
190 return NULL;
191 }