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()
40 #include "wx/dynload.h"
41 #include "wx/module.h"
43 #if defined(__DARWIN__)
45 * The dlopen port is a port from dl_next.xs by Anno Siegel.
46 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
47 * The method used here is just to supply the sun style dlopen etc.
48 * functions in terms of Darwin NS*.
50 void *dlopen(const char *path
, int mode
/* mode is ignored */);
51 void *dlsym(void *handle
, const char *symbol
);
52 int dlclose(void *handle
);
53 const char *dlerror(void);
56 // ============================================================================
58 // ============================================================================
60 // ---------------------------------------------------------------------------
62 // ---------------------------------------------------------------------------
64 //FIXME: This class isn't really common at all, it should be moved into
65 // platform dependent files.
67 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
68 const wxString
wxDynamicLibrary::ms_dllext( _T(".dll") );
69 #elif defined(__UNIX__)
71 const wxString
wxDynamicLibrary::ms_dllext( _T(".sl") );
73 const wxString
wxDynamicLibrary::ms_dllext( _T(".so") );
77 wxDllType
wxDynamicLibrary::GetProgramHandle()
79 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
80 return dlopen(0, RTLD_LAZY
);
81 #elif defined (HAVE_SHL_LOAD)
84 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
89 bool wxDynamicLibrary::Load(wxString libname
, int flags
)
91 wxASSERT_MSG(m_handle
== 0, _T("Library already loaded."));
93 // add the proper extension for the DLL ourselves unless told not to
94 if ( !(flags
& wxDL_VERBATIM
) )
96 // and also check that the libname doesn't already have it
98 wxFileName::SplitPath(libname
, NULL
, NULL
, &ext
);
101 libname
+= GetDllExt();
105 #if defined(__WXMAC__) && !defined(__DARWIN__)
110 wxMacFilename2FSSpec( libname
, &myFSSpec
);
112 if( GetDiskFragment( &myFSSpec
,
119 myErrName
) != noErr
)
122 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
128 #elif defined(__WXPM__) || defined(__EMX__)
130 DosLoadModule(err
, sizeof(err
), libname
.c_str(), &m_handle
);
132 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
134 #if defined(__VMS) || defined(__DARWIN__)
135 m_handle
= dlopen(libname
.c_str(), 0); // The second parameter is ignored
139 if( flags
& wxDL_LAZY
)
141 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
142 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
143 rtldFlags
|= RTLD_LAZY
;
145 else if( flags
& wxDL_NOW
)
147 rtldFlags
|= RTLD_NOW
;
149 if( flags
& wxDL_GLOBAL
)
152 wxLogDebug(_T("WARNING: RTLD_GLOBAL is not a supported on this platform."));
154 rtldFlags
|= RTLD_GLOBAL
;
157 m_handle
= dlopen(libname
.c_str(), rtldFlags
);
158 #endif // __VMS || __DARWIN__
160 #elif defined(HAVE_SHL_LOAD)
163 if( flags
& wxDL_LAZY
)
165 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
166 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
167 shlFlags
|= BIND_DEFERRED
;
169 else if( flags
& wxDL_NOW
)
171 shlFlags
|= BIND_IMMEDIATE
;
173 m_handle
= shl_load(libname
.c_str(), BIND_DEFERRED
, 0);
175 #elif defined(__WINDOWS__)
176 m_handle
= ::LoadLibrary(libname
.c_str());
179 #error "runtime shared lib support not implemented"
184 wxString
msg(_("Failed to load shared library '%s'"));
185 #if defined(HAVE_DLERROR) && !defined(__EMX__)
186 const wxChar
*err
= dlerror();
188 wxLogError( msg
, err
);
190 wxLogSysError( msg
, libname
.c_str() );
197 void wxDynamicLibrary::Unload()
201 #if defined(__WXPM__) || defined(__EMX__)
202 DosFreeModule( m_handle
);
203 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
205 #elif defined(HAVE_SHL_LOAD)
206 shl_unload( m_handle
);
207 #elif defined(__WINDOWS__)
208 ::FreeLibrary( m_handle
);
209 #elif defined(__WXMAC__) && !defined(__DARWIN__)
210 CloseConnection( (CFragConnectionID
*) &m_handle
);
212 #error "runtime shared lib support not implemented"
218 void *wxDynamicLibrary::GetSymbol(const wxString
&name
, bool *success
) const
220 wxCHECK_MSG( IsLoaded(), NULL
,
221 _T("Can't load symbol from unloaded library") );
226 #if defined(__WXMAC__) && !defined(__DARWIN__)
228 CFragSymbolClass symClass
;
231 c2pstrcpy( (StringPtr
) symName
, name
);
233 strcpy( (char *)symName
, name
);
234 c2pstr( (char *)symName
);
236 if( FindSymbol( dllHandle
, symName
, &symAddress
, &symClass
) == noErr
)
237 symbol
= (void *)symAddress
;
239 #elif defined(__WXPM__) || defined(__EMX__)
240 DosQueryProcAddr( m_handle
, 1L, name
.c_str(), (PFN
*)symbol
);
242 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
243 symbol
= dlsym( m_handle
, name
.c_str() );
245 #elif defined(HAVE_SHL_LOAD)
246 if( shl_findsym( &m_handle
, name
.c_str(), TYPE_UNDEFINED
, &symbol
) != 0 )
249 #elif defined(__WINDOWS__)
250 symbol
= (void*) ::GetProcAddress( m_handle
, name
.mb_str() );
253 #error "runtime shared lib support not implemented"
258 wxString
msg(_("wxDynamicLibrary failed to GetSymbol '%s'"));
259 #if defined(HAVE_DLERROR) && !defined(__EMX__)
260 const wxChar
*err
= dlerror();
264 wxLogError( msg
, err
);
268 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
279 // ---------------------------------------------------------------------------
281 // ---------------------------------------------------------------------------
284 wxDLImports
* wxPluginLibrary::ms_classes
= NULL
;
286 class wxPluginLibraryModule
: public wxModule
289 wxPluginLibraryModule() {}
290 bool OnInit() { wxPluginLibrary::ms_classes
= new wxDLImports(wxKEY_STRING
); wxPluginManager::CreateManifest(); return TRUE
; }
291 void OnExit() { delete wxPluginLibrary::ms_classes
; wxPluginLibrary::ms_classes
= NULL
;
292 wxPluginManager::ClearManifest(); }
295 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule
)
298 IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule
, wxModule
)
301 wxPluginLibrary::wxPluginLibrary(const wxString
&libname
, int flags
)
305 m_before
= wxClassInfo::sm_first
;
306 Load( libname
, flags
);
307 m_after
= wxClassInfo::sm_first
;
315 --m_linkcount
; // Flag us for deletion
318 wxPluginLibrary::~wxPluginLibrary()
327 bool wxPluginLibrary::UnrefLib()
329 wxASSERT_MSG( m_objcount
== 0, _T("Library unloaded before all objects were destroyed") );
330 if( m_linkcount
== 0 || --m_linkcount
== 0 )
338 // ------------------------
340 // ------------------------
342 void wxPluginLibrary::UpdateClassInfo()
345 wxHashTable
*t
= wxClassInfo::sm_classTable
;
347 // FIXME: Below is simply a cut and paste specialisation of
348 // wxClassInfo::InitializeClasses. Once this stabilises,
349 // the two should probably be merged.
351 // Actually it's becoming questionable whether we should merge
352 // this info with the main ClassInfo tables since we can nearly
353 // handle this completely internally now and it does expose
354 // certain (minimal % user_stupidy) risks.
356 for(info
= m_after
; info
!= m_before
; info
= info
->m_next
)
358 if( info
->m_className
)
360 if( t
->Get(info
->m_className
) == 0 )
361 t
->Put(info
->m_className
, (wxObject
*)info
);
363 // Hash all the class names into a local table too so
364 // we can quickly find the entry they correspond to.
366 if( ms_classes
->Get(info
->m_className
) == 0 )
367 ms_classes
->Put(info
->m_className
, (wxObject
*) this);
371 for(info
= m_after
; info
!= m_before
; info
= info
->m_next
)
373 if( info
->m_baseClassName1
)
374 info
->m_baseInfo1
= (wxClassInfo
*)t
->Get(info
->m_baseClassName1
);
375 if( info
->m_baseClassName2
)
376 info
->m_baseInfo2
= (wxClassInfo
*)t
->Get(info
->m_baseClassName2
);
380 void wxPluginLibrary::RestoreClassInfo()
384 for(info
= m_after
; info
!= m_before
; info
= info
->m_next
)
386 wxClassInfo::sm_classTable
->Delete(info
->m_className
);
387 ms_classes
->Delete(info
->m_className
);
390 if( wxClassInfo::sm_first
== m_after
)
391 wxClassInfo::sm_first
= m_before
;
394 info
= wxClassInfo::sm_first
;
395 while( info
->m_next
&& info
->m_next
!= m_after
) info
= info
->m_next
;
397 wxASSERT_MSG( info
, _T("ClassInfo from wxPluginLibrary not found on purge"))
399 info
->m_next
= m_before
;
403 void wxPluginLibrary::RegisterModules()
405 // Plugin libraries might have wxModules, Register and initialise them if
408 // Note that these classes are NOT included in the reference counting since
409 // it's implicit that they will be unloaded if and when the last handle to
410 // the library is. We do have to keep a copy of the module's pointer
411 // though, as there is currently no way to Unregister it without it.
413 wxASSERT_MSG( m_linkcount
== 1,
414 _T("RegisterModules should only be called for the first load") );
416 for(wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->m_next
)
418 if( info
->IsKindOf(CLASSINFO(wxModule
)) )
420 wxModule
*m
= wxDynamicCast(info
->CreateObject(), wxModule
);
422 wxASSERT_MSG( m
, _T("wxDynamicCast of wxModule failed") );
424 m_wxmodules
.Append(m
);
425 wxModule::RegisterModule(m
);
429 // FIXME: Likewise this is (well was) very similar to InitializeModules()
431 for(wxModuleList::Node
*node
= m_wxmodules
.GetFirst(); node
; node
->GetNext())
433 if( !node
->GetData()->Init() )
435 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
437 // XXX: Watch this, a different hash implementation might break it,
438 // a good hash implementation would let us fix it though.
440 // The name of the game is to remove any uninitialised modules and
441 // let the dtor Exit the rest on shutdown, (which we'll initiate
444 wxModuleList::Node
*oldNode
= 0;
446 node
= node
->GetNext();
448 wxModule::UnregisterModule( node
->GetData() );
452 --m_linkcount
; // Flag us for deletion
458 void wxPluginLibrary::UnregisterModules()
460 wxModuleList::Node
*node
;
462 for(node
= m_wxmodules
.GetFirst(); node
; node
->GetNext())
463 node
->GetData()->Exit();
465 for(node
= m_wxmodules
.GetFirst(); node
; node
->GetNext())
466 wxModule::UnregisterModule( node
->GetData() );
468 m_wxmodules
.DeleteContents(TRUE
);
472 // ---------------------------------------------------------------------------
474 // ---------------------------------------------------------------------------
476 wxDLManifest
* wxPluginManager::ms_manifest
= NULL
;
478 // ------------------------
480 // ------------------------
482 wxPluginLibrary
*wxPluginManager::LoadLibrary(const wxString
&libname
, int flags
)
484 wxString
realname(libname
);
486 if( !(flags
& wxDL_VERBATIM
) )
487 realname
+= wxDynamicLibrary::GetDllExt();
489 wxPluginLibrary
*entry
= (wxPluginLibrary
*) ms_manifest
->Get(realname
);
497 entry
= new wxPluginLibrary( libname
, flags
);
499 if( entry
->IsLoaded() )
501 ms_manifest
->Put(realname
, (wxObject
*) entry
);
505 wxCHECK_MSG( entry
->UnrefLib(), 0,
506 _T("Currently linked library is, ..not loaded??") );
513 bool wxPluginManager::UnloadLibrary(const wxString
&libname
)
515 wxPluginLibrary
*entry
= (wxPluginLibrary
*) ms_manifest
->Get(libname
);
518 entry
= (wxPluginLibrary
*) ms_manifest
->Get(libname
+ wxDynamicLibrary::GetDllExt());
521 return entry
->UnrefLib();
523 wxLogDebug(_T("Attempt to Unlink library '%s' (which is not linked)."), libname
.c_str());
527 #if WXWIN_COMPATIBILITY_2_2
528 wxPluginLibrary
*wxPluginManager::GetObjectFromHandle(wxDllType handle
)
531 ms_manifest
->BeginFind();
533 for(node
= ms_manifest
->Next(); node
; node
= ms_manifest
->Next())
534 if( ((wxPluginLibrary
*)node
->GetData())->GetLibHandle() == handle
)
535 return (wxPluginLibrary
*)node
->GetData();
541 // ------------------------
542 // Class implementation
543 // ------------------------
545 bool wxPluginManager::Load(const wxString
&libname
, int flags
)
547 m_entry
= wxPluginManager::LoadLibrary(libname
, flags
);
551 void wxPluginManager::Unload()
554 ms_manifest
->BeginFind();
556 // It's either this or store the name of the lib just to do this.
558 for(node
= ms_manifest
->Next(); node
; node
= ms_manifest
->Next())
559 if( (wxPluginLibrary
*)node
->GetData() == m_entry
)
562 if( m_entry
&& m_entry
->UnrefLib() )
569 // ---------------------------------------------------------------------------
570 // wxDllLoader (all these methods are static)
571 // ---------------------------------------------------------------------------
573 #if WXWIN_COMPATIBILITY_2_2
575 wxDllType
wxDllLoader::LoadLibrary(const wxString
&name
)
577 wxPluginLibrary
*p
= wxPluginManager::LoadLibrary(name
, wxDL_DEFAULT
| wxDL_VERBATIM
);
578 return p
->GetLibHandle();
581 void wxDllLoader::UnloadLibrary(wxDllType handle
)
583 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(handle
);
587 void *wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
)
589 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(dllHandle
);
590 return p
->GetSymbol(name
, success
);
593 #endif // WXWIN_COMPATIBILITY_2_2
595 #endif // wxUSE_DYNAMIC_LOADER