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"
40 #include "wx/strconv.h"
42 #include "wx/dynload.h"
43 #include "wx/module.h"
46 // ---------------------------------------------------------------------------
48 // ---------------------------------------------------------------------------
51 wxDLImports
* wxPluginLibrary::ms_classes
= NULL
;
53 class wxPluginLibraryModule
: public wxModule
56 wxPluginLibraryModule() { }
58 // TODO: create ms_classes on demand, why always preallocate it?
61 wxPluginLibrary::ms_classes
= new wxDLImports(wxKEY_STRING
);
62 wxPluginManager::CreateManifest();
68 delete wxPluginLibrary::ms_classes
;
69 wxPluginLibrary::ms_classes
= NULL
;
70 wxPluginManager::ClearManifest();
74 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule
)
77 IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule
, wxModule
)
80 wxPluginLibrary::wxPluginLibrary(const wxString
&libname
, int flags
)
84 m_before
= wxClassInfo::sm_first
;
85 Load( libname
, flags
);
86 m_after
= wxClassInfo::sm_first
;
95 // Flag us for deletion
100 wxPluginLibrary::~wxPluginLibrary()
109 wxPluginLibrary
*wxPluginLibrary::RefLib()
111 wxCHECK_MSG( m_linkcount
> 0, NULL
,
112 _T("Library had been already deleted!") );
118 bool wxPluginLibrary::UnrefLib()
120 wxASSERT_MSG( m_objcount
== 0,
121 _T("Library unloaded before all objects were destroyed") );
123 if ( --m_linkcount
== 0 )
132 // ------------------------
134 // ------------------------
136 void wxPluginLibrary::UpdateClassInfo()
139 wxHashTable
*t
= wxClassInfo::sm_classTable
;
141 // FIXME: Below is simply a cut and paste specialisation of
142 // wxClassInfo::InitializeClasses. Once this stabilises,
143 // the two should probably be merged.
145 // Actually it's becoming questionable whether we should merge
146 // this info with the main ClassInfo tables since we can nearly
147 // handle this completely internally now and it does expose
148 // certain (minimal % user_stupidy) risks.
150 for(info
= m_after
; info
!= m_before
; info
= info
->m_next
)
152 if( info
->m_className
)
154 if( t
->Get(info
->m_className
) == 0 )
155 t
->Put(info
->m_className
, (wxObject
*)info
);
157 // Hash all the class names into a local table too so
158 // we can quickly find the entry they correspond to.
159 (*ms_classes
)[info
->m_className
] = this;
163 #if wxUSE_EXTENDED_RTTI == 0
164 for(info
= m_after
; info
!= m_before
; info
= info
->m_next
)
166 if( info
->m_baseClassName1
)
167 info
->m_baseInfo1
= (wxClassInfo
*)t
->Get(info
->m_baseClassName1
);
168 if( info
->m_baseClassName2
)
169 info
->m_baseInfo2
= (wxClassInfo
*)t
->Get(info
->m_baseClassName2
);
174 void wxPluginLibrary::RestoreClassInfo()
178 for(info
= m_after
; info
!= m_before
; info
= info
->m_next
)
180 wxClassInfo::sm_classTable
->Delete(info
->m_className
);
181 ms_classes
->erase(ms_classes
->find(info
->m_className
));
184 if( wxClassInfo::sm_first
== m_after
)
185 wxClassInfo::sm_first
= m_before
;
188 info
= wxClassInfo::sm_first
;
189 while( info
->m_next
&& info
->m_next
!= m_after
) info
= info
->m_next
;
191 wxASSERT_MSG( info
, _T("ClassInfo from wxPluginLibrary not found on purge"));
193 info
->m_next
= m_before
;
197 void wxPluginLibrary::RegisterModules()
199 // Plugin libraries might have wxModules, Register and initialise them if
202 // Note that these classes are NOT included in the reference counting since
203 // it's implicit that they will be unloaded if and when the last handle to
204 // the library is. We do have to keep a copy of the module's pointer
205 // though, as there is currently no way to Unregister it without it.
207 wxASSERT_MSG( m_linkcount
== 1,
208 _T("RegisterModules should only be called for the first load") );
210 for ( wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->m_next
)
212 if( info
->IsKindOf(CLASSINFO(wxModule
)) )
214 wxModule
*m
= wxDynamicCast(info
->CreateObject(), wxModule
);
216 wxASSERT_MSG( m
, _T("wxDynamicCast of wxModule failed") );
218 m_wxmodules
.push_back(m
);
219 wxModule::RegisterModule(m
);
223 // FIXME: Likewise this is (well was) very similar to InitializeModules()
225 for ( wxModuleList::iterator it
= m_wxmodules
.begin();
226 it
!= m_wxmodules
.end();
231 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
233 // XXX: Watch this, a different hash implementation might break it,
234 // a good hash implementation would let us fix it though.
236 // The name of the game is to remove any uninitialised modules and
237 // let the dtor Exit the rest on shutdown, (which we'll initiate
240 wxModuleList::iterator oldNode
= m_wxmodules
.end();
243 if( oldNode
!= m_wxmodules
.end() )
244 m_wxmodules
.erase(oldNode
);
245 wxModule::UnregisterModule( *it
);
247 } while( it
!= m_wxmodules
.end() );
249 --m_linkcount
; // Flag us for deletion
255 void wxPluginLibrary::UnregisterModules()
257 wxModuleList::iterator it
;
259 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
262 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
263 wxModule::UnregisterModule( *it
);
265 WX_CLEAR_LIST(wxModuleList
, m_wxmodules
);
269 // ---------------------------------------------------------------------------
271 // ---------------------------------------------------------------------------
273 wxDLManifest
* wxPluginManager::ms_manifest
= NULL
;
275 // ------------------------
277 // ------------------------
280 wxPluginManager::LoadLibrary(const wxString
&libname
, int flags
)
282 wxString
realname(libname
);
284 if( !(flags
& wxDL_VERBATIM
) )
285 realname
+= wxDynamicLibrary::GetDllExt();
287 wxPluginLibrary
*entry
;
289 if ( flags
& wxDL_NOSHARE
)
295 entry
= FindByName(realname
);
300 wxLogTrace(_T("dll"),
301 _T("LoadLibrary(%s): already loaded."), realname
.c_str());
307 entry
= new wxPluginLibrary( libname
, flags
);
309 if ( entry
->IsLoaded() )
311 (*ms_manifest
)[realname
] = entry
;
313 wxLogTrace(_T("dll"),
314 _T("LoadLibrary(%s): loaded ok."), realname
.c_str());
319 wxLogTrace(_T("dll"),
320 _T("LoadLibrary(%s): failed to load."), realname
.c_str());
322 // we have created entry just above
323 if ( !entry
->UnrefLib() )
325 // ... so UnrefLib() is supposed to delete it
326 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
336 bool wxPluginManager::UnloadLibrary(const wxString
& libname
)
338 wxString realname
= libname
;
340 wxPluginLibrary
*entry
= FindByName(realname
);
344 realname
+= wxDynamicLibrary::GetDllExt();
346 entry
= FindByName(realname
);
351 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
357 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname
.c_str());
359 if ( !entry
->UnrefLib() )
361 // not really unloaded yet
365 ms_manifest
->erase(ms_manifest
->find(realname
));
370 // ------------------------
371 // Class implementation
372 // ------------------------
374 bool wxPluginManager::Load(const wxString
&libname
, int flags
)
376 m_entry
= wxPluginManager::LoadLibrary(libname
, flags
);
381 void wxPluginManager::Unload()
383 wxCHECK_RET( m_entry
, _T("unloading an invalid wxPluginManager?") );
385 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
386 i
!= ms_manifest
->end();
389 if ( i
->second
== m_entry
)
391 ms_manifest
->erase(i
);
403 #if WXWIN_COMPATIBILITY_2_2
405 wxPluginLibrary
*wxPluginManager::GetObjectFromHandle(wxDllType handle
)
407 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
408 i
!= ms_manifest
->end();
411 wxPluginLibrary
* const lib
= i
->second
;
413 if ( lib
->GetLibHandle() == handle
)
420 // ---------------------------------------------------------------------------
421 // wxDllLoader (all these methods are static)
422 // ---------------------------------------------------------------------------
425 wxDllType
wxDllLoader::LoadLibrary(const wxString
&name
, bool *success
)
427 wxPluginLibrary
*p
= wxPluginManager::LoadLibrary
430 wxDL_DEFAULT
| wxDL_VERBATIM
| wxDL_NOSHARE
434 *success
= p
!= NULL
;
436 return p
? p
->GetLibHandle() : 0;
439 void wxDllLoader::UnloadLibrary(wxDllType handle
)
441 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(handle
);
443 wxCHECK_RET( p
, _T("Unloading a library not loaded with wxDllLoader?") );
449 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
)
451 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(dllHandle
);
455 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
463 return p
->GetSymbol(name
, success
);
467 // ---------------------------------------------------------------------------
469 // ---------------------------------------------------------------------------
471 wxLibraries wxTheLibraries
;
473 // ============================================================================
475 // ============================================================================
477 // construct the full name from the base shared object name: adds a .dll
478 // suffix under Windows or .so under Unix
479 static wxString
ConstructLibraryName(const wxString
& basename
)
482 fullname
<< basename
<< wxDllLoader::GetDllExt();
487 // ---------------------------------------------------------------------------
488 // wxLibrary (one instance per dynamic library)
489 // ---------------------------------------------------------------------------
491 wxLibrary::wxLibrary(wxDllType handle
)
493 typedef wxClassInfo
*(*t_get_first
)(void);
494 t_get_first get_first
;
498 // Some system may use a local heap for library.
499 get_first
= (t_get_first
)GetSymbol(_T("wxGetClassFirst"));
500 // It is a wxWindows DLL.
502 PrepareClasses(get_first());
505 wxLibrary::~wxLibrary()
509 wxDllLoader::UnloadLibrary(m_handle
);
513 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
515 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
520 return info
->CreateObject();
523 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
525 // Index all class infos by their class name
526 wxClassInfo
*info
= first
;
529 if (info
->m_className
)
530 classTable
.Put(info
->m_className
, (wxObject
*)info
);
534 // Set base pointers for each wxClassInfo
538 if (info
->GetBaseClassName1())
539 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
540 if (info
->GetBaseClassName2())
541 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
546 void *wxLibrary::GetSymbol(const wxString
& symbname
)
548 return wxDllLoader::GetSymbol(m_handle
, symbname
);
552 // ---------------------------------------------------------------------------
553 // wxLibraries (only one instance should normally exist)
554 // ---------------------------------------------------------------------------
556 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING
)
560 wxLibraries::~wxLibraries()
562 wxNode
*node
= m_loaded
.First();
565 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
572 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
575 wxClassInfo
*old_sm_first
;
576 wxNode
*node
= m_loaded
.Find(name
.GetData());
579 return ((wxLibrary
*)node
->Data());
581 // If DLL shares data, this is necessary.
582 old_sm_first
= wxClassInfo::sm_first
;
583 wxClassInfo::sm_first
= NULL
;
585 wxString libname
= ConstructLibraryName(name
);
587 bool success
= FALSE
;
588 wxDllType handle
= wxDllLoader::LoadLibrary(libname
, &success
);
591 lib
= new wxLibrary(handle
);
592 wxClassInfo::sm_first
= old_sm_first
;
594 m_loaded
.Append(name
.GetData(), lib
);
601 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
603 wxNode
*node
= m_loaded
.First();
607 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
616 #endif // WXWIN_COMPATIBILITY_2_2
619 #endif // wxUSE_DYNAMIC_LOADER