1 /////////////////////////////////////////////////////////////////////////////
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)
9 // Copyright: (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "dynload.h"
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 #include "wx/wxprec.h"
27 #if wxUSE_DYNAMIC_LOADER
30 #include "wx/msw/private.h"
39 #include "wx/filename.h" // for SplitPath()
40 #include "wx/strconv.h"
42 #include "wx/dynload.h"
43 #include "wx/module.h"
45 #if defined(__DARWIN__)
47 * The dlopen port is a port from dl_next.xs by Anno Siegel.
48 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
49 * The method used here is just to supply the sun style dlopen etc.
50 * functions in terms of Darwin NS*.
52 void *dlopen(const char *path
, int mode
/* mode is ignored */);
53 void *dlsym(void *handle
, const char *symbol
);
54 int dlclose(void *handle
);
55 const char *dlerror(void);
58 // ============================================================================
60 // ============================================================================
62 // ---------------------------------------------------------------------------
64 // ---------------------------------------------------------------------------
66 //FIXME: This class isn't really common at all, it should be moved into
67 // platform dependent files.
69 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
70 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".dll");
71 #elif defined(__UNIX__)
73 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".sl");
75 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".so");
79 wxDllType
wxDynamicLibrary::GetProgramHandle()
81 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
82 return dlopen(0, RTLD_LAZY
);
83 #elif defined (HAVE_SHL_LOAD)
86 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
91 bool wxDynamicLibrary::Load(wxString libname
, int flags
)
93 wxASSERT_MSG(m_handle
== 0, _T("Library already loaded."));
95 // add the proper extension for the DLL ourselves unless told not to
96 if ( !(flags
& wxDL_VERBATIM
) )
98 // and also check that the libname doesn't already have it
100 wxFileName::SplitPath(libname
, NULL
, NULL
, &ext
);
103 libname
+= GetDllExt();
107 // different ways to load a shared library
109 // FIXME: should go to the platform-specific files!
110 #if defined(__WXMAC__) && !defined(__DARWIN__)
115 wxMacFilename2FSSpec( libname
, &myFSSpec
);
117 if( GetDiskFragment( &myFSSpec
,
124 myErrName
) != noErr
)
127 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
133 #elif defined(__WXPM__) || defined(__EMX__)
135 DosLoadModule(err
, sizeof(err
), libname
.c_str(), &m_handle
);
137 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
139 #if defined(__VMS) || defined(__DARWIN__)
140 m_handle
= dlopen(libname
.c_str(), 0); // The second parameter is ignored
141 #else // !__VMS && !__DARWIN__
144 if ( flags
& wxDL_LAZY
)
146 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
147 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
149 rtldFlags
|= RTLD_LAZY
;
151 wxLogDebug(_T("wxDL_LAZY is not supported on this platform"));
154 else if ( flags
& wxDL_NOW
)
157 rtldFlags
|= RTLD_NOW
;
159 wxLogDebug(_T("wxDL_NOW is not supported on this platform"));
163 if ( flags
& wxDL_GLOBAL
)
166 rtldFlags
|= RTLD_GLOBAL
;
168 wxLogDebug(_T("RTLD_GLOBAL is not supported on this platform."));
172 m_handle
= dlopen(libname
.fn_str(), rtldFlags
);
173 #endif // __VMS || __DARWIN__ ?
175 #elif defined(HAVE_SHL_LOAD)
178 if( flags
& wxDL_LAZY
)
180 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
181 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
182 shlFlags
|= BIND_DEFERRED
;
184 else if( flags
& wxDL_NOW
)
186 shlFlags
|= BIND_IMMEDIATE
;
188 m_handle
= shl_load(libname
.fn_str(), BIND_DEFERRED
, 0);
190 #elif defined(__WINDOWS__)
191 m_handle
= ::LoadLibrary(libname
.c_str());
193 #error "runtime shared lib support not implemented on this platform"
198 wxString
msg(_("Failed to load shared library '%s'"));
199 #if defined(HAVE_DLERROR) && !defined(__EMX__)
202 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
203 const wxChar
*err
= buffer
;
205 const wxChar
*err
= dlerror();
209 wxLogError( msg
, err
);
211 wxLogSysError( msg
, libname
.c_str() );
218 void wxDynamicLibrary::Unload()
222 #if defined(__WXPM__) || defined(__EMX__)
223 DosFreeModule( m_handle
);
224 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
226 #elif defined(HAVE_SHL_LOAD)
227 shl_unload( m_handle
);
228 #elif defined(__WINDOWS__)
229 ::FreeLibrary( m_handle
);
230 #elif defined(__WXMAC__) && !defined(__DARWIN__)
231 CloseConnection( (CFragConnectionID
*) &m_handle
);
233 #error "runtime shared lib support not implemented"
239 void *wxDynamicLibrary::GetSymbol(const wxString
&name
, bool *success
) const
241 wxCHECK_MSG( IsLoaded(), NULL
,
242 _T("Can't load symbol from unloaded library") );
247 #if defined(__WXMAC__) && !defined(__DARWIN__)
249 CFragSymbolClass symClass
;
252 c2pstrcpy( (StringPtr
) symName
, name
);
254 strcpy( (char *)symName
, name
);
255 c2pstr( (char *)symName
);
257 if( FindSymbol( dllHandle
, symName
, &symAddress
, &symClass
) == noErr
)
258 symbol
= (void *)symAddress
;
260 #elif defined(__WXPM__) || defined(__EMX__)
261 DosQueryProcAddr( m_handle
, 1L, name
.c_str(), (PFN
*)symbol
);
263 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
264 symbol
= dlsym( m_handle
, name
.fn_str() );
266 #elif defined(HAVE_SHL_LOAD)
267 // use local variable since shl_findsym modifies the handle argument
268 // to indicate where the symbol was found (GD)
269 wxDllType the_handle
= m_handle
;
270 if( shl_findsym( &the_handle
, name
.fn_str(), TYPE_UNDEFINED
, &symbol
) != 0 )
273 #elif defined(__WINDOWS__)
274 symbol
= (void*) ::GetProcAddress( m_handle
, name
.mb_str() );
277 #error "runtime shared lib support not implemented"
282 #if defined(HAVE_DLERROR) && !defined(__EMX__)
285 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
286 const wxChar
*err
= buffer
;
288 const wxChar
*err
= dlerror();
293 wxLogError(wxT("%s"), err
);
297 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
308 // ---------------------------------------------------------------------------
310 // ---------------------------------------------------------------------------
313 wxDLImports
* wxPluginLibrary::ms_classes
= NULL
;
315 class wxPluginLibraryModule
: public wxModule
318 wxPluginLibraryModule() { }
320 // TODO: create ms_classes on demand, why always preallocate it?
321 virtual bool OnInit()
323 wxPluginLibrary::ms_classes
= new wxDLImports(wxKEY_STRING
);
324 wxPluginManager::CreateManifest();
328 virtual void OnExit()
330 delete wxPluginLibrary::ms_classes
;
331 wxPluginLibrary::ms_classes
= NULL
;
332 wxPluginManager::ClearManifest();
336 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule
)
339 IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule
, wxModule
)
342 wxPluginLibrary::wxPluginLibrary(const wxString
&libname
, int flags
)
346 m_before
= wxClassInfo::sm_first
;
347 Load( libname
, flags
);
348 m_after
= wxClassInfo::sm_first
;
357 // Flag us for deletion
362 wxPluginLibrary::~wxPluginLibrary()
371 wxPluginLibrary
*wxPluginLibrary::RefLib()
373 wxCHECK_MSG( m_linkcount
> 0, NULL
,
374 _T("Library had been already deleted!") );
380 bool wxPluginLibrary::UnrefLib()
382 wxASSERT_MSG( m_objcount
== 0,
383 _T("Library unloaded before all objects were destroyed") );
385 if ( --m_linkcount
== 0 )
394 // ------------------------
396 // ------------------------
398 void wxPluginLibrary::UpdateClassInfo()
401 wxHashTable
*t
= wxClassInfo::sm_classTable
;
403 // FIXME: Below is simply a cut and paste specialisation of
404 // wxClassInfo::InitializeClasses. Once this stabilises,
405 // the two should probably be merged.
407 // Actually it's becoming questionable whether we should merge
408 // this info with the main ClassInfo tables since we can nearly
409 // handle this completely internally now and it does expose
410 // certain (minimal % user_stupidy) risks.
412 for(info
= m_after
; info
!= m_before
; info
= info
->m_next
)
414 if( info
->m_className
)
416 if( t
->Get(info
->m_className
) == 0 )
417 t
->Put(info
->m_className
, (wxObject
*)info
);
419 // Hash all the class names into a local table too so
420 // we can quickly find the entry they correspond to.
421 (*ms_classes
)[info
->m_className
] = this;
425 for(info
= m_after
; info
!= m_before
; info
= info
->m_next
)
427 if( info
->m_baseClassName1
)
428 info
->m_baseInfo1
= (wxClassInfo
*)t
->Get(info
->m_baseClassName1
);
429 if( info
->m_baseClassName2
)
430 info
->m_baseInfo2
= (wxClassInfo
*)t
->Get(info
->m_baseClassName2
);
434 void wxPluginLibrary::RestoreClassInfo()
438 for(info
= m_after
; info
!= m_before
; info
= info
->m_next
)
440 wxClassInfo::sm_classTable
->Delete(info
->m_className
);
441 ms_classes
->erase(ms_classes
->find(info
->m_className
));
444 if( wxClassInfo::sm_first
== m_after
)
445 wxClassInfo::sm_first
= m_before
;
448 info
= wxClassInfo::sm_first
;
449 while( info
->m_next
&& info
->m_next
!= m_after
) info
= info
->m_next
;
451 wxASSERT_MSG( info
, _T("ClassInfo from wxPluginLibrary not found on purge"));
453 info
->m_next
= m_before
;
457 void wxPluginLibrary::RegisterModules()
459 // Plugin libraries might have wxModules, Register and initialise them if
462 // Note that these classes are NOT included in the reference counting since
463 // it's implicit that they will be unloaded if and when the last handle to
464 // the library is. We do have to keep a copy of the module's pointer
465 // though, as there is currently no way to Unregister it without it.
467 wxASSERT_MSG( m_linkcount
== 1,
468 _T("RegisterModules should only be called for the first load") );
470 for ( wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->m_next
)
472 if( info
->IsKindOf(CLASSINFO(wxModule
)) )
474 wxModule
*m
= wxDynamicCast(info
->CreateObject(), wxModule
);
476 wxASSERT_MSG( m
, _T("wxDynamicCast of wxModule failed") );
478 m_wxmodules
.push_back(m
);
479 wxModule::RegisterModule(m
);
483 // FIXME: Likewise this is (well was) very similar to InitializeModules()
485 for ( wxModuleList::iterator it
= m_wxmodules
.begin();
486 it
!= m_wxmodules
.end();
491 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
493 // XXX: Watch this, a different hash implementation might break it,
494 // a good hash implementation would let us fix it though.
496 // The name of the game is to remove any uninitialised modules and
497 // let the dtor Exit the rest on shutdown, (which we'll initiate
500 wxModuleList::iterator oldNode
= m_wxmodules
.end();
503 if( oldNode
!= m_wxmodules
.end() )
504 m_wxmodules
.erase(oldNode
);
505 wxModule::UnregisterModule( *it
);
507 } while( it
!= m_wxmodules
.end() );
509 --m_linkcount
; // Flag us for deletion
515 void wxPluginLibrary::UnregisterModules()
517 wxModuleList::iterator it
;
519 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
522 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
523 wxModule::UnregisterModule( *it
);
525 WX_CLEAR_LIST(wxModuleList
, m_wxmodules
);
529 // ---------------------------------------------------------------------------
531 // ---------------------------------------------------------------------------
533 wxDLManifest
* wxPluginManager::ms_manifest
= NULL
;
535 // ------------------------
537 // ------------------------
540 wxPluginManager::LoadLibrary(const wxString
&libname
, int flags
)
542 wxString
realname(libname
);
544 if( !(flags
& wxDL_VERBATIM
) )
545 realname
+= wxDynamicLibrary::GetDllExt();
547 wxPluginLibrary
*entry
;
549 if ( flags
& wxDL_NOSHARE
)
555 entry
= FindByName(realname
);
560 wxLogTrace(_T("dll"),
561 _T("LoadLibrary(%s): already loaded."), realname
.c_str());
567 entry
= new wxPluginLibrary( libname
, flags
);
569 if ( entry
->IsLoaded() )
571 (*ms_manifest
)[realname
] = entry
;
573 wxLogTrace(_T("dll"),
574 _T("LoadLibrary(%s): loaded ok."), realname
.c_str());
579 wxLogTrace(_T("dll"),
580 _T("LoadLibrary(%s): failed to load."), realname
.c_str());
582 // we have created entry just above
583 if ( !entry
->UnrefLib() )
585 // ... so UnrefLib() is supposed to delete it
586 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
596 bool wxPluginManager::UnloadLibrary(const wxString
& libname
)
598 wxString realname
= libname
;
600 wxPluginLibrary
*entry
= FindByName(realname
);
604 realname
+= wxDynamicLibrary::GetDllExt();
606 entry
= FindByName(realname
);
611 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
617 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname
.c_str());
619 if ( !entry
->UnrefLib() )
621 // not really unloaded yet
625 ms_manifest
->erase(ms_manifest
->find(realname
));
630 #if WXWIN_COMPATIBILITY_2_2
631 wxPluginLibrary
*wxPluginManager::GetObjectFromHandle(wxDllType handle
)
633 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
634 i
!= ms_manifest
->end();
637 wxPluginLibrary
* const lib
= i
->second
;
639 if ( lib
->GetLibHandle() == handle
)
645 #endif // WXWIN_COMPATIBILITY_2_2
647 // ------------------------
648 // Class implementation
649 // ------------------------
651 bool wxPluginManager::Load(const wxString
&libname
, int flags
)
653 m_entry
= wxPluginManager::LoadLibrary(libname
, flags
);
658 void wxPluginManager::Unload()
660 wxCHECK_RET( m_entry
, _T("unloading an invalid wxPluginManager?") );
662 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
663 i
!= ms_manifest
->end();
666 if ( i
->second
== m_entry
)
668 ms_manifest
->erase(i
);
678 // ---------------------------------------------------------------------------
679 // wxDllLoader (all these methods are static)
680 // ---------------------------------------------------------------------------
682 #if WXWIN_COMPATIBILITY_2_2
684 wxDllType
wxDllLoader::LoadLibrary(const wxString
&name
, bool *success
)
686 wxPluginLibrary
*p
= wxPluginManager::LoadLibrary
689 wxDL_DEFAULT
| wxDL_VERBATIM
| wxDL_NOSHARE
693 *success
= p
!= NULL
;
695 return p
? p
->GetLibHandle() : 0;
698 void wxDllLoader::UnloadLibrary(wxDllType handle
)
700 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(handle
);
702 wxCHECK_RET( p
, _T("Unloading a library not loaded with wxDllLoader?") );
708 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
)
710 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(dllHandle
);
714 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
722 return p
->GetSymbol(name
, success
);
725 #endif // WXWIN_COMPATIBILITY_2_2
727 #endif // wxUSE_DYNAMIC_LOADER