]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynload.cpp
Unicode compilation fixes
[wxWidgets.git] / src / common / dynload.cpp
CommitLineData
0b9ab0bd
RL
1/////////////////////////////////////////////////////////////////////////////
2// Name: dynload.cpp
3// Purpose: Dynamic loading framework
4// Author: Ron Lee, David Falkinder, Vadim Zeitlin and a cast of 1000's
5// (derived in part from dynlib.cpp (c) 1998 Guilhem Lavaux)
6// Modified by:
7// Created: 03/12/01
8// RCS-ID: $Id$
9// Copyright: (c) 2001 Ron Lee <ron@debian.org>
55d99c7a 10// Licence: wxWindows licence
0b9ab0bd
RL
11/////////////////////////////////////////////////////////////////////////////
12
13#ifdef __GNUG__
14#pragma implementation "dynload.h"
15#endif
16
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
20
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
409bd320 24 #pragma hdrstop
0b9ab0bd
RL
25#endif
26
27#if wxUSE_DYNAMIC_LOADER
28
29#ifdef __WINDOWS__
409bd320 30 #include "wx/msw/private.h"
0b9ab0bd
RL
31#endif
32
33#ifndef WX_PRECOMP
409bd320
VZ
34 #include "wx/log.h"
35 #include "wx/intl.h"
2b5f62a0 36 #include "wx/hash.h"
076fdb21 37 #include "wx/utils.h"
0b9ab0bd
RL
38#endif
39
409bd320 40#include "wx/filename.h" // for SplitPath()
401eb3de 41#include "wx/strconv.h"
0b9ab0bd 42
409bd320 43#include "wx/dynload.h"
dabd1377 44#include "wx/module.h"
0b9ab0bd 45
f9ee64b1
GD
46#if defined(__DARWIN__)
47/* Porting notes:
48 * The dlopen port is a port from dl_next.xs by Anno Siegel.
49 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
50 * The method used here is just to supply the sun style dlopen etc.
51 * functions in terms of Darwin NS*.
52 */
53void *dlopen(const char *path, int mode /* mode is ignored */);
54void *dlsym(void *handle, const char *symbol);
55int dlclose(void *handle);
56const char *dlerror(void);
57#endif
58
0b9ab0bd
RL
59// ============================================================================
60// implementation
61// ============================================================================
62
63// ---------------------------------------------------------------------------
4f89dbc4 64// wxDynamicLibrary
0b9ab0bd
RL
65// ---------------------------------------------------------------------------
66
4f89dbc4
RL
67//FIXME: This class isn't really common at all, it should be moved into
68// platform dependent files.
0b9ab0bd
RL
69
70#if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
478e0341 71 const wxChar *wxDynamicLibrary::ms_dllext = _T(".dll");
0b9ab0bd 72#elif defined(__UNIX__)
d0fcccc4 73 #if defined(__HPUX__)
478e0341 74 const wxChar *wxDynamicLibrary::ms_dllext = _T(".sl");
d0fcccc4 75 #else
478e0341 76 const wxChar *wxDynamicLibrary::ms_dllext = _T(".so");
d0fcccc4 77 #endif
0b9ab0bd
RL
78#endif
79
4f89dbc4 80wxDllType wxDynamicLibrary::GetProgramHandle()
0b9ab0bd
RL
81{
82#if defined( HAVE_DLOPEN ) && !defined(__EMX__)
4f89dbc4 83 return dlopen(0, RTLD_LAZY);
0b9ab0bd 84#elif defined (HAVE_SHL_LOAD)
4f89dbc4 85 return PROG_HANDLE;
0b9ab0bd
RL
86#else
87 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
88 return 0;
89#endif
90}
91
23213f18 92bool wxDynamicLibrary::Load(wxString libname, int flags)
0b9ab0bd 93{
4f89dbc4
RL
94 wxASSERT_MSG(m_handle == 0, _T("Library already loaded."));
95
409bd320
VZ
96 // add the proper extension for the DLL ourselves unless told not to
97 if ( !(flags & wxDL_VERBATIM) )
98 {
99 // and also check that the libname doesn't already have it
100 wxString ext;
101 wxFileName::SplitPath(libname, NULL, NULL, &ext);
102 if ( ext.empty() )
103 {
104 libname += GetDllExt();
105 }
106 }
0b9ab0bd 107
ee1107cf
VZ
108 // different ways to load a shared library
109 //
110 // FIXME: should go to the platform-specific files!
f9ee64b1 111#if defined(__WXMAC__) && !defined(__DARWIN__)
0b9ab0bd
RL
112 FSSpec myFSSpec;
113 Ptr myMainAddr;
114 Str255 myErrName;
115
116 wxMacFilename2FSSpec( libname , &myFSSpec );
117
118 if( GetDiskFragment( &myFSSpec,
119 0,
120 kCFragGoesToEOF,
121 "\p",
122 kPrivateCFragCopy,
4f89dbc4 123 &m_handle,
0b9ab0bd
RL
124 &myMainAddr,
125 myErrName ) != noErr )
126 {
127 p2cstr( myErrName );
128 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
129 libname.c_str(),
130 (char*)myErrName );
4f89dbc4 131 m_handle = 0;
0b9ab0bd
RL
132 }
133
134#elif defined(__WXPM__) || defined(__EMX__)
4f89dbc4 135 char err[256] = "";
c822ad5a 136 DosLoadModule(err, sizeof(err), libname.c_str(), &m_handle);
4f89dbc4 137
f9ee64b1 138#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
4f89dbc4 139
f9ee64b1 140#if defined(__VMS) || defined(__DARWIN__)
4f89dbc4 141 m_handle = dlopen(libname.c_str(), 0); // The second parameter is ignored
ee1107cf 142#else // !__VMS && !__DARWIN__
4f89dbc4
RL
143 int rtldFlags = 0;
144
ee1107cf 145 if ( flags & wxDL_LAZY )
4f89dbc4
RL
146 {
147 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
148 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
ee1107cf 149#ifdef RTLD_LAZY
4f89dbc4 150 rtldFlags |= RTLD_LAZY;
ee1107cf
VZ
151#else
152 wxLogDebug(_T("wxDL_LAZY is not supported on this platform"));
153#endif
4f89dbc4 154 }
ee1107cf 155 else if ( flags & wxDL_NOW )
4f89dbc4 156 {
ee1107cf 157#ifdef RTLD_NOW
4f89dbc4 158 rtldFlags |= RTLD_NOW;
ee1107cf
VZ
159#else
160 wxLogDebug(_T("wxDL_NOW is not supported on this platform"));
161#endif
4f89dbc4 162 }
ee1107cf
VZ
163
164 if ( flags & wxDL_GLOBAL )
4f89dbc4 165 {
ee1107cf 166#ifdef RTLD_GLOBAL
4f89dbc4 167 rtldFlags |= RTLD_GLOBAL;
ee1107cf
VZ
168#else
169 wxLogDebug(_T("RTLD_GLOBAL is not supported on this platform."));
170#endif
4f89dbc4
RL
171 }
172
401eb3de 173 m_handle = dlopen(libname.fn_str(), rtldFlags);
ee1107cf 174#endif // __VMS || __DARWIN__ ?
4f89dbc4
RL
175
176#elif defined(HAVE_SHL_LOAD)
177 int shlFlags = 0;
178
179 if( flags & wxDL_LAZY )
180 {
181 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
182 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
183 shlFlags |= BIND_DEFERRED;
184 }
185 else if( flags & wxDL_NOW )
186 {
187 shlFlags |= BIND_IMMEDIATE;
188 }
401eb3de 189 m_handle = shl_load(libname.fn_str(), BIND_DEFERRED, 0);
4f89dbc4 190
4f89dbc4
RL
191#elif defined(__WINDOWS__)
192 m_handle = ::LoadLibrary(libname.c_str());
0b9ab0bd 193#else
ee1107cf 194 #error "runtime shared lib support not implemented on this platform"
0b9ab0bd 195#endif
4f89dbc4
RL
196
197 if ( m_handle == 0 )
0b9ab0bd
RL
198 {
199 wxString msg(_("Failed to load shared library '%s'"));
c822ad5a 200#if defined(HAVE_DLERROR) && !defined(__EMX__)
401eb3de 201
8e11fbb0 202#if wxUSE_UNICODE
401eb3de
RR
203 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
204 const wxChar *err = buffer;
205#else
206 const wxChar *err = dlerror();
207#endif
208
0b9ab0bd
RL
209 if( err )
210 wxLogError( msg, err );
211#else
212 wxLogSysError( msg, libname.c_str() );
213#endif
214 }
4f89dbc4
RL
215
216 return IsLoaded();
0b9ab0bd
RL
217}
218
4f89dbc4 219void wxDynamicLibrary::Unload()
0b9ab0bd 220{
4f89dbc4
RL
221 if( IsLoaded() )
222 {
223#if defined(__WXPM__) || defined(__EMX__)
224 DosFreeModule( m_handle );
f9ee64b1 225#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
4f89dbc4
RL
226 dlclose( m_handle );
227#elif defined(HAVE_SHL_LOAD)
228 shl_unload( m_handle );
229#elif defined(__WINDOWS__)
230 ::FreeLibrary( m_handle );
f9ee64b1
GD
231#elif defined(__WXMAC__) && !defined(__DARWIN__)
232 CloseConnection( (CFragConnectionID*) &m_handle );
4f89dbc4
RL
233#else
234#error "runtime shared lib support not implemented"
235#endif
236 m_handle = 0;
237 }
0b9ab0bd
RL
238}
239
4f89dbc4 240void *wxDynamicLibrary::GetSymbol(const wxString &name, bool *success) const
0b9ab0bd 241{
4f89dbc4
RL
242 wxCHECK_MSG( IsLoaded(), NULL,
243 _T("Can't load symbol from unloaded library") );
244
0b9ab0bd
RL
245 bool failed = FALSE;
246 void *symbol = 0;
247
f9ee64b1 248#if defined(__WXMAC__) && !defined(__DARWIN__)
0b9ab0bd
RL
249 Ptr symAddress;
250 CFragSymbolClass symClass;
251 Str255 symName;
252#if TARGET_CARBON
253 c2pstrcpy( (StringPtr) symName, name );
254#else
4f89dbc4
RL
255 strcpy( (char *)symName, name );
256 c2pstr( (char *)symName );
0b9ab0bd
RL
257#endif
258 if( FindSymbol( dllHandle, symName, &symAddress, &symClass ) == noErr )
259 symbol = (void *)symAddress;
260
261#elif defined(__WXPM__) || defined(__EMX__)
4f89dbc4
RL
262 DosQueryProcAddr( m_handle, 1L, name.c_str(), (PFN*)symbol );
263
f9ee64b1 264#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
401eb3de 265 symbol = dlsym( m_handle, name.fn_str() );
0b9ab0bd 266
4f89dbc4 267#elif defined(HAVE_SHL_LOAD)
2b5f62a0
VZ
268 // use local variable since shl_findsym modifies the handle argument
269 // to indicate where the symbol was found (GD)
270 wxDllType the_handle = m_handle;
271 if( shl_findsym( &the_handle, name.fn_str(), TYPE_UNDEFINED, &symbol ) != 0 )
4f89dbc4
RL
272 symbol = 0;
273
4f89dbc4 274#elif defined(__WINDOWS__)
7fc0bd1c 275 symbol = (void*) ::GetProcAddress( m_handle, name.mb_str() );
4f89dbc4
RL
276
277#else
278#error "runtime shared lib support not implemented"
0b9ab0bd
RL
279#endif
280
281 if ( !symbol )
282 {
c822ad5a 283#if defined(HAVE_DLERROR) && !defined(__EMX__)
401eb3de 284
8e11fbb0 285#if wxUSE_UNICODE
401eb3de
RR
286 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
287 const wxChar *err = buffer;
288#else
a1706592 289 const wxChar *err = dlerror();
401eb3de
RR
290#endif
291
0b9ab0bd
RL
292 if( err )
293 {
ee682a94 294 wxLogError(wxT("%s"), err);
0b9ab0bd
RL
295 }
296#else
297 failed = TRUE;
298 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
299 name.c_str());
300#endif
301 }
302 if( success )
303 *success = !failed;
304
305 return symbol;
306}
076fdb21
VS
307
308
309/*static*/
310wxString wxDynamicLibrary::CanonicalizeName(const wxString& name,
311 wxDynamicLibraryCategory cat)
312{
313#ifdef __UNIX__
314 if ( cat == wxDL_MODULE )
315 return name + GetDllExt();
316 else
317 return wxString(_T("lib")) + name + GetDllExt();
318#else
319 return name + GetDllExt();
320#endif
321}
322
323/*static*/
324wxString wxDynamicLibrary::CanonicalizePluginName(const wxString& name,
325 wxPluginCategory cat)
326{
327 wxString suffix;
328 if ( cat == wxDL_PLUGIN_GUI )
329 {
330 suffix = wxString::FromAscii(
331#if defined(__WXMSW__)
332 "msw"
333#elif defined(__WXGTK__)
334 "gtk"
335#elif defined(__WXMGL__)
336 "mgl"
337#elif defined(__WXMOTIF__)
338 "motif"
339#elif defined(__WXOS2__)
340 "pm"
341#elif defined(__WXX11__)
342 "x11"
343#elif defined(__WXMAC__)
344 "mac"
345#elif defined(__WXCOCOA__)
346 "cocoa"
347#endif
348 );
349
350#ifdef __WXUNIVERSAL__
351 suffix << _T("univ");
352#endif
353 }
354#if wxUSE_UNICODE
355 suffix << _T('u');
356#endif
357#ifdef __WXDEBUG__
358 suffix << _T('d');
359#endif
360
361 if ( !suffix.empty() )
362 suffix = wxString(_T("_")) + suffix;
363
364#ifdef __UNIX__
365 #if (wxMINOR_VERSION % 2) == 0
366 #define wxDLLVER(x,y,z) "-" #x "." #y
367 #else
368 #define wxDLLVER(x,y,z) "-" #x "." #y "." #z
369 #endif
370#else
371 #if (wxMINOR_VERSION % 2) == 0
372 #define wxDLLVER(x,y,z) #x #y
373 #else
374 #define wxDLLVER(x,y,z) #x #y #z
375 #endif
376#endif
377 suffix << wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION, wxMINOR_VERSION,
378 wxRELEASE_NUMBER));
379#undef wxDLLVER
380
381 return CanonicalizeName(name + suffix, wxDL_MODULE);
382}
383
384/*static*/
385wxString wxDynamicLibrary::GetPluginsDirectory()
386{
387#ifdef __UNIX__
388 wxString format = wxGetInstallPrefix();
389 format << wxFILE_SEP_PATH
390 << wxT("lib") << wxFILE_SEP_PATH
391 << wxT("wx") << wxFILE_SEP_PATH
392 << wxT("%i.%i");
393 wxString dir;
394 dir.Printf(format.c_str(), wxMAJOR_VERSION, wxMINOR_VERSION);
395 return dir;
396#else
397 return wxEmptyString;
398#endif
399}
0b9ab0bd
RL
400
401
402// ---------------------------------------------------------------------------
4f89dbc4 403// wxPluginLibrary
0b9ab0bd
RL
404// ---------------------------------------------------------------------------
405
406
dabd1377
JS
407wxDLImports* wxPluginLibrary::ms_classes = NULL;
408
409class wxPluginLibraryModule : public wxModule
410{
411public:
d0fcccc4
VZ
412 wxPluginLibraryModule() { }
413
414 // TODO: create ms_classes on demand, why always preallocate it?
415 virtual bool OnInit()
416 {
417 wxPluginLibrary::ms_classes = new wxDLImports(wxKEY_STRING);
418 wxPluginManager::CreateManifest();
419 return TRUE;
420 }
421
422 virtual void OnExit()
423 {
424 delete wxPluginLibrary::ms_classes;
425 wxPluginLibrary::ms_classes = NULL;
426 wxPluginManager::ClearManifest();
427 }
dabd1377
JS
428
429private:
430 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule )
431};
432
433IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule, wxModule)
434
0b9ab0bd 435
23213f18 436wxPluginLibrary::wxPluginLibrary(const wxString &libname, int flags)
4f89dbc4 437 : m_linkcount(1)
7c1e2b44 438 , m_objcount(0)
0b9ab0bd 439{
4f89dbc4
RL
440 m_before = wxClassInfo::sm_first;
441 Load( libname, flags );
442 m_after = wxClassInfo::sm_first;
443
0b9ab0bd
RL
444 if( m_handle != 0 )
445 {
446 UpdateClassInfo();
447 RegisterModules();
448 }
449 else
d0fcccc4
VZ
450 {
451 // Flag us for deletion
452 --m_linkcount;
453 }
0b9ab0bd
RL
454}
455
4f89dbc4 456wxPluginLibrary::~wxPluginLibrary()
0b9ab0bd 457{
01610529
RL
458 if( m_handle != 0 )
459 {
460 UnregisterModules();
461 RestoreClassInfo();
01610529 462 }
0b9ab0bd
RL
463}
464
d0fcccc4
VZ
465wxPluginLibrary *wxPluginLibrary::RefLib()
466{
467 wxCHECK_MSG( m_linkcount > 0, NULL,
468 _T("Library had been already deleted!") );
469
470 ++m_linkcount;
471 return this;
472}
473
4f89dbc4 474bool wxPluginLibrary::UnrefLib()
7c1e2b44 475{
d0fcccc4
VZ
476 wxASSERT_MSG( m_objcount == 0,
477 _T("Library unloaded before all objects were destroyed") );
478
479 if ( --m_linkcount == 0 )
7c1e2b44
RL
480 {
481 delete this;
482 return TRUE;
483 }
d0fcccc4 484
7c1e2b44
RL
485 return FALSE;
486}
0b9ab0bd
RL
487
488// ------------------------
489// Private methods
490// ------------------------
491
4f89dbc4 492void wxPluginLibrary::UpdateClassInfo()
0b9ab0bd
RL
493{
494 wxClassInfo *info;
495 wxHashTable *t = wxClassInfo::sm_classTable;
496
497 // FIXME: Below is simply a cut and paste specialisation of
498 // wxClassInfo::InitializeClasses. Once this stabilises,
499 // the two should probably be merged.
500 //
501 // Actually it's becoming questionable whether we should merge
502 // this info with the main ClassInfo tables since we can nearly
503 // handle this completely internally now and it does expose
504 // certain (minimal % user_stupidy) risks.
505
506 for(info = m_after; info != m_before; info = info->m_next)
507 {
508 if( info->m_className )
509 {
510 if( t->Get(info->m_className) == 0 )
511 t->Put(info->m_className, (wxObject *)info);
512
513 // Hash all the class names into a local table too so
514 // we can quickly find the entry they correspond to.
2b5f62a0 515 (*ms_classes)[info->m_className] = this;
0b9ab0bd
RL
516 }
517 }
518
30bfc425 519#if wxUSE_EXTENDED_RTTI == 0
0b9ab0bd
RL
520 for(info = m_after; info != m_before; info = info->m_next)
521 {
522 if( info->m_baseClassName1 )
523 info->m_baseInfo1 = (wxClassInfo *)t->Get(info->m_baseClassName1);
524 if( info->m_baseClassName2 )
525 info->m_baseInfo2 = (wxClassInfo *)t->Get(info->m_baseClassName2);
526 }
a095505c 527#endif
0b9ab0bd
RL
528}
529
4f89dbc4 530void wxPluginLibrary::RestoreClassInfo()
0b9ab0bd
RL
531{
532 wxClassInfo *info;
533
534 for(info = m_after; info != m_before; info = info->m_next)
535 {
536 wxClassInfo::sm_classTable->Delete(info->m_className);
2b5f62a0 537 ms_classes->erase(ms_classes->find(info->m_className));
0b9ab0bd
RL
538 }
539
540 if( wxClassInfo::sm_first == m_after )
541 wxClassInfo::sm_first = m_before;
542 else
543 {
544 info = wxClassInfo::sm_first;
545 while( info->m_next && info->m_next != m_after ) info = info->m_next;
546
63fd5f0b 547 wxASSERT_MSG( info, _T("ClassInfo from wxPluginLibrary not found on purge"));
0b9ab0bd
RL
548
549 info->m_next = m_before;
550 }
551}
552
4f89dbc4 553void wxPluginLibrary::RegisterModules()
0b9ab0bd
RL
554{
555 // Plugin libraries might have wxModules, Register and initialise them if
556 // they do.
557 //
558 // Note that these classes are NOT included in the reference counting since
559 // it's implicit that they will be unloaded if and when the last handle to
560 // the library is. We do have to keep a copy of the module's pointer
561 // though, as there is currently no way to Unregister it without it.
562
7c1e2b44 563 wxASSERT_MSG( m_linkcount == 1,
0b9ab0bd
RL
564 _T("RegisterModules should only be called for the first load") );
565
6eccc0c4 566 for ( wxClassInfo *info = m_after; info != m_before; info = info->m_next)
0b9ab0bd
RL
567 {
568 if( info->IsKindOf(CLASSINFO(wxModule)) )
569 {
570 wxModule *m = wxDynamicCast(info->CreateObject(), wxModule);
571
572 wxASSERT_MSG( m, _T("wxDynamicCast of wxModule failed") );
573
df5168c4 574 m_wxmodules.push_back(m);
0b9ab0bd
RL
575 wxModule::RegisterModule(m);
576 }
577 }
578
579 // FIXME: Likewise this is (well was) very similar to InitializeModules()
580
df5168c4
MB
581 for ( wxModuleList::iterator it = m_wxmodules.begin();
582 it != m_wxmodules.end();
583 ++it)
0b9ab0bd 584 {
df5168c4 585 if( !(*it)->Init() )
0b9ab0bd 586 {
4f89dbc4 587 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
0b9ab0bd
RL
588
589 // XXX: Watch this, a different hash implementation might break it,
590 // a good hash implementation would let us fix it though.
591
592 // The name of the game is to remove any uninitialised modules and
593 // let the dtor Exit the rest on shutdown, (which we'll initiate
594 // shortly).
595
df5168c4 596 wxModuleList::iterator oldNode = m_wxmodules.end();
0b9ab0bd 597 do {
df5168c4
MB
598 ++it;
599 if( oldNode != m_wxmodules.end() )
600 m_wxmodules.erase(oldNode);
601 wxModule::UnregisterModule( *it );
602 oldNode = it;
603 } while( it != m_wxmodules.end() );
0b9ab0bd 604
7c1e2b44 605 --m_linkcount; // Flag us for deletion
0b9ab0bd
RL
606 break;
607 }
608 }
609}
610
4f89dbc4 611void wxPluginLibrary::UnregisterModules()
0b9ab0bd 612{
df5168c4 613 wxModuleList::iterator it;
0b9ab0bd 614
df5168c4
MB
615 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
616 (*it)->Exit();
0b9ab0bd 617
df5168c4
MB
618 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
619 wxModule::UnregisterModule( *it );
0b9ab0bd 620
df5168c4 621 WX_CLEAR_LIST(wxModuleList, m_wxmodules);
0b9ab0bd
RL
622}
623
624
625// ---------------------------------------------------------------------------
d0fcccc4 626// wxPluginManager
0b9ab0bd
RL
627// ---------------------------------------------------------------------------
628
dabd1377 629wxDLManifest* wxPluginManager::ms_manifest = NULL;
0b9ab0bd
RL
630
631// ------------------------
632// Static accessors
633// ------------------------
634
d0fcccc4
VZ
635wxPluginLibrary *
636wxPluginManager::LoadLibrary(const wxString &libname, int flags)
0b9ab0bd 637{
4f89dbc4 638 wxString realname(libname);
0b9ab0bd 639
4f89dbc4
RL
640 if( !(flags & wxDL_VERBATIM) )
641 realname += wxDynamicLibrary::GetDllExt();
642
d0fcccc4
VZ
643 wxPluginLibrary *entry;
644
645 if ( flags & wxDL_NOSHARE )
646 {
647 entry = NULL;
648 }
649 else
650 {
2b5f62a0 651 entry = FindByName(realname);
d0fcccc4 652 }
4f89dbc4 653
d0fcccc4 654 if ( entry )
0b9ab0bd 655 {
d0fcccc4
VZ
656 wxLogTrace(_T("dll"),
657 _T("LoadLibrary(%s): already loaded."), realname.c_str());
658
7c1e2b44 659 entry->RefLib();
0b9ab0bd
RL
660 }
661 else
662 {
4f89dbc4 663 entry = new wxPluginLibrary( libname, flags );
0b9ab0bd 664
d0fcccc4 665 if ( entry->IsLoaded() )
0b9ab0bd 666 {
2b5f62a0 667 (*ms_manifest)[realname] = entry;
d0fcccc4
VZ
668
669 wxLogTrace(_T("dll"),
670 _T("LoadLibrary(%s): loaded ok."), realname.c_str());
671
0b9ab0bd
RL
672 }
673 else
674 {
d0fcccc4
VZ
675 wxLogTrace(_T("dll"),
676 _T("LoadLibrary(%s): failed to load."), realname.c_str());
677
678 // we have created entry just above
679 if ( !entry->UnrefLib() )
680 {
681 // ... so UnrefLib() is supposed to delete it
682 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
683 }
684
685 entry = NULL;
0b9ab0bd
RL
686 }
687 }
d0fcccc4 688
0b9ab0bd
RL
689 return entry;
690}
691
d0fcccc4 692bool wxPluginManager::UnloadLibrary(const wxString& libname)
0b9ab0bd 693{
d0fcccc4 694 wxString realname = libname;
4f89dbc4 695
2b5f62a0 696 wxPluginLibrary *entry = FindByName(realname);
0b9ab0bd 697
d0fcccc4
VZ
698 if ( !entry )
699 {
700 realname += wxDynamicLibrary::GetDllExt();
0b9ab0bd 701
2b5f62a0 702 entry = FindByName(realname);
d0fcccc4
VZ
703 }
704
705 if ( !entry )
706 {
707 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
708 libname.c_str());
709
710 return FALSE;
711 }
712
713 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname.c_str());
714
715 if ( !entry->UnrefLib() )
716 {
717 // not really unloaded yet
718 return FALSE;
719 }
720
2b5f62a0 721 ms_manifest->erase(ms_manifest->find(realname));
d0fcccc4
VZ
722
723 return TRUE;
4f89dbc4
RL
724}
725
726#if WXWIN_COMPATIBILITY_2_2
727wxPluginLibrary *wxPluginManager::GetObjectFromHandle(wxDllType handle)
728{
2b5f62a0
VZ
729 for ( wxDLManifest::iterator i = ms_manifest->begin();
730 i != ms_manifest->end();
731 ++i )
732 {
733 wxPluginLibrary * const lib = i->second;
4f89dbc4 734
2b5f62a0
VZ
735 if ( lib->GetLibHandle() == handle )
736 return lib;
737 }
4f89dbc4 738
d0fcccc4 739 return NULL;
0b9ab0bd 740}
d0fcccc4 741#endif // WXWIN_COMPATIBILITY_2_2
0b9ab0bd
RL
742
743// ------------------------
744// Class implementation
745// ------------------------
746
23213f18 747bool wxPluginManager::Load(const wxString &libname, int flags)
0b9ab0bd 748{
4f89dbc4 749 m_entry = wxPluginManager::LoadLibrary(libname, flags);
2b5f62a0 750
4f89dbc4 751 return IsLoaded();
0b9ab0bd
RL
752}
753
4f89dbc4 754void wxPluginManager::Unload()
0b9ab0bd 755{
2b5f62a0 756 wxCHECK_RET( m_entry, _T("unloading an invalid wxPluginManager?") );
0b9ab0bd 757
2b5f62a0
VZ
758 for ( wxDLManifest::iterator i = ms_manifest->begin();
759 i != ms_manifest->end();
760 ++i )
4f89dbc4 761 {
2b5f62a0
VZ
762 if ( i->second == m_entry )
763 {
764 ms_manifest->erase(i);
46113053 765 break;
2b5f62a0 766 }
4f89dbc4 767 }
2b5f62a0
VZ
768
769 m_entry->UnrefLib();
770
771 m_entry = NULL;
0b9ab0bd
RL
772}
773
4f89dbc4
RL
774// ---------------------------------------------------------------------------
775// wxDllLoader (all these methods are static)
776// ---------------------------------------------------------------------------
777
778#if WXWIN_COMPATIBILITY_2_2
779
3d7fa4e6 780wxDllType wxDllLoader::LoadLibrary(const wxString &name, bool *success)
4f89dbc4 781{
d0fcccc4
VZ
782 wxPluginLibrary *p = wxPluginManager::LoadLibrary
783 (
784 name,
785 wxDL_DEFAULT | wxDL_VERBATIM | wxDL_NOSHARE
786 );
787
3d7fa4e6
VZ
788 if ( success )
789 *success = p != NULL;
790
d0fcccc4 791 return p ? p->GetLibHandle() : 0;
0b9ab0bd
RL
792}
793
4f89dbc4 794void wxDllLoader::UnloadLibrary(wxDllType handle)
0b9ab0bd 795{
4f89dbc4 796 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(handle);
d0fcccc4
VZ
797
798 wxCHECK_RET( p, _T("Unloading a library not loaded with wxDllLoader?") );
799
4f89dbc4 800 p->UnrefLib();
0b9ab0bd
RL
801}
802
d0fcccc4
VZ
803void *
804wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name, bool *success)
0b9ab0bd 805{
4f89dbc4 806 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(dllHandle);
d0fcccc4
VZ
807
808 if ( !p )
809 {
810 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
811
812 if ( success )
813 *success = FALSE;
814
815 return NULL;
816 }
817
4f89dbc4 818 return p->GetSymbol(name, success);
0b9ab0bd
RL
819}
820
4f89dbc4
RL
821#endif // WXWIN_COMPATIBILITY_2_2
822
0b9ab0bd
RL
823#endif // wxUSE_DYNAMIC_LOADER
824