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