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