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 /////////////////////////////////////////////////////////////////////////////
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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;
164 void wxPluginLibrary::RestoreClassInfo()
168 for(info
= m_after
; info
!= m_before
; info
= info
->m_next
)
170 wxClassInfo::sm_classTable
->Delete(info
->m_className
);
171 ms_classes
->erase(ms_classes
->find(info
->m_className
));
174 if( wxClassInfo::sm_first
== m_after
)
175 wxClassInfo::sm_first
= m_before
;
178 info
= wxClassInfo::sm_first
;
179 while( info
->m_next
&& info
->m_next
!= m_after
) info
= info
->m_next
;
181 wxASSERT_MSG( info
, _T("ClassInfo from wxPluginLibrary not found on purge"));
183 info
->m_next
= m_before
;
187 void wxPluginLibrary::RegisterModules()
189 // Plugin libraries might have wxModules, Register and initialise them if
192 // Note that these classes are NOT included in the reference counting since
193 // it's implicit that they will be unloaded if and when the last handle to
194 // the library is. We do have to keep a copy of the module's pointer
195 // though, as there is currently no way to Unregister it without it.
197 wxASSERT_MSG( m_linkcount
== 1,
198 _T("RegisterModules should only be called for the first load") );
200 for ( wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->m_next
)
202 if( info
->IsKindOf(CLASSINFO(wxModule
)) )
204 wxModule
*m
= wxDynamicCast(info
->CreateObject(), wxModule
);
206 wxASSERT_MSG( m
, _T("wxDynamicCast of wxModule failed") );
208 m_wxmodules
.push_back(m
);
209 wxModule::RegisterModule(m
);
213 // FIXME: Likewise this is (well was) very similar to InitializeModules()
215 for ( wxModuleList::iterator it
= m_wxmodules
.begin();
216 it
!= m_wxmodules
.end();
221 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
223 // XXX: Watch this, a different hash implementation might break it,
224 // a good hash implementation would let us fix it though.
226 // The name of the game is to remove any uninitialised modules and
227 // let the dtor Exit the rest on shutdown, (which we'll initiate
230 wxModuleList::iterator oldNode
= m_wxmodules
.end();
233 if( oldNode
!= m_wxmodules
.end() )
234 m_wxmodules
.erase(oldNode
);
235 wxModule::UnregisterModule( *it
);
237 } while( it
!= m_wxmodules
.end() );
239 --m_linkcount
; // Flag us for deletion
245 void wxPluginLibrary::UnregisterModules()
247 wxModuleList::iterator it
;
249 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
252 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
253 wxModule::UnregisterModule( *it
);
255 WX_CLEAR_LIST(wxModuleList
, m_wxmodules
);
259 // ---------------------------------------------------------------------------
261 // ---------------------------------------------------------------------------
263 wxDLManifest
* wxPluginManager::ms_manifest
= NULL
;
265 // ------------------------
267 // ------------------------
270 wxPluginManager::LoadLibrary(const wxString
&libname
, int flags
)
272 wxString
realname(libname
);
274 if( !(flags
& wxDL_VERBATIM
) )
275 realname
+= wxDynamicLibrary::GetDllExt();
277 wxPluginLibrary
*entry
;
279 if ( flags
& wxDL_NOSHARE
)
285 entry
= FindByName(realname
);
290 wxLogTrace(_T("dll"),
291 _T("LoadLibrary(%s): already loaded."), realname
.c_str());
297 entry
= new wxPluginLibrary( libname
, flags
);
299 if ( entry
->IsLoaded() )
301 (*ms_manifest
)[realname
] = entry
;
303 wxLogTrace(_T("dll"),
304 _T("LoadLibrary(%s): loaded ok."), realname
.c_str());
309 wxLogTrace(_T("dll"),
310 _T("LoadLibrary(%s): failed to load."), realname
.c_str());
312 // we have created entry just above
313 if ( !entry
->UnrefLib() )
315 // ... so UnrefLib() is supposed to delete it
316 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
326 bool wxPluginManager::UnloadLibrary(const wxString
& libname
)
328 wxString realname
= libname
;
330 wxPluginLibrary
*entry
= FindByName(realname
);
334 realname
+= wxDynamicLibrary::GetDllExt();
336 entry
= FindByName(realname
);
341 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
347 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname
.c_str());
349 if ( !entry
->UnrefLib() )
351 // not really unloaded yet
355 ms_manifest
->erase(ms_manifest
->find(realname
));
360 // ------------------------
361 // Class implementation
362 // ------------------------
364 bool wxPluginManager::Load(const wxString
&libname
, int flags
)
366 m_entry
= wxPluginManager::LoadLibrary(libname
, flags
);
371 void wxPluginManager::Unload()
373 wxCHECK_RET( m_entry
, _T("unloading an invalid wxPluginManager?") );
375 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
376 i
!= ms_manifest
->end();
379 if ( i
->second
== m_entry
)
381 ms_manifest
->erase(i
);
393 #if WXWIN_COMPATIBILITY_2_2
395 wxPluginLibrary
*wxPluginManager::GetObjectFromHandle(wxDllType handle
)
397 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
398 i
!= ms_manifest
->end();
401 wxPluginLibrary
* const lib
= i
->second
;
403 if ( lib
->GetLibHandle() == handle
)
410 // ---------------------------------------------------------------------------
411 // wxDllLoader (all these methods are static)
412 // ---------------------------------------------------------------------------
415 wxDllType
wxDllLoader::LoadLibrary(const wxString
&name
, bool *success
)
417 wxPluginLibrary
*p
= wxPluginManager::LoadLibrary
420 wxDL_DEFAULT
| wxDL_VERBATIM
| wxDL_NOSHARE
424 *success
= p
!= NULL
;
426 return p
? p
->GetLibHandle() : 0;
429 void wxDllLoader::UnloadLibrary(wxDllType handle
)
431 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(handle
);
433 wxCHECK_RET( p
, _T("Unloading a library not loaded with wxDllLoader?") );
439 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
)
441 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(dllHandle
);
445 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
453 return p
->GetSymbol(name
, success
);
457 // ---------------------------------------------------------------------------
459 // ---------------------------------------------------------------------------
461 wxLibraries wxTheLibraries
;
463 // ============================================================================
465 // ============================================================================
467 // construct the full name from the base shared object name: adds a .dll
468 // suffix under Windows or .so under Unix
469 static wxString
ConstructLibraryName(const wxString
& basename
)
472 fullname
<< basename
<< wxDllLoader::GetDllExt();
477 // ---------------------------------------------------------------------------
478 // wxLibrary (one instance per dynamic library)
479 // ---------------------------------------------------------------------------
481 wxLibrary::wxLibrary(wxDllType handle
)
483 typedef wxClassInfo
*(*t_get_first
)(void);
484 t_get_first get_first
;
488 // Some system may use a local heap for library.
489 get_first
= (t_get_first
)GetSymbol(_T("wxGetClassFirst"));
490 // It is a wxWindows DLL.
492 PrepareClasses(get_first());
495 wxLibrary::~wxLibrary()
499 wxDllLoader::UnloadLibrary(m_handle
);
503 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
505 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
510 return info
->CreateObject();
513 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
515 // Index all class infos by their class name
516 wxClassInfo
*info
= first
;
519 if (info
->m_className
)
520 classTable
.Put(info
->m_className
, (wxObject
*)info
);
524 #if !wxUSE_EXTENDED_RTTI
525 // Set base pointers for each wxClassInfo
529 if (info
->GetBaseClassName1())
530 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
531 if (info
->GetBaseClassName2())
532 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
538 void *wxLibrary::GetSymbol(const wxString
& symbname
)
540 return wxDllLoader::GetSymbol(m_handle
, symbname
);
544 // ---------------------------------------------------------------------------
545 // wxLibraries (only one instance should normally exist)
546 // ---------------------------------------------------------------------------
548 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING
)
552 wxLibraries::~wxLibraries()
554 wxNode
*node
= m_loaded
.First();
557 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
564 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
567 wxClassInfo
*old_sm_first
;
568 wxNode
*node
= m_loaded
.Find(name
.GetData());
571 return ((wxLibrary
*)node
->Data());
573 // If DLL shares data, this is necessary.
574 old_sm_first
= wxClassInfo::sm_first
;
575 wxClassInfo::sm_first
= NULL
;
577 wxString libname
= ConstructLibraryName(name
);
579 bool success
= FALSE
;
580 wxDllType handle
= wxDllLoader::LoadLibrary(libname
, &success
);
583 lib
= new wxLibrary(handle
);
584 wxClassInfo::sm_first
= old_sm_first
;
586 m_loaded
.Append(name
.GetData(), lib
);
593 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
595 wxNode
*node
= m_loaded
.First();
599 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
608 #endif // WXWIN_COMPATIBILITY_2_2
611 #endif // wxUSE_DYNAMIC_LOADER