]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
Applied patch [ 602924 ] colours and effects in wxMSW::wxFontDialog
[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
481fcc78 30#if wxUSE_DYNLIB_CLASS && !wxUSE_DYNAMIC_LOADER
8a0d4cf6 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"
40
76a5e5d2
SC
41#if defined(__WXMAC__)
42 #include "wx/mac/private.h"
43#endif
44
7b0bfbb2
VZ
45// ----------------------------------------------------------------------------
46// conditional compilation
47// ----------------------------------------------------------------------------
48
1a787c5d
SN
49#if defined(__WXPM__) || defined(__EMX__)
50# define INCL_DOS
51# include <os2.h>
52# define wxDllOpen(error, lib, handle) DosLoadModule(error, sizeof(error), lib, &handle)
53# define wxDllGetSymbol(handle, modaddr) DosQueryProcAddr(handle, 1L, NULL, (PFN*)modaddr)
54# define wxDllClose(handle) DosFreeModule(handle)
55#elif defined(HAVE_DLOPEN)
237c5c02
VZ
56 // note about dlopen() flags: we use RTLD_NOW to have more Windows-like
57 // behaviour (Win won't let you load a library with missing symbols) and
58 // RTLD_GLOBAL because it is needed sometimes and probably doesn't hurt
343a06a5
JJ
59 // otherwise. On True64-Unix RTLD_GLOBAL is not allowed and on VMS the
60 // second argument on dlopen is ignored.
99d96a2b
JJ
61#ifdef __VMS
62# define wxDllOpen(lib) dlopen(lib.fn_str(), 0 )
343a06a5
JJ
63#elif defined( __osf__ )
64# define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_LAZY )
99d96a2b
JJ
65#else
66# define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_LAZY | RTLD_GLOBAL)
67#endif
68#define wxDllGetSymbol(handle, name) dlsym(handle, name)
a585ca5c 69# define wxDllClose dlclose
8a0d4cf6 70#elif defined(HAVE_SHL_LOAD)
a585ca5c 71# define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
237c5c02 72# define wxDllClose shl_unload
846e1424 73
0b9ab0bd
RL
74static inline void *wxDllGetSymbol(shl_t handle, const wxString& name)
75{
76 void *sym;
77 if ( shl_findsym(&handle, name.mb_str(), TYPE_UNDEFINED, &sym) == 0 )
78 return sym;
79 else
80 return 0;
81}
82
f11bdd03
GD
83#elif defined(__DARWIN__)
84/* Porting notes:
85 * The dlopen port is a port from dl_next.xs by Anno Siegel.
86 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
87 * The method used here is just to supply the sun style dlopen etc.
88 * functions in terms of Darwin NS*.
89 */
4c27c3c6
GD
90void *dlopen(const char *path, int mode /* mode is ignored */);
91void *dlsym(void *handle, const char *symbol);
03e11df5 92int dlclose(void *handle);
4c27c3c6 93const char *dlerror(void);
03e11df5
GD
94
95# define wxDllOpen(lib) dlopen(lib.fn_str(), 0)
96# define wxDllGetSymbol(handle, name) dlsym(handle, name)
97# define wxDllClose dlclose
7b0bfbb2 98#elif defined(__WINDOWS__)
90022ddc 99 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
a585ca5c 100# ifdef __WIN32__
f6bcfd97
BP
101#ifdef _UNICODE
102# define wxDllOpen(lib) ::LoadLibraryExW(lib, 0, 0)
103#else
104# define wxDllOpen(lib) ::LoadLibraryExA(lib, 0, 0)
105#endif
a585ca5c
KB
106# else // Win16
107# define wxDllOpen(lib) ::LoadLibrary(lib)
108# endif // Win32/16
109# define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
110# define wxDllClose ::FreeLibrary
763ca781 111#elif defined(__WXMAC__)
76a5e5d2 112# define wxDllClose(handle) CloseConnection(&((CFragConnectionID)handle))
7b0bfbb2 113#else
a585ca5c 114# error "Don't know how to load shared libraries on this platform."
7b0bfbb2 115#endif // OS
7a4b9130
GL
116
117// ---------------------------------------------------------------------------
7b0bfbb2 118// Global variables
7a4b9130
GL
119// ---------------------------------------------------------------------------
120
7b0bfbb2 121wxLibraries wxTheLibraries;
7a4b9130 122
f6bcfd97
BP
123// ============================================================================
124// implementation
125// ============================================================================
123a7fdd 126
7b0bfbb2
VZ
127// construct the full name from the base shared object name: adds a .dll
128// suffix under Windows or .so under Unix
129static wxString ConstructLibraryName(const wxString& basename)
130{
f6bcfd97
BP
131 wxString fullname;
132 fullname << basename << wxDllLoader::GetDllExt();
27529614 133
7b0bfbb2
VZ
134 return fullname;
135}
7a4b9130 136
7a4b9130 137// ---------------------------------------------------------------------------
7b0bfbb2 138// wxLibrary (one instance per dynamic library)
7a4b9130
GL
139// ---------------------------------------------------------------------------
140
7b0bfbb2 141wxLibrary::wxLibrary(wxDllType handle)
7a4b9130 142{
7b0bfbb2
VZ
143 typedef wxClassInfo *(*t_get_first)(void);
144 t_get_first get_first;
7a4b9130 145
7b0bfbb2 146 m_handle = handle;
7a4b9130 147
7b0bfbb2
VZ
148 // Some system may use a local heap for library.
149 get_first = (t_get_first)GetSymbol("wxGetClassFirst");
150 // It is a wxWindows DLL.
151 if (get_first)
152 PrepareClasses(get_first());
7a4b9130
GL
153}
154
155wxLibrary::~wxLibrary()
156{
7b0bfbb2
VZ
157 if ( m_handle )
158 {
159 wxDllClose(m_handle);
160 }
7a4b9130
GL
161}
162
163wxObject *wxLibrary::CreateObject(const wxString& name)
164{
7b0bfbb2 165 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
f4a8c29f 166
7b0bfbb2
VZ
167 if (!info)
168 return NULL;
f4a8c29f 169
7b0bfbb2 170 return info->CreateObject();
f4a8c29f
GL
171}
172
856d2e52 173void wxLibrary::PrepareClasses(wxClassInfo *first)
f4a8c29f 174{
7b0bfbb2
VZ
175 // Index all class infos by their class name
176 wxClassInfo *info = first;
177 while (info)
178 {
179 if (info->m_className)
180 classTable.Put(info->m_className, (wxObject *)info);
0b9ab0bd 181 info = info->m_next;
7b0bfbb2
VZ
182 }
183
184 // Set base pointers for each wxClassInfo
185 info = first;
186 while (info)
187 {
188 if (info->GetBaseClassName1())
189 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
190 if (info->GetBaseClassName2())
191 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
192 info = info->m_next;
193 }
7a4b9130
GL
194}
195
196void *wxLibrary::GetSymbol(const wxString& symbname)
197{
a585ca5c
KB
198 return wxDllLoader::GetSymbol(m_handle, symbname);
199}
7b0bfbb2 200
a585ca5c
KB
201// ---------------------------------------------------------------------------
202// wxDllLoader
203// ---------------------------------------------------------------------------
7b0bfbb2 204
f6bcfd97
BP
205
206#if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
0b9ab0bd 207const wxString wxDllLoader::ms_dllext( _T(".dll") );
f6bcfd97 208#elif defined(__UNIX__)
0b9ab0bd
RL
209#if defined(__HPUX__)
210const wxString wxDllLoader::ms_dllext( _T(".sl") );
211#else
212const wxString wxDllLoader::ms_dllext( _T(".so") );
213#endif
337b2454
SC
214#elif defined(__WXMAC__)
215const wxString wxDllLoader::ms_dllext( _T("") );
f6bcfd97 216#endif
f6bcfd97 217
0868079c 218/* static */
0b9ab0bd 219wxDllType wxDllLoader::GetProgramHandle()
0868079c 220{
1a787c5d 221#if defined( HAVE_DLOPEN ) && !defined(__EMX__)
7742efff 222 // optain handle for main program
d236a959 223 return dlopen(NULL, RTLD_NOW/*RTLD_LAZY*/);
7742efff
KB
224#elif defined (HAVE_SHL_LOAD)
225 // shl_findsymbol with NULL handle looks up in main program
d236a959 226 return 0;
0868079c 227#else
58c837a4 228 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
7742efff 229 return 0;
7cc98b3e 230#endif
0868079c
KB
231}
232
a585ca5c 233/* static */
0b9ab0bd 234wxDllType wxDllLoader::LoadLibrary(const wxString & libname, bool *success)
a585ca5c 235{
0b9ab0bd
RL
236 wxDllType handle;
237 bool failed = FALSE;
7b0bfbb2 238
03e11df5 239#if defined(__WXMAC__) && !defined(__UNIX__)
0b9ab0bd
RL
240 FSSpec myFSSpec;
241 Ptr myMainAddr;
242 Str255 myErrName;
243
244 wxMacFilename2FSSpec( libname , &myFSSpec );
245
246 if( GetDiskFragment( &myFSSpec,
247 0,
248 kCFragGoesToEOF,
249 "\p",
250 kPrivateCFragCopy,
76a5e5d2 251 &((CFragConnectionID)handle),
0b9ab0bd
RL
252 &myMainAddr,
253 myErrName ) != noErr )
7cc98b3e 254 {
0b9ab0bd
RL
255 p2cstr( myErrName );
256 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
257 libname.c_str(),
258 (char*)myErrName );
259 handle = 0;
260 failed = TRUE;
7cc98b3e 261 }
0b9ab0bd 262
1a787c5d 263#elif defined(__WXPM__) || defined(__EMX__)
0b9ab0bd 264 char zError[256] = "";
c2ff79b1 265 wxDllOpen(zError, libname, handle);
0b9ab0bd
RL
266
267#else
93d2bf44 268 handle = wxDllOpen(libname);
0b9ab0bd
RL
269
270#endif
a585ca5c 271
7cc98b3e
VZ
272 if ( !handle )
273 {
f6bcfd97
BP
274 wxString msg(_("Failed to load shared library '%s'"));
275
276#ifdef HAVE_DLERROR
d0166913 277 const wxChar *err = dlerror();
0b9ab0bd 278 if( err )
f6bcfd97 279 {
0b9ab0bd
RL
280 failed = TRUE;
281 wxLogError( msg, err );
f6bcfd97 282 }
0b9ab0bd
RL
283#else
284 failed = TRUE;
285 wxLogSysError( msg, libname.c_str() );
286#endif
7cc98b3e
VZ
287 }
288
289 if ( success )
0b9ab0bd 290 *success = !failed;
7cc98b3e
VZ
291
292 return handle;
a585ca5c
KB
293}
294
752c7d6b
KB
295
296/* static */
0b9ab0bd 297void wxDllLoader::UnloadLibrary(wxDllType handle)
752c7d6b
KB
298{
299 wxDllClose(handle);
300}
301
a585ca5c 302/* static */
0b9ab0bd 303void *wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name, bool *success)
a585ca5c 304{
0b9ab0bd
RL
305 bool failed = FALSE;
306 void *symbol = 0;
a585ca5c 307
03e11df5 308#if defined(__WXMAC__) && !defined(__UNIX__)
0b9ab0bd
RL
309 Ptr symAddress;
310 CFragSymbolClass symClass;
311 Str255 symName;
7cc98b3e 312
03e11df5 313#if TARGET_CARBON
0b9ab0bd 314 c2pstrcpy( (StringPtr) symName, name );
03e11df5 315#else
0b9ab0bd
RL
316 strcpy( (char *) symName, name );
317 c2pstr( (char *) symName );
03e11df5 318#endif
76a5e5d2 319 if( FindSymbol( ((CFragConnectionID)dllHandle), symName, &symAddress, &symClass ) == noErr )
0b9ab0bd 320 symbol = (void *)symAddress;
7cc98b3e 321
0b9ab0bd 322#elif defined(__WXPM__) || defined(__EMX__)
c2ff79b1 323 wxDllGetSymbol(dllHandle, symbol);
0b9ab0bd 324
b7b35e50
VZ
325#else // Windows or Unix
326
f6bcfd97 327 // mb_str() is necessary in Unicode build
b7b35e50
VZ
328 //
329 // "void *" cast is needed by gcc 3.1 + w32api 1.4, don't ask me why
330 symbol = (void *)wxDllGetSymbol(dllHandle, name.mb_str());
0b9ab0bd 331
b7b35e50 332#endif // OS
7b0bfbb2
VZ
333
334 if ( !symbol )
335 {
0b9ab0bd
RL
336 wxString msg(_("wxDllLoader failed to GetSymbol '%s'"));
337
338#ifdef HAVE_DLERROR
d0166913 339 const wxChar *err = dlerror();
0b9ab0bd
RL
340 if( err )
341 {
342 failed = TRUE;
343 wxLogError( msg, err );
344 }
345#else
346 failed = TRUE;
7cc98b3e
VZ
347 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
348 name.c_str());
0b9ab0bd 349#endif
7b0bfbb2 350 }
0b9ab0bd
RL
351
352 if( success )
353 *success = !failed;
354
7b0bfbb2 355 return symbol;
7a4b9130
GL
356}
357
358// ---------------------------------------------------------------------------
359// wxLibraries (only one instance should normally exist)
360// ---------------------------------------------------------------------------
361
65fd5cb0 362wxLibraries::wxLibraries():m_loaded(wxKEY_STRING)
7a4b9130
GL
363{
364}
365
366wxLibraries::~wxLibraries()
367{
7b0bfbb2 368 wxNode *node = m_loaded.First();
7a4b9130 369
7b0bfbb2
VZ
370 while (node) {
371 wxLibrary *lib = (wxLibrary *)node->Data();
372 delete lib;
7a4b9130 373
7b0bfbb2
VZ
374 node = node->Next();
375 }
7a4b9130
GL
376}
377
378wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
379{
0b9ab0bd 380 wxLibrary *lib;
7b0bfbb2 381 wxClassInfo *old_sm_first;
0b9ab0bd 382 wxNode *node = m_loaded.Find(name.GetData());
7a4b9130 383
c2ff79b1
DW
384 if (node != NULL)
385 return ((wxLibrary *)node->Data());
0b9ab0bd 386
7b0bfbb2
VZ
387 // If DLL shares data, this is necessary.
388 old_sm_first = wxClassInfo::sm_first;
389 wxClassInfo::sm_first = NULL;
390
7cc98b3e 391 wxString libname = ConstructLibraryName(name);
856d2e52 392
4dc2c3bb 393 bool success = FALSE;
7cc98b3e 394 wxDllType handle = wxDllLoader::LoadLibrary(libname, &success);
a585ca5c 395 if(success)
7b0bfbb2 396 {
a585ca5c
KB
397 lib = new wxLibrary(handle);
398 wxClassInfo::sm_first = old_sm_first;
0b9ab0bd 399
a585ca5c 400 m_loaded.Append(name.GetData(), lib);
7b0bfbb2 401 }
a585ca5c
KB
402 else
403 lib = NULL;
7b0bfbb2 404 return lib;
7a4b9130
GL
405}
406
407wxObject *wxLibraries::CreateObject(const wxString& path)
408{
7b0bfbb2
VZ
409 wxNode *node = m_loaded.First();
410 wxObject *obj;
7a4b9130 411
7b0bfbb2
VZ
412 while (node) {
413 obj = ((wxLibrary *)node->Data())->CreateObject(path);
414 if (obj)
415 return obj;
7a4b9130 416
7b0bfbb2
VZ
417 node = node->Next();
418 }
419 return NULL;
7a4b9130 420}
8a0d4cf6 421
229d3f1c
GD
422#endif // wxUSE_DYNLIB_CLASS && !wxUSE_DYNAMIC_LOADER
423
424#if defined(__DARWIN__) && (wxUSE_DYNLIB_CLASS || wxUSE_DYNAMIC_LOADER)
f11bdd03
GD
425// ---------------------------------------------------------------------------
426// For Darwin/Mac OS X
427// supply the sun style dlopen functions in terms of Darwin NS*
428// ---------------------------------------------------------------------------
429
229d3f1c
GD
430#include <stdio.h>
431#include <mach-o/dyld.h>
f11bdd03
GD
432
433static char dl_last_error[1024];
434
435static
f9ee64b1 436void TranslateError(const char *path, int number)
f11bdd03
GD
437{
438 unsigned int index;
439 static char *OFIErrorStrings[] =
440 {
441 "%s(%d): Object Image Load Failure\n",
442 "%s(%d): Object Image Load Success\n",
443 "%s(%d): Not an recognisable object file\n",
444 "%s(%d): No valid architecture\n",
445 "%s(%d): Object image has an invalid format\n",
446 "%s(%d): Invalid access (permissions?)\n",
447 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
448 };
449#define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
450
f9ee64b1
GD
451 index = number;
452 if (index > NUM_OFI_ERRORS - 1) {
453 index = NUM_OFI_ERRORS - 1;
f11bdd03 454 }
f9ee64b1 455 sprintf(dl_last_error, OFIErrorStrings[index], path, number);
f11bdd03
GD
456}
457
458const char *dlerror()
459{
460 return dl_last_error;
461}
462
1b0674f7 463void *dlopen(const char *path, int WXUNUSED(mode) /* mode is ignored */)
f11bdd03
GD
464{
465 int dyld_result;
466 NSObjectFileImage ofile;
467 NSModule handle = NULL;
468
469 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
470 if (dyld_result != NSObjectFileImageSuccess)
471 {
f9ee64b1 472 TranslateError(path, dyld_result);
f11bdd03
GD
473 }
474 else
475 {
476 // NSLinkModule will cause the run to abort on any link error's
477 // not very friendly but the error recovery functionality is limited.
f9ee64b1 478 handle = NSLinkModule(ofile, path, NSLINKMODULE_OPTION_BINDNOW);
f11bdd03
GD
479 }
480
481 return handle;
482}
483
f9ee64b1 484int dlclose(void *handle)
f11bdd03 485{
a8522481 486 NSUnLinkModule( handle, NSUNLINKMODULE_OPTION_NONE);
f11bdd03
GD
487 return 0;
488}
489
1b0674f7 490void *dlsym(void *WXUNUSED(handle), const char *symbol)
f11bdd03
GD
491{
492 void *addr;
493
494 if (NSIsSymbolNameDefined(symbol)) {
495 addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol));
496 }
497 else {
498 addr = NULL;
499 }
500 return addr;
501}
502
229d3f1c 503#endif // defined(__DARWIN__) && (wxUSE_DYNLIB_CLASS || wxUSE_DYNAMIC_LOADER)