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 license
11 /////////////////////////////////////////////////////////////////////////////
14 #pragma implementation "dynload.h"
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 #include "wx/wxprec.h"
27 #if wxUSE_DYNAMIC_LOADER
30 #include "wx/msw/private.h"
38 #include "wx/filename.h" // for SplitPath()
39 #include "wx/strconv.h"
41 #include "wx/dynload.h"
42 #include "wx/module.h"
44 #if defined(__DARWIN__)
46 * The dlopen port is a port from dl_next.xs by Anno Siegel.
47 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
48 * The method used here is just to supply the sun style dlopen etc.
49 * functions in terms of Darwin NS*.
51 void *dlopen(const char *path
, int mode
/* mode is ignored */);
52 void *dlsym(void *handle
, const char *symbol
);
53 int dlclose(void *handle
);
54 const char *dlerror(void);
57 // ============================================================================
59 // ============================================================================
61 // ---------------------------------------------------------------------------
63 // ---------------------------------------------------------------------------
65 //FIXME: This class isn't really common at all, it should be moved into
66 // platform dependent files.
68 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
69 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".dll");
70 #elif defined(__UNIX__)
72 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".sl");
74 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".so");
78 wxDllType
wxDynamicLibrary::GetProgramHandle()
80 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
81 return dlopen(0, RTLD_LAZY
);
82 #elif defined (HAVE_SHL_LOAD)
85 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
90 bool wxDynamicLibrary::Load(wxString libname
, int flags
)
92 wxASSERT_MSG(m_handle
== 0, _T("Library already loaded."));
94 // add the proper extension for the DLL ourselves unless told not to
95 if ( !(flags
& wxDL_VERBATIM
) )
97 // and also check that the libname doesn't already have it
99 wxFileName::SplitPath(libname
, NULL
, NULL
, &ext
);
102 libname
+= GetDllExt();
106 // different ways to load a shared library
108 // FIXME: should go to the platform-specific files!
109 #if defined(__WXMAC__) && !defined(__DARWIN__)
114 wxMacFilename2FSSpec( libname
, &myFSSpec
);
116 if( GetDiskFragment( &myFSSpec
,
123 myErrName
) != noErr
)
126 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
132 #elif defined(__WXPM__) || defined(__EMX__)
134 DosLoadModule(err
, sizeof(err
), libname
.c_str(), &m_handle
);
136 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
138 #if defined(__VMS) || defined(__DARWIN__)
139 m_handle
= dlopen(libname
.c_str(), 0); // The second parameter is ignored
140 #else // !__VMS && !__DARWIN__
143 if ( flags
& wxDL_LAZY
)
145 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
146 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
148 rtldFlags
|= RTLD_LAZY
;
150 wxLogDebug(_T("wxDL_LAZY is not supported on this platform"));
153 else if ( flags
& wxDL_NOW
)
156 rtldFlags
|= RTLD_NOW
;
158 wxLogDebug(_T("wxDL_NOW is not supported on this platform"));
162 if ( flags
& wxDL_GLOBAL
)
165 rtldFlags
|= RTLD_GLOBAL
;
167 wxLogDebug(_T("RTLD_GLOBAL is not supported on this platform."));
171 m_handle
= dlopen(libname
.fn_str(), rtldFlags
);
172 #endif // __VMS || __DARWIN__ ?
174 #elif defined(HAVE_SHL_LOAD)
177 if( flags
& wxDL_LAZY
)
179 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
180 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
181 shlFlags
|= BIND_DEFERRED
;
183 else if( flags
& wxDL_NOW
)
185 shlFlags
|= BIND_IMMEDIATE
;
187 m_handle
= shl_load(libname
.fn_str(), BIND_DEFERRED
, 0);
189 #elif defined(__WINDOWS__)
190 m_handle
= ::LoadLibrary(libname
.c_str());
192 #error "runtime shared lib support not implemented on this platform"
197 wxString
msg(_("Failed to load shared library '%s'"));
198 #if defined(HAVE_DLERROR) && !defined(__EMX__)
201 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
202 const wxChar
*err
= buffer
;
204 const wxChar
*err
= dlerror();
208 wxLogError( msg
, err
);
210 wxLogSysError( msg
, libname
.c_str() );
217 void wxDynamicLibrary::Unload()
221 #if defined(__WXPM__) || defined(__EMX__)
222 DosFreeModule( m_handle
);
223 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
225 #elif defined(HAVE_SHL_LOAD)
226 shl_unload( m_handle
);
227 #elif defined(__WINDOWS__)
228 ::FreeLibrary( m_handle
);
229 #elif defined(__WXMAC__) && !defined(__DARWIN__)
230 CloseConnection( (CFragConnectionID
*) &m_handle
);
232 #error "runtime shared lib support not implemented"
238 void *wxDynamicLibrary::GetSymbol(const wxString
&name
, bool *success
) const
240 wxCHECK_MSG( IsLoaded(), NULL
,
241 _T("Can't load symbol from unloaded library") );
246 #if defined(__WXMAC__) && !defined(__DARWIN__)
248 CFragSymbolClass symClass
;
251 c2pstrcpy( (StringPtr
) symName
, name
);
253 strcpy( (char *)symName
, name
);
254 c2pstr( (char *)symName
);
256 if( FindSymbol( dllHandle
, symName
, &symAddress
, &symClass
) == noErr
)
257 symbol
= (void *)symAddress
;
259 #elif defined(__WXPM__) || defined(__EMX__)
260 DosQueryProcAddr( m_handle
, 1L, name
.c_str(), (PFN
*)symbol
);
262 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
263 symbol
= dlsym( m_handle
, name
.fn_str() );
265 #elif defined(HAVE_SHL_LOAD)
266 if( shl_findsym( &m_handle
, name
.fn_str(), TYPE_UNDEFINED
, &symbol
) != 0 )
269 #elif defined(__WINDOWS__)
270 symbol
= (void*) ::GetProcAddress( m_handle
, name
.mb_str() );
273 #error "runtime shared lib support not implemented"
278 wxString
msg(_("wxDynamicLibrary failed to GetSymbol '%s'"));
279 #if defined(HAVE_DLERROR) && !defined(__EMX__)
282 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
283 const wxChar
*err
= buffer
;
285 const wxChar
*err
= dlerror();
291 wxLogError( msg
, err
);
295 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
306 // ---------------------------------------------------------------------------
308 // ---------------------------------------------------------------------------
311 wxDLImports
* wxPluginLibrary::ms_classes
= NULL
;
313 class wxPluginLibraryModule
: public wxModule
316 wxPluginLibraryModule() { }
318 // TODO: create ms_classes on demand, why always preallocate it?
319 virtual bool OnInit()
321 wxPluginLibrary::ms_classes
= new wxDLImports(wxKEY_STRING
);
322 wxPluginManager::CreateManifest();
326 virtual void OnExit()
328 delete wxPluginLibrary::ms_classes
;
329 wxPluginLibrary::ms_classes
= NULL
;
330 wxPluginManager::ClearManifest();
334 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule
)
337 IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule
, wxModule
)
340 wxPluginLibrary::wxPluginLibrary(const wxString
&libname
, int flags
)
344 m_before
= wxClassInfo::sm_first
;
345 Load( libname
, flags
);
346 m_after
= wxClassInfo::sm_first
;
355 // Flag us for deletion
360 wxPluginLibrary::~wxPluginLibrary()
369 wxPluginLibrary
*wxPluginLibrary::RefLib()
371 wxCHECK_MSG( m_linkcount
> 0, NULL
,
372 _T("Library had been already deleted!") );
378 bool wxPluginLibrary::UnrefLib()
380 wxASSERT_MSG( m_objcount
== 0,
381 _T("Library unloaded before all objects were destroyed") );
383 if ( --m_linkcount
== 0 )
392 // ------------------------
394 // ------------------------
396 void wxPluginLibrary::UpdateClassInfo()
399 wxHashTable
*t
= wxClassInfo::sm_classTable
;
401 // FIXME: Below is simply a cut and paste specialisation of
402 // wxClassInfo::InitializeClasses. Once this stabilises,
403 // the two should probably be merged.
405 // Actually it's becoming questionable whether we should merge
406 // this info with the main ClassInfo tables since we can nearly
407 // handle this completely internally now and it does expose
408 // certain (minimal % user_stupidy) risks.
410 for(info
= m_after
; info
!= m_before
; info
= info
->m_next
)
412 if( info
->m_className
)
414 if( t
->Get(info
->m_className
) == 0 )
415 t
->Put(info
->m_className
, (wxObject
*)info
);
417 // Hash all the class names into a local table too so
418 // we can quickly find the entry they correspond to.
420 if( ms_classes
->Get(info
->m_className
) == 0 )
421 ms_classes
->Put(info
->m_className
, (wxObject
*) 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
->Delete(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
.Append(m
);
479 wxModule::RegisterModule(m
);
483 // FIXME: Likewise this is (well was) very similar to InitializeModules()
485 for(wxModuleList::Node
*node
= m_wxmodules
.GetFirst(); node
; node
->GetNext())
487 if( !node
->GetData()->Init() )
489 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
491 // XXX: Watch this, a different hash implementation might break it,
492 // a good hash implementation would let us fix it though.
494 // The name of the game is to remove any uninitialised modules and
495 // let the dtor Exit the rest on shutdown, (which we'll initiate
498 wxModuleList::Node
*oldNode
= 0;
500 node
= node
->GetNext();
502 wxModule::UnregisterModule( node
->GetData() );
506 --m_linkcount
; // Flag us for deletion
512 void wxPluginLibrary::UnregisterModules()
514 wxModuleList::Node
*node
;
516 for(node
= m_wxmodules
.GetFirst(); node
; node
->GetNext())
517 node
->GetData()->Exit();
519 for(node
= m_wxmodules
.GetFirst(); node
; node
->GetNext())
520 wxModule::UnregisterModule( node
->GetData() );
522 m_wxmodules
.DeleteContents(TRUE
);
526 // ---------------------------------------------------------------------------
528 // ---------------------------------------------------------------------------
530 wxDLManifest
* wxPluginManager::ms_manifest
= NULL
;
532 // ------------------------
534 // ------------------------
537 wxPluginManager::LoadLibrary(const wxString
&libname
, int flags
)
539 wxString
realname(libname
);
541 if( !(flags
& wxDL_VERBATIM
) )
542 realname
+= wxDynamicLibrary::GetDllExt();
544 wxPluginLibrary
*entry
;
546 if ( flags
& wxDL_NOSHARE
)
552 entry
= (wxPluginLibrary
*) ms_manifest
->Get(realname
);
557 wxLogTrace(_T("dll"),
558 _T("LoadLibrary(%s): already loaded."), realname
.c_str());
564 entry
= new wxPluginLibrary( libname
, flags
);
566 if ( entry
->IsLoaded() )
568 ms_manifest
->Put(realname
, (wxObject
*) entry
);
570 wxLogTrace(_T("dll"),
571 _T("LoadLibrary(%s): loaded ok."), realname
.c_str());
576 wxLogTrace(_T("dll"),
577 _T("LoadLibrary(%s): failed to load."), realname
.c_str());
579 // we have created entry just above
580 if ( !entry
->UnrefLib() )
582 // ... so UnrefLib() is supposed to delete it
583 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
593 bool wxPluginManager::UnloadLibrary(const wxString
& libname
)
595 wxString realname
= libname
;
597 wxPluginLibrary
*entry
= (wxPluginLibrary
*) ms_manifest
->Get(realname
);
601 realname
+= wxDynamicLibrary::GetDllExt();
603 entry
= (wxPluginLibrary
*) ms_manifest
->Get(realname
);
608 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
614 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname
.c_str());
616 if ( !entry
->UnrefLib() )
618 // not really unloaded yet
622 ms_manifest
->Delete(realname
);
627 #if WXWIN_COMPATIBILITY_2_2
628 wxPluginLibrary
*wxPluginManager::GetObjectFromHandle(wxDllType handle
)
631 ms_manifest
->BeginFind();
633 for(node
= ms_manifest
->Next(); node
; node
= ms_manifest
->Next())
634 if( ((wxPluginLibrary
*)node
->GetData())->GetLibHandle() == handle
)
635 return (wxPluginLibrary
*)node
->GetData();
639 #endif // WXWIN_COMPATIBILITY_2_2
641 // ------------------------
642 // Class implementation
643 // ------------------------
645 bool wxPluginManager::Load(const wxString
&libname
, int flags
)
647 m_entry
= wxPluginManager::LoadLibrary(libname
, flags
);
651 void wxPluginManager::Unload()
654 ms_manifest
->BeginFind();
656 // It's either this or store the name of the lib just to do this.
658 for(node
= ms_manifest
->Next(); node
; node
= ms_manifest
->Next())
659 if( (wxPluginLibrary
*)node
->GetData() == m_entry
)
662 if( m_entry
&& m_entry
->UnrefLib() )
669 // ---------------------------------------------------------------------------
670 // wxDllLoader (all these methods are static)
671 // ---------------------------------------------------------------------------
673 #if WXWIN_COMPATIBILITY_2_2
675 wxDllType
wxDllLoader::LoadLibrary(const wxString
&name
, bool *success
)
677 wxPluginLibrary
*p
= wxPluginManager::LoadLibrary
680 wxDL_DEFAULT
| wxDL_VERBATIM
| wxDL_NOSHARE
684 *success
= p
!= NULL
;
686 return p
? p
->GetLibHandle() : 0;
689 void wxDllLoader::UnloadLibrary(wxDllType handle
)
691 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(handle
);
693 wxCHECK_RET( p
, _T("Unloading a library not loaded with wxDllLoader?") );
699 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
)
701 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(dllHandle
);
705 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
713 return p
->GetSymbol(name
, success
);
716 #endif // WXWIN_COMPATIBILITY_2_2
718 #endif // wxUSE_DYNAMIC_LOADER