]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
wxTLW::SetLeft/RightMenu of smart phones documented.
[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
65571936 9// Licence: wxWindows licence
7a4b9130
GL
10/////////////////////////////////////////////////////////////////////////////
11
7b0bfbb2
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
1948bb32 30#if wxUSE_DYNLIB_CLASS
8a0d4cf6 31
f6bcfd97 32#if defined(__WINDOWS__)
5fccb5b4 33 #include "wx/msw/wrapwin.h"
f6bcfd97
BP
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"
1948bb32
VS
40#include "wx/utils.h"
41#include "wx/filename.h" // for SplitPath()
a8eaaeb2
VS
42#include "wx/app.h"
43#include "wx/apptrait.h"
7b0bfbb2 44
76a5e5d2
SC
45#if defined(__WXMAC__)
46 #include "wx/mac/private.h"
47#endif
48
7b0bfbb2 49
1948bb32
VS
50// ============================================================================
51// implementation
52// ============================================================================
846e1424 53
1948bb32
VS
54#if defined(__DARWIN__)
55// ---------------------------------------------------------------------------
56// For Darwin/Mac OS X
57// supply the sun style dlopen functions in terms of Darwin NS*
58// ---------------------------------------------------------------------------
0b9ab0bd 59
f11bdd03
GD
60/* Porting notes:
61 * The dlopen port is a port from dl_next.xs by Anno Siegel.
62 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
63 * The method used here is just to supply the sun style dlopen etc.
64 * functions in terms of Darwin NS*.
65 */
7a4b9130 66
1948bb32
VS
67#include <stdio.h>
68#include <mach-o/dyld.h>
7a4b9130 69
1948bb32 70static char dl_last_error[1024];
123a7fdd 71
1948bb32
VS
72static
73void TranslateError(const char *path, int number)
7b0bfbb2 74{
1948bb32
VS
75 unsigned int index;
76 static char *OFIErrorStrings[] =
77 {
5fccb5b4
VZ
78 "%s(%d): Object Image Load Failure\n",
79 "%s(%d): Object Image Load Success\n",
80 "%s(%d): Not an recognisable object file\n",
81 "%s(%d): No valid architecture\n",
82 "%s(%d): Object image has an invalid format\n",
83 "%s(%d): Invalid access (permissions?)\n",
84 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
1948bb32
VS
85 };
86#define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
27529614 87
1948bb32
VS
88 index = number;
89 if (index > NUM_OFI_ERRORS - 1) {
90 index = NUM_OFI_ERRORS - 1;
91 }
92 sprintf(dl_last_error, OFIErrorStrings[index], path, number);
7b0bfbb2 93}
7a4b9130 94
1948bb32 95const char *dlerror()
7a4b9130 96{
1948bb32 97 return dl_last_error;
7a4b9130
GL
98}
99
1948bb32 100void *dlopen(const char *path, int WXUNUSED(mode) /* mode is ignored */)
7a4b9130 101{
1948bb32
VS
102 int dyld_result;
103 NSObjectFileImage ofile;
104 NSModule handle = NULL;
105
106 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
107 if (dyld_result != NSObjectFileImageSuccess)
7b0bfbb2 108 {
5fccb5b4 109 TranslateError(path, dyld_result);
7b0bfbb2 110 }
1948bb32
VS
111 else
112 {
5fccb5b4
VZ
113 // NSLinkModule will cause the run to abort on any link error's
114 // not very friendly but the error recovery functionality is limited.
115 handle = NSLinkModule(ofile, path, NSLINKMODULE_OPTION_BINDNOW);
1948bb32
VS
116 }
117
118 return handle;
7a4b9130
GL
119}
120
1948bb32 121int dlclose(void *handle)
7a4b9130 122{
1948bb32
VS
123 NSUnLinkModule( handle, NSUNLINKMODULE_OPTION_NONE);
124 return 0;
f4a8c29f
GL
125}
126
1948bb32 127void *dlsym(void *handle, const char *symbol)
f4a8c29f 128{
1948bb32 129 void *addr;
5fccb5b4 130
1948bb32
VS
131 NSSymbol nsSymbol = NSLookupSymbolInModule( handle , symbol ) ;
132
5fccb5b4 133 if ( nsSymbol)
7b0bfbb2 134 {
5fccb5b4 135 addr = NSAddressOfSymbol(nsSymbol);
7b0bfbb2 136 }
5fccb5b4 137 else
7b0bfbb2 138 {
5fccb5b4 139 addr = NULL;
7b0bfbb2 140 }
1948bb32 141 return addr;
7a4b9130
GL
142}
143
1948bb32
VS
144#endif // defined(__DARWIN__)
145
7b0bfbb2 146
a585ca5c 147// ---------------------------------------------------------------------------
1948bb32 148// wxDynamicLibrary
a585ca5c 149// ---------------------------------------------------------------------------
7b0bfbb2 150
1948bb32
VS
151//FIXME: This class isn't really common at all, it should be moved into
152// platform dependent files.
f6bcfd97
BP
153
154#if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
1948bb32 155 const wxChar *wxDynamicLibrary::ms_dllext = _T(".dll");
87ec9b8f
GD
156#elif defined(__WXMAC__) && !defined(__DARWIN__)
157 const wxChar *wxDynamicLibrary::ms_dllext = _T("");
f6bcfd97 158#elif defined(__UNIX__)
1948bb32
VS
159 #if defined(__HPUX__)
160 const wxChar *wxDynamicLibrary::ms_dllext = _T(".sl");
87ec9b8f
GD
161 #elif defined(__DARWIN__)
162 const wxChar *wxDynamicLibrary::ms_dllext = _T(".dylib");
1948bb32
VS
163 #else
164 const wxChar *wxDynamicLibrary::ms_dllext = _T(".so");
165 #endif
f6bcfd97 166#endif
f6bcfd97 167
1948bb32 168wxDllType wxDynamicLibrary::GetProgramHandle()
0868079c 169{
1a787c5d 170#if defined( HAVE_DLOPEN ) && !defined(__EMX__)
1948bb32 171 return dlopen(0, RTLD_LAZY);
7742efff 172#elif defined (HAVE_SHL_LOAD)
1948bb32 173 return PROG_HANDLE;
0868079c 174#else
58c837a4 175 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
7742efff 176 return 0;
7cc98b3e 177#endif
0868079c
KB
178}
179
1948bb32 180bool wxDynamicLibrary::Load(wxString libname, int flags)
a585ca5c 181{
1948bb32
VS
182 wxASSERT_MSG(m_handle == 0, _T("Library already loaded."));
183
184 // add the proper extension for the DLL ourselves unless told not to
185 if ( !(flags & wxDL_VERBATIM) )
186 {
187 // and also check that the libname doesn't already have it
188 wxString ext;
189 wxFileName::SplitPath(libname, NULL, NULL, &ext);
190 if ( ext.empty() )
191 {
192 libname += GetDllExt();
193 }
194 }
7b0bfbb2 195
1948bb32
VS
196 // different ways to load a shared library
197 //
198 // FIXME: should go to the platform-specific files!
199#if defined(__WXMAC__) && !defined(__DARWIN__)
0b9ab0bd
RL
200 FSSpec myFSSpec;
201 Ptr myMainAddr;
202 Str255 myErrName;
203
204 wxMacFilename2FSSpec( libname , &myFSSpec );
205
206 if( GetDiskFragment( &myFSSpec,
207 0,
208 kCFragGoesToEOF,
209 "\p",
210 kPrivateCFragCopy,
1948bb32 211 &m_handle,
0b9ab0bd
RL
212 &myMainAddr,
213 myErrName ) != noErr )
7cc98b3e 214 {
0b9ab0bd
RL
215 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
216 libname.c_str(),
fb44fc34 217 wxMacMakeStringFromPascal( myErrName ).c_str() );
1948bb32 218 m_handle = 0;
7cc98b3e 219 }
0b9ab0bd 220
1a787c5d 221#elif defined(__WXPM__) || defined(__EMX__)
1948bb32
VS
222 char err[256] = "";
223 DosLoadModule(err, sizeof(err), libname.c_str(), &m_handle);
224
225#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
0b9ab0bd 226
1948bb32 227#if defined(__VMS) || defined(__DARWIN__)
5f3f0f17 228 m_handle = dlopen(libname.fn_str(), 0); // The second parameter is ignored
1948bb32
VS
229#else // !__VMS && !__DARWIN__
230 int rtldFlags = 0;
231
232 if ( flags & wxDL_LAZY )
233 {
234 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
235 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
236#ifdef RTLD_LAZY
237 rtldFlags |= RTLD_LAZY;
238#else
239 wxLogDebug(_T("wxDL_LAZY is not supported on this platform"));
240#endif
241 }
242 else if ( flags & wxDL_NOW )
243 {
244#ifdef RTLD_NOW
245 rtldFlags |= RTLD_NOW;
0b9ab0bd 246#else
1948bb32
VS
247 wxLogDebug(_T("wxDL_NOW is not supported on this platform"));
248#endif
249 }
0b9ab0bd 250
1948bb32
VS
251 if ( flags & wxDL_GLOBAL )
252 {
253#ifdef RTLD_GLOBAL
254 rtldFlags |= RTLD_GLOBAL;
255#else
256 wxLogDebug(_T("RTLD_GLOBAL is not supported on this platform."));
0b9ab0bd 257#endif
1948bb32
VS
258 }
259
260 m_handle = dlopen(libname.fn_str(), rtldFlags);
261#endif // __VMS || __DARWIN__ ?
262
263#elif defined(HAVE_SHL_LOAD)
264 int shlFlags = 0;
a585ca5c 265
1948bb32
VS
266 if( flags & wxDL_LAZY )
267 {
268 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
269 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
270 shlFlags |= BIND_DEFERRED;
271 }
272 else if( flags & wxDL_NOW )
273 {
274 shlFlags |= BIND_IMMEDIATE;
275 }
276 m_handle = shl_load(libname.fn_str(), BIND_DEFERRED, 0);
277
278#elif defined(__WINDOWS__)
279 m_handle = ::LoadLibrary(libname.c_str());
280#else
281 #error "runtime shared lib support not implemented on this platform"
282#endif
283
284 if ( m_handle == 0 )
7cc98b3e 285 {
f6bcfd97 286 wxString msg(_("Failed to load shared library '%s'"));
1948bb32 287#if defined(HAVE_DLERROR) && !defined(__EMX__)
f6bcfd97 288
1948bb32
VS
289#if wxUSE_UNICODE
290 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
291 const wxChar *err = buffer;
292#else
d0166913 293 const wxChar *err = dlerror();
1948bb32
VS
294#endif
295
0b9ab0bd 296 if( err )
0b9ab0bd 297 wxLogError( msg, err );
0b9ab0bd 298#else
0b9ab0bd
RL
299 wxLogSysError( msg, libname.c_str() );
300#endif
7cc98b3e
VZ
301 }
302
1948bb32 303 return IsLoaded();
a585ca5c
KB
304}
305
9d033af9
VZ
306/* static */
307void wxDynamicLibrary::Unload(wxDllType handle)
752c7d6b 308{
1948bb32 309#if defined(__WXPM__) || defined(__EMX__)
ff793cab 310 DosFreeModule( handle );
1948bb32 311#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
ff793cab 312 dlclose( handle );
1948bb32 313#elif defined(HAVE_SHL_LOAD)
ff793cab 314 shl_unload( handle );
1948bb32 315#elif defined(__WINDOWS__)
ff793cab 316 ::FreeLibrary( handle );
1948bb32 317#elif defined(__WXMAC__) && !defined(__DARWIN__)
ff793cab 318 CloseConnection( (CFragConnectionID*) &handle );
1948bb32 319#else
9d033af9 320 #error "runtime shared lib support not implemented"
1948bb32 321#endif
752c7d6b
KB
322}
323
1948bb32 324void *wxDynamicLibrary::GetSymbol(const wxString &name, bool *success) const
a585ca5c 325{
1948bb32
VS
326 wxCHECK_MSG( IsLoaded(), NULL,
327 _T("Can't load symbol from unloaded library") );
328
68379eaf 329 bool failed = false;
0b9ab0bd 330 void *symbol = 0;
a585ca5c 331
999836aa 332 wxUnusedVar(symbol);
1948bb32 333#if defined(__WXMAC__) && !defined(__DARWIN__)
0b9ab0bd
RL
334 Ptr symAddress;
335 CFragSymbolClass symClass;
336 Str255 symName;
1948bb32 337#if TARGET_CARBON
f73bc315 338 c2pstrcpy( (StringPtr) symName, name.fn_str() );
1948bb32 339#else
f73bc315 340 strcpy( (char *)symName, name.fn_str() );
1948bb32
VS
341 c2pstr( (char *)symName );
342#endif
609533d5 343 if( FindSymbol( m_handle, symName, &symAddress, &symClass ) == noErr )
0b9ab0bd 344 symbol = (void *)symAddress;
7cc98b3e 345
0b9ab0bd 346#elif defined(__WXPM__) || defined(__EMX__)
1948bb32 347 DosQueryProcAddr( m_handle, 1L, name.c_str(), (PFN*)symbol );
0b9ab0bd 348
1948bb32
VS
349#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
350 symbol = dlsym( m_handle, name.fn_str() );
351
352#elif defined(HAVE_SHL_LOAD)
353 // use local variable since shl_findsym modifies the handle argument
354 // to indicate where the symbol was found (GD)
355 wxDllType the_handle = m_handle;
356 if( shl_findsym( &the_handle, name.fn_str(), TYPE_UNDEFINED, &symbol ) != 0 )
357 symbol = 0;
358
359#elif defined(__WINDOWS__)
ffcb4ee4
JS
360#ifdef __WXWINCE__
361 symbol = (void*) ::GetProcAddress( m_handle, name );
362#else
1948bb32 363 symbol = (void*) ::GetProcAddress( m_handle, name.mb_str() );
ffcb4ee4 364#endif
b7b35e50 365
1c193821 366#else
1948bb32 367#error "runtime shared lib support not implemented"
1c193821 368#endif
0b9ab0bd 369
7b0bfbb2
VZ
370 if ( !symbol )
371 {
1948bb32
VS
372#if defined(HAVE_DLERROR) && !defined(__EMX__)
373
374#if wxUSE_UNICODE
375 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
376 const wxChar *err = buffer;
377#else
d0166913 378 const wxChar *err = dlerror();
1948bb32
VS
379#endif
380
0b9ab0bd
RL
381 if( err )
382 {
ee682a94 383 wxLogError(wxT("%s"), err);
0b9ab0bd
RL
384 }
385#else
68379eaf 386 failed = true;
7cc98b3e
VZ
387 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
388 name.c_str());
0b9ab0bd 389#endif
7b0bfbb2 390 }
0b9ab0bd
RL
391 if( success )
392 *success = !failed;
393
7b0bfbb2 394 return symbol;
7a4b9130 395}
5fccb5b4 396
7a4b9130 397
1948bb32 398/*static*/
350fffae
VZ
399wxString
400wxDynamicLibrary::CanonicalizeName(const wxString& name,
401 wxDynamicLibraryCategory
402#ifdef __UNIX__
403 cat
404#else // !__UNIX__
405 WXUNUSED(cat)
406#endif // __UNIX__/!__UNIX__
407 )
7a4b9130 408{
350fffae
VZ
409 wxString nameCanonic;
410
87ec9b8f 411 // under Unix the library names usually start with "lib" prefix, add it
1948bb32 412#ifdef __UNIX__
350fffae
VZ
413 switch ( cat )
414 {
415 default:
416 wxFAIL_MSG( _T("unknown wxDynamicLibraryCategory value") );
417 // fall through
418
419 case wxDL_MODULE:
420 // don't do anything for modules, their names are arbitrary
421 break;
422
423 case wxDL_LIBRARY:
424 // library names should start with "lib" under Unix
425 nameCanonic = _T("lib");
426 break;
427 }
428#endif // __UNIX__
429
430 nameCanonic << name << GetDllExt();
431 return nameCanonic;
7a4b9130 432}
8a0d4cf6 433
1948bb32
VS
434/*static*/
435wxString wxDynamicLibrary::CanonicalizePluginName(const wxString& name,
436 wxPluginCategory cat)
f11bdd03 437{
1948bb32
VS
438 wxString suffix;
439 if ( cat == wxDL_PLUGIN_GUI )
f11bdd03 440 {
a8eaaeb2
VS
441 wxAppTraits *traits = wxAppConsole::GetInstance() ?
442 wxAppConsole::GetInstance()->GetTraits() : NULL;
5fccb5b4 443 wxASSERT_MSG( traits,
a8eaaeb2 444 _("can't query for GUI plugins name in console applications") );
324899f6 445 suffix = traits->GetToolkitInfo().shortName;
f11bdd03 446 }
1948bb32
VS
447#if wxUSE_UNICODE
448 suffix << _T('u');
449#endif
450#ifdef __WXDEBUG__
451 suffix << _T('d');
452#endif
f11bdd03 453
1948bb32
VS
454 if ( !suffix.empty() )
455 suffix = wxString(_T("_")) + suffix;
f11bdd03 456
3546ffae 457#define WXSTRINGIZE(x) #x
1948bb32
VS
458#ifdef __UNIX__
459 #if (wxMINOR_VERSION % 2) == 0
3546ffae 460 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
1948bb32 461 #else
3546ffae 462 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
1948bb32
VS
463 #endif
464#else
465 #if (wxMINOR_VERSION % 2) == 0
3546ffae 466 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
1948bb32 467 #else
3546ffae 468 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
1948bb32
VS
469 #endif
470#endif
350fffae 471
1948bb32
VS
472 suffix << wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION, wxMINOR_VERSION,
473 wxRELEASE_NUMBER));
474#undef wxDLLVER
3546ffae 475#undef WXSTRINGIZE
f11bdd03 476
2c02ec05
VS
477#ifdef __WINDOWS__
478 // Add compiler identification:
479 #if defined(__GNUG__)
480 suffix << _T("_gcc");
481 #elif defined(__VISUALC__)
482 suffix << _T("_vc");
483 #elif defined(__WATCOMC__)
484 suffix << _T("_wat");
485 #elif defined(__BORLANDC__)
486 suffix << _T("_bcc");
487 #endif
488#endif
489
1948bb32 490 return CanonicalizeName(name + suffix, wxDL_MODULE);
f11bdd03 491}
5fccb5b4 492
1948bb32
VS
493/*static*/
494wxString wxDynamicLibrary::GetPluginsDirectory()
f11bdd03 495{
1948bb32
VS
496#ifdef __UNIX__
497 wxString format = wxGetInstallPrefix();
cb979fac 498 wxString dir;
1948bb32
VS
499 format << wxFILE_SEP_PATH
500 << wxT("lib") << wxFILE_SEP_PATH
501 << wxT("wx") << wxFILE_SEP_PATH
cb979fac 502#if (wxMINOR_VERSION % 2) == 0
1948bb32 503 << wxT("%i.%i");
1948bb32 504 dir.Printf(format.c_str(), wxMAJOR_VERSION, wxMINOR_VERSION);
1948bb32 505#else
cb979fac
VS
506 << wxT("%i.%i.%i");
507 dir.Printf(format.c_str(),
508 wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER);
509#endif
510 return dir;
511
512#else // ! __UNIX__
1948bb32
VS
513 return wxEmptyString;
514#endif
f11bdd03
GD
515}
516
f11bdd03 517
1948bb32 518#endif // wxUSE_DYNLIB_CLASS