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