]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
1. a couple of compilation warnings fixed (strconv/listctrl)
[wxWidgets.git] / src / common / dynlib.cpp
CommitLineData
7a4b9130
GL
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
7b0bfbb2
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
7a4b9130 20#ifdef __GNUG__
a585ca5c 21# pragma implementation "dynlib.h"
7a4b9130
GL
22#endif
23
0c32066b
JS
24#include "wx/wxprec.h"
25
2df7be7f
RR
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
f6bcfd97 29
8a0d4cf6
VZ
30#if wxUSE_DYNLIB_CLASS
31
f6bcfd97
BP
32#if defined(__WINDOWS__)
33 #include "wx/msw/private.h"
34#endif
35
7b0bfbb2
VZ
36#include "wx/dynlib.h"
37#include "wx/filefn.h"
38#include "wx/intl.h"
39#include "wx/log.h"
8a0d4cf6 40#include "wx/tokenzr.h"
7b0bfbb2
VZ
41
42// ----------------------------------------------------------------------------
43// conditional compilation
44// ----------------------------------------------------------------------------
45
1a787c5d
SN
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)
f6bcfd97
BP
53# define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_NOW/*RTLD_LAZY*/)
54# define wxDllGetSymbol(handle, name) dlsym(handle, name)
a585ca5c 55# define wxDllClose dlclose
8a0d4cf6 56#elif defined(HAVE_SHL_LOAD)
a585ca5c
KB
57# define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
58# define wxDllClose shl_unload
846e1424 59
50920146 60 static inline void *wxDllGetSymbol(shl_t handle, const wxString& name)
7b0bfbb2
VZ
61 {
62 void *sym;
50920146 63 if ( shl_findsym(&handle, name.mb_str(), TYPE_UNDEFINED, &sym) == 0 )
7b0bfbb2
VZ
64 return sym;
65 else
66 return (void *)0;
67 }
68#elif defined(__WINDOWS__)
90022ddc 69 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
a585ca5c 70# ifdef __WIN32__
f6bcfd97
BP
71#ifdef _UNICODE
72# define wxDllOpen(lib) ::LoadLibraryExW(lib, 0, 0)
73#else
74# define wxDllOpen(lib) ::LoadLibraryExA(lib, 0, 0)
75#endif
a585ca5c
KB
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
7b0bfbb2 81#else
a585ca5c 82# error "Don't know how to load shared libraries on this platform."
7b0bfbb2 83#endif // OS
7a4b9130
GL
84
85// ---------------------------------------------------------------------------
7b0bfbb2 86// Global variables
7a4b9130
GL
87// ---------------------------------------------------------------------------
88
7b0bfbb2 89wxLibraries wxTheLibraries;
7a4b9130 90
f6bcfd97
BP
91// ============================================================================
92// implementation
93// ============================================================================
123a7fdd 94
7b0bfbb2
VZ
95// construct the full name from the base shared object name: adds a .dll
96// suffix under Windows or .so under Unix
97static wxString ConstructLibraryName(const wxString& basename)
98{
f6bcfd97
BP
99 wxString fullname;
100 fullname << basename << wxDllLoader::GetDllExt();
27529614 101
7b0bfbb2
VZ
102 return fullname;
103}
7a4b9130 104
7a4b9130 105// ---------------------------------------------------------------------------
7b0bfbb2 106// wxLibrary (one instance per dynamic library)
7a4b9130
GL
107// ---------------------------------------------------------------------------
108
7b0bfbb2 109wxLibrary::wxLibrary(wxDllType handle)
7a4b9130 110{
7b0bfbb2
VZ
111 typedef wxClassInfo *(*t_get_first)(void);
112 t_get_first get_first;
7a4b9130 113
7b0bfbb2 114 m_handle = handle;
7a4b9130 115
7b0bfbb2
VZ
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());
7a4b9130
GL
121}
122
123wxLibrary::~wxLibrary()
124{
7b0bfbb2
VZ
125 if ( m_handle )
126 {
127 wxDllClose(m_handle);
128 }
7a4b9130
GL
129}
130
131wxObject *wxLibrary::CreateObject(const wxString& name)
132{
7b0bfbb2 133 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
f4a8c29f 134
7b0bfbb2
VZ
135 if (!info)
136 return NULL;
f4a8c29f 137
7b0bfbb2 138 return info->CreateObject();
f4a8c29f
GL
139}
140
856d2e52 141void wxLibrary::PrepareClasses(wxClassInfo *first)
f4a8c29f 142{
7b0bfbb2
VZ
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 }
7a4b9130
GL
162}
163
164void *wxLibrary::GetSymbol(const wxString& symbname)
165{
a585ca5c
KB
166 return wxDllLoader::GetSymbol(m_handle, symbname);
167}
7b0bfbb2 168
a585ca5c
KB
169// ---------------------------------------------------------------------------
170// wxDllLoader
171// ---------------------------------------------------------------------------
7b0bfbb2 172
f6bcfd97
BP
173/* static */
174wxString 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
0868079c
KB
191/* static */
192wxDllType
193wxDllLoader::GetProgramHandle(void)
194{
1a787c5d 195#if defined( HAVE_DLOPEN ) && !defined(__EMX__)
7742efff
KB
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;
0868079c 201#else
58c837a4 202 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
7742efff 203 return 0;
7cc98b3e 204#endif
0868079c
KB
205}
206
a585ca5c
KB
207/* static */
208wxDllType
7cc98b3e 209wxDllLoader::LoadLibrary(const wxString & libname, bool *success)
a585ca5c 210{
7cc98b3e 211 wxDllType handle;
7b0bfbb2 212
a585ca5c 213#if defined(__WXMAC__)
7cc98b3e 214 FSSpec myFSSpec ;
f6bcfd97
BP
215 Ptr myMainAddr ;
216 Str255 myErrName ;
7cc98b3e
VZ
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 }
1a787c5d 226#elif defined(__WXPM__) || defined(__EMX__)
f6bcfd97 227 char zError[256] = "";
c2ff79b1 228 wxDllOpen(zError, libname, handle);
a585ca5c 229#else // !Mac
7cc98b3e 230 handle = wxDllOpen(libname);
a585ca5c
KB
231#endif // OS
232
7cc98b3e
VZ
233 if ( !handle )
234 {
f6bcfd97
BP
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 }
7cc98b3e
VZ
262 }
263
264 if ( success )
265 {
266 *success = handle != 0;
267 }
268
269 return handle;
a585ca5c
KB
270}
271
752c7d6b
KB
272
273/* static */
274void
275wxDllLoader::UnloadLibrary(wxDllType handle)
276{
277 wxDllClose(handle);
278}
279
a585ca5c
KB
280/* static */
281void *
282wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name)
283{
7cc98b3e 284 void *symbol = NULL; // return value
a585ca5c
KB
285
286#if defined( __WXMAC__ )
7cc98b3e
VZ
287 Ptr symAddress ;
288 CFragSymbolClass symClass ;
f6bcfd97 289 Str255 symName ;
7cc98b3e
VZ
290
291 strcpy( (char*) symName , name ) ;
292 c2pstr( (char*) symName ) ;
293
294 if ( FindSymbol( dllHandle , symName , &symAddress , &symClass ) == noErr )
295 symbol = (void *)symAddress ;
1a787c5d 296#elif defined( __WXPM__ ) || defined(__EMX__)
c2ff79b1 297 wxDllGetSymbol(dllHandle, symbol);
7b0bfbb2 298#else
f6bcfd97
BP
299 // mb_str() is necessary in Unicode build
300 symbol = wxDllGetSymbol(dllHandle, name.mb_str());
123a7fdd 301#endif
7b0bfbb2
VZ
302
303 if ( !symbol )
304 {
7cc98b3e
VZ
305 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
306 name.c_str());
7b0bfbb2 307 }
7b0bfbb2 308 return symbol;
7a4b9130
GL
309}
310
311// ---------------------------------------------------------------------------
312// wxLibraries (only one instance should normally exist)
313// ---------------------------------------------------------------------------
314
65fd5cb0 315wxLibraries::wxLibraries():m_loaded(wxKEY_STRING)
7a4b9130
GL
316{
317}
318
319wxLibraries::~wxLibraries()
320{
7b0bfbb2 321 wxNode *node = m_loaded.First();
7a4b9130 322
7b0bfbb2
VZ
323 while (node) {
324 wxLibrary *lib = (wxLibrary *)node->Data();
325 delete lib;
7a4b9130 326
7b0bfbb2
VZ
327 node = node->Next();
328 }
7a4b9130
GL
329}
330
331wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
332{
7b0bfbb2
VZ
333 wxNode *node;
334 wxLibrary *lib;
335 wxClassInfo *old_sm_first;
7a4b9130 336
c2ff79b1
DW
337#if defined(__VISAGECPP__)
338 node = m_loaded.Find(name.GetData());
339 if (node != NULL)
340 return ((wxLibrary *)node->Data());
341#else // !OS/2
7b0bfbb2
VZ
342 if ( (node = m_loaded.Find(name.GetData())) )
343 return ((wxLibrary *)node->Data());
c2ff79b1 344#endif
7b0bfbb2
VZ
345 // If DLL shares data, this is necessary.
346 old_sm_first = wxClassInfo::sm_first;
347 wxClassInfo::sm_first = NULL;
348
7cc98b3e 349 wxString libname = ConstructLibraryName(name);
856d2e52 350
a585ca5c
KB
351/*
352 Unix automatically builds that library name, at least for dlopen()
353*/
354#if 0
9e4b2f1c 355#if defined(__UNIX__)
8a0d4cf6
VZ
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 )
58c837a4 360 libPath << wxT(':') << envLibPath;
223d09f6 361 wxStringTokenizer tokenizer(libPath, wxT(':'));
8a0d4cf6
VZ
362 while ( tokenizer.HasMoreToken() )
363 {
364 wxString fullname(tokenizer.NextToken());
365
58c837a4 366 fullname << wxT('/') << libname;
8a0d4cf6
VZ
367 if ( wxFileExists(fullname) )
368 {
7cc98b3e 369 libname = fullname;
8a0d4cf6
VZ
370
371 // found the library
372 break;
373 }
374 }
375 //else: not found in the path, leave the name as is (secutiry risk?)
376
7b0bfbb2 377#endif // __UNIX__
a585ca5c 378#endif
7a4b9130 379
4dc2c3bb 380 bool success = FALSE;
7cc98b3e 381 wxDllType handle = wxDllLoader::LoadLibrary(libname, &success);
a585ca5c 382 if(success)
7b0bfbb2 383 {
a585ca5c
KB
384 lib = new wxLibrary(handle);
385 wxClassInfo::sm_first = old_sm_first;
386 m_loaded.Append(name.GetData(), lib);
7b0bfbb2 387 }
a585ca5c
KB
388 else
389 lib = NULL;
7b0bfbb2 390 return lib;
7a4b9130
GL
391}
392
393wxObject *wxLibraries::CreateObject(const wxString& path)
394{
7b0bfbb2
VZ
395 wxNode *node = m_loaded.First();
396 wxObject *obj;
7a4b9130 397
7b0bfbb2
VZ
398 while (node) {
399 obj = ((wxLibrary *)node->Data())->CreateObject(path);
400 if (obj)
401 return obj;
7a4b9130 402
7b0bfbb2
VZ
403 node = node->Next();
404 }
405 return NULL;
7a4b9130 406}
8a0d4cf6
VZ
407
408#endif // wxUSE_DYNLIB_CLASS