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