*** empty log message ***
[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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 # pragma implementation "dynlib.h"
22 #endif
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29 #if wxUSE_DYNLIB_CLASS
30
31 #include "wx/dynlib.h"
32 #include "wx/filefn.h"
33 #include "wx/intl.h"
34 #include "wx/log.h"
35 #include "wx/tokenzr.h"
36
37 // ----------------------------------------------------------------------------
38 // conditional compilation
39 // ----------------------------------------------------------------------------
40
41 #if defined(HAVE_DLOPEN)
42 # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_NOW/*RTLD_LAZY*/)
43 # define wxDllGetSymbol(handle, name) dlsym(handle, name.mb_str())
44 # define wxDllClose dlclose
45 #elif defined(HAVE_SHL_LOAD)
46 # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
47 # define wxDllClose shl_unload
48
49 static inline void *wxDllGetSymbol(shl_t handle, const wxString& name)
50 {
51 void *sym;
52 if ( shl_findsym(&handle, name.mb_str(), TYPE_UNDEFINED, &sym) == 0 )
53 return sym;
54 else
55 return (void *)0;
56 }
57 #elif defined(__WINDOWS__)
58 # include <windows.h>
59
60 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
61 # ifdef __WIN32__
62 # define wxDllOpen(lib) ::LoadLibraryEx(lib, 0, 0)
63 # else // Win16
64 # define wxDllOpen(lib) ::LoadLibrary(lib)
65 # endif // Win32/16
66 # define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
67 # define wxDllClose ::FreeLibrary
68
69 #elif defined(__OS2__)
70
71 # define INCL_DOS
72 # include <os2.h>
73 # define wxDllOpen(error, lib, handle) DosLoadModule(error, sizeof(error), lib, &handle)
74 # define wxDllGetSymbol(handle, modaddr) DosQueryProcAddr(handle, 1L, NULL, (PFN*)modaddr)
75 # define wxDllClose(handle) DosFreeModule(handle)
76 #else
77 # error "Don't know how to load shared libraries on this platform."
78 #endif // OS
79
80 // ---------------------------------------------------------------------------
81 // Global variables
82 // ---------------------------------------------------------------------------
83
84 wxLibraries wxTheLibraries;
85
86 // ----------------------------------------------------------------------------
87 // private functions
88 // ----------------------------------------------------------------------------
89
90 // construct the full name from the base shared object name: adds a .dll
91 // suffix under Windows or .so under Unix
92 static wxString ConstructLibraryName(const wxString& basename)
93 {
94 wxString fullname(basename);
95
96 #if defined(__UNIX__)
97 # if defined(__HPUX__)
98 fullname << ".sl";
99 # else //__HPUX__
100 fullname << ".so";
101 # endif //__HPUX__
102 #elif defined(__WINDOWS__) || defined(__OS2__)
103 fullname << ".dll";
104 #endif
105
106 return fullname;
107 }
108
109 // ============================================================================
110 // implementation
111 // ============================================================================
112
113
114 // ---------------------------------------------------------------------------
115 // wxLibrary (one instance per dynamic library)
116 // ---------------------------------------------------------------------------
117
118 wxLibrary::wxLibrary(wxDllType handle)
119 {
120 typedef wxClassInfo *(*t_get_first)(void);
121 t_get_first get_first;
122
123 m_handle = handle;
124
125 // Some system may use a local heap for library.
126 get_first = (t_get_first)GetSymbol("wxGetClassFirst");
127 // It is a wxWindows DLL.
128 if (get_first)
129 PrepareClasses(get_first());
130 }
131
132 wxLibrary::~wxLibrary()
133 {
134 if ( m_handle )
135 {
136 wxDllClose(m_handle);
137 }
138 }
139
140 wxObject *wxLibrary::CreateObject(const wxString& name)
141 {
142 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
143
144 if (!info)
145 return NULL;
146
147 return info->CreateObject();
148 }
149
150 void wxLibrary::PrepareClasses(wxClassInfo *first)
151 {
152 // Index all class infos by their class name
153 wxClassInfo *info = first;
154 while (info)
155 {
156 if (info->m_className)
157 classTable.Put(info->m_className, (wxObject *)info);
158 info = info->GetNext();
159 }
160
161 // Set base pointers for each wxClassInfo
162 info = first;
163 while (info)
164 {
165 if (info->GetBaseClassName1())
166 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
167 if (info->GetBaseClassName2())
168 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
169 info = info->m_next;
170 }
171 }
172
173 void *wxLibrary::GetSymbol(const wxString& symbname)
174 {
175 return wxDllLoader::GetSymbol(m_handle, symbname);
176 }
177
178 // ---------------------------------------------------------------------------
179 // wxDllLoader
180 // ---------------------------------------------------------------------------
181
182 /* static */
183 wxDllType
184 wxDllLoader::GetProgramHandle(void)
185 {
186 #ifdef __UNIX__
187 return dlopen(NULL, RTLD_NOW/*RTLD_LAZY*/);
188 #else
189 wxFAIL_MSG(_("This method is not implemented under Windows or OS/2"));
190
191 return 0;
192 #endif
193 }
194
195 /* static */
196 wxDllType
197 wxDllLoader::LoadLibrary(const wxString & libname, bool *success)
198 {
199 wxDllType handle;
200
201 #if defined(__WXMAC__)
202 FSSpec myFSSpec ;
203 Ptr myMainAddr ;
204 Str255 myErrName ;
205
206 wxMacPathToFSSpec( libname , &myFSSpec ) ;
207 if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr ,
208 myErrName ) != noErr )
209 {
210 p2cstr( myErrName ) ;
211 wxASSERT_MSG( 1 , (char*)myErrName ) ;
212 return NULL ;
213 }
214 #elif defined(__OS2__)
215 char zError[256] = "";
216 wxDllOpen(zError, libname, handle);
217 #else // !Mac
218 handle = wxDllOpen(libname);
219 #endif // OS
220
221 if ( !handle )
222 {
223 wxLogSysError(_("Failed to load shared library '%s'"), libname.c_str());
224 }
225
226 if ( success )
227 {
228 *success = handle != 0;
229 }
230
231 return handle;
232 }
233
234
235 /* static */
236 void
237 wxDllLoader::UnloadLibrary(wxDllType handle)
238 {
239 wxDllClose(handle);
240 }
241
242 /* static */
243 void *
244 wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name)
245 {
246 void *symbol = NULL; // return value
247
248 #if defined( __WXMAC__ )
249 Ptr symAddress ;
250 CFragSymbolClass symClass ;
251 Str255 symName ;
252
253 strcpy( (char*) symName , name ) ;
254 c2pstr( (char*) symName ) ;
255
256 if ( FindSymbol( dllHandle , symName , &symAddress , &symClass ) == noErr )
257 symbol = (void *)symAddress ;
258 #elif defined( __OS2__ )
259 wxDllGetSymbol(dllHandle, symbol);
260 #else
261 symbol = wxDllGetSymbol(dllHandle, name);
262 #endif
263
264 if ( !symbol )
265 {
266 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
267 name.c_str());
268 }
269 return symbol;
270 }
271
272 // ---------------------------------------------------------------------------
273 // wxLibraries (only one instance should normally exist)
274 // ---------------------------------------------------------------------------
275
276 wxLibraries::wxLibraries()
277 {
278 }
279
280 wxLibraries::~wxLibraries()
281 {
282 wxNode *node = m_loaded.First();
283
284 while (node) {
285 wxLibrary *lib = (wxLibrary *)node->Data();
286 delete lib;
287
288 node = node->Next();
289 }
290 }
291
292 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
293 {
294 wxNode *node;
295 wxLibrary *lib;
296 wxClassInfo *old_sm_first;
297
298 #if defined(__VISAGECPP__)
299 node = m_loaded.Find(name.GetData());
300 if (node != NULL)
301 return ((wxLibrary *)node->Data());
302 #else // !OS/2
303 if ( (node = m_loaded.Find(name.GetData())) )
304 return ((wxLibrary *)node->Data());
305 #endif
306 // If DLL shares data, this is necessary.
307 old_sm_first = wxClassInfo::sm_first;
308 wxClassInfo::sm_first = NULL;
309
310 wxString libname = ConstructLibraryName(name);
311
312 /*
313 Unix automatically builds that library name, at least for dlopen()
314 */
315 #if 0
316 #if defined(__UNIX__)
317 // found the first file in LD_LIBRARY_PATH with this name
318 wxString libPath("/lib:/usr/lib"); // system path first
319 const char *envLibPath = getenv("LD_LIBRARY_PATH");
320 if ( envLibPath )
321 libPath << ':' << envLibPath;
322 wxStringTokenizer tokenizer(libPath, _T(':'));
323 while ( tokenizer.HasMoreToken() )
324 {
325 wxString fullname(tokenizer.NextToken());
326
327 fullname << '/' << libname;
328 if ( wxFileExists(fullname) )
329 {
330 libname = fullname;
331
332 // found the library
333 break;
334 }
335 }
336 //else: not found in the path, leave the name as is (secutiry risk?)
337
338 #endif // __UNIX__
339 #endif
340
341 bool success = FALSE;
342 wxDllType handle = wxDllLoader::LoadLibrary(libname, &success);
343 if(success)
344 {
345 lib = new wxLibrary(handle);
346 wxClassInfo::sm_first = old_sm_first;
347 m_loaded.Append(name.GetData(), lib);
348 }
349 else
350 lib = NULL;
351 return lib;
352 }
353
354 wxObject *wxLibraries::CreateObject(const wxString& path)
355 {
356 wxNode *node = m_loaded.First();
357 wxObject *obj;
358
359 while (node) {
360 obj = ((wxLibrary *)node->Data())->CreateObject(path);
361 if (obj)
362 return obj;
363
364 node = node->Next();
365 }
366 return NULL;
367 }
368
369 #endif // wxUSE_DYNLIB_CLASS