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