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
;
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::UpdateClasses()
138 for (wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->m_next
)
140 if( info
->m_className
)
142 // Hash all the class names into a local table too so
143 // we can quickly find the entry they correspond to.
144 (*ms_classes
)[info
->m_className
] = this;
149 void wxPluginLibrary::RestoreClasses()
151 for(wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->m_next
)
153 ms_classes
->erase(ms_classes
->find(info
->m_className
));
157 void wxPluginLibrary::RegisterModules()
159 // Plugin libraries might have wxModules, Register and initialise them if
162 // Note that these classes are NOT included in the reference counting since
163 // it's implicit that they will be unloaded if and when the last handle to
164 // the library is. We do have to keep a copy of the module's pointer
165 // though, as there is currently no way to Unregister it without it.
167 wxASSERT_MSG( m_linkcount
== 1,
168 _T("RegisterModules should only be called for the first load") );
170 for ( wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->m_next
)
172 if( info
->IsKindOf(CLASSINFO(wxModule
)) )
174 wxModule
*m
= wxDynamicCast(info
->CreateObject(), wxModule
);
176 wxASSERT_MSG( m
, _T("wxDynamicCast of wxModule failed") );
178 m_wxmodules
.push_back(m
);
179 wxModule::RegisterModule(m
);
183 // FIXME: Likewise this is (well was) very similar to InitializeModules()
185 for ( wxModuleList::iterator it
= m_wxmodules
.begin();
186 it
!= m_wxmodules
.end();
191 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
193 // XXX: Watch this, a different hash implementation might break it,
194 // a good hash implementation would let us fix it though.
196 // The name of the game is to remove any uninitialised modules and
197 // let the dtor Exit the rest on shutdown, (which we'll initiate
200 wxModuleList::iterator oldNode
= m_wxmodules
.end();
203 if( oldNode
!= m_wxmodules
.end() )
204 m_wxmodules
.erase(oldNode
);
205 wxModule::UnregisterModule( *it
);
207 } while( it
!= m_wxmodules
.end() );
209 --m_linkcount
; // Flag us for deletion
215 void wxPluginLibrary::UnregisterModules()
217 wxModuleList::iterator it
;
219 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
222 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
223 wxModule::UnregisterModule( *it
);
225 WX_CLEAR_LIST(wxModuleList
, m_wxmodules
);
229 // ---------------------------------------------------------------------------
231 // ---------------------------------------------------------------------------
233 wxDLManifest
* wxPluginManager::ms_manifest
= NULL
;
235 // ------------------------
237 // ------------------------
240 wxPluginManager::LoadLibrary(const wxString
&libname
, int flags
)
242 wxString
realname(libname
);
244 if( !(flags
& wxDL_VERBATIM
) )
245 realname
+= wxDynamicLibrary::GetDllExt();
247 wxPluginLibrary
*entry
;
249 if ( flags
& wxDL_NOSHARE
)
255 entry
= FindByName(realname
);
260 wxLogTrace(_T("dll"),
261 _T("LoadLibrary(%s): already loaded."), realname
.c_str());
267 entry
= new wxPluginLibrary( libname
, flags
);
269 if ( entry
->IsLoaded() )
271 (*ms_manifest
)[realname
] = entry
;
273 wxLogTrace(_T("dll"),
274 _T("LoadLibrary(%s): loaded ok."), realname
.c_str());
279 wxLogTrace(_T("dll"),
280 _T("LoadLibrary(%s): failed to load."), realname
.c_str());
282 // we have created entry just above
283 if ( !entry
->UnrefLib() )
285 // ... so UnrefLib() is supposed to delete it
286 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
296 bool wxPluginManager::UnloadLibrary(const wxString
& libname
)
298 wxString realname
= libname
;
300 wxPluginLibrary
*entry
= FindByName(realname
);
304 realname
+= wxDynamicLibrary::GetDllExt();
306 entry
= FindByName(realname
);
311 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
317 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname
.c_str());
319 if ( !entry
->UnrefLib() )
321 // not really unloaded yet
325 ms_manifest
->erase(ms_manifest
->find(realname
));
330 // ------------------------
331 // Class implementation
332 // ------------------------
334 bool wxPluginManager::Load(const wxString
&libname
, int flags
)
336 m_entry
= wxPluginManager::LoadLibrary(libname
, flags
);
341 void wxPluginManager::Unload()
343 wxCHECK_RET( m_entry
, _T("unloading an invalid wxPluginManager?") );
345 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
346 i
!= ms_manifest
->end();
349 if ( i
->second
== m_entry
)
351 ms_manifest
->erase(i
);
363 #if WXWIN_COMPATIBILITY_2_2
365 wxPluginLibrary
*wxPluginManager::GetObjectFromHandle(wxDllType handle
)
367 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
368 i
!= ms_manifest
->end();
371 wxPluginLibrary
* const lib
= i
->second
;
373 if ( lib
->GetLibHandle() == handle
)
380 // ---------------------------------------------------------------------------
381 // wxDllLoader (all these methods are static)
382 // ---------------------------------------------------------------------------
385 wxDllType
wxDllLoader::LoadLibrary(const wxString
&name
, bool *success
)
387 wxPluginLibrary
*p
= wxPluginManager::LoadLibrary
390 wxDL_DEFAULT
| wxDL_VERBATIM
| wxDL_NOSHARE
394 *success
= p
!= NULL
;
396 return p
? p
->GetLibHandle() : 0;
399 void wxDllLoader::UnloadLibrary(wxDllType handle
)
401 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(handle
);
403 wxCHECK_RET( p
, _T("Unloading a library not loaded with wxDllLoader?") );
409 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
)
411 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(dllHandle
);
415 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
423 return p
->GetSymbol(name
, success
);
427 // ---------------------------------------------------------------------------
429 // ---------------------------------------------------------------------------
431 wxLibraries wxTheLibraries
;
433 // ============================================================================
435 // ============================================================================
437 // construct the full name from the base shared object name: adds a .dll
438 // suffix under Windows or .so under Unix
439 static wxString
ConstructLibraryName(const wxString
& basename
)
442 fullname
<< basename
<< wxDllLoader::GetDllExt();
447 // ---------------------------------------------------------------------------
448 // wxLibrary (one instance per dynamic library)
449 // ---------------------------------------------------------------------------
451 wxLibrary::wxLibrary(wxDllType handle
)
453 typedef wxClassInfo
*(*t_get_first
)(void);
454 t_get_first get_first
;
458 // Some system may use a local heap for library.
459 get_first
= (t_get_first
)GetSymbol(_T("wxGetClassFirst"));
460 // It is a wxWidgets DLL.
462 PrepareClasses(get_first());
465 wxLibrary::~wxLibrary()
469 wxDllLoader::UnloadLibrary(m_handle
);
473 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
475 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
480 return info
->CreateObject();
483 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
485 // Index all class infos by their class name
486 wxClassInfo
*info
= first
;
489 if (info
->m_className
)
490 classTable
.Put(info
->m_className
, (wxObject
*)info
);
494 #if !wxUSE_EXTENDED_RTTI
495 // Set base pointers for each wxClassInfo
499 if (info
->GetBaseClassName1())
500 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
501 if (info
->GetBaseClassName2())
502 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
508 void *wxLibrary::GetSymbol(const wxString
& symbname
)
510 return wxDllLoader::GetSymbol(m_handle
, symbname
);
514 // ---------------------------------------------------------------------------
515 // wxLibraries (only one instance should normally exist)
516 // ---------------------------------------------------------------------------
518 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING
)
522 wxLibraries::~wxLibraries()
524 wxNode
*node
= m_loaded
.First();
527 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
534 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
537 wxClassInfo
*old_sm_first
;
538 wxNode
*node
= m_loaded
.Find(name
.GetData());
541 return ((wxLibrary
*)node
->Data());
543 // If DLL shares data, this is necessary.
544 old_sm_first
= wxClassInfo::sm_first
;
545 wxClassInfo::sm_first
= NULL
;
547 wxString libname
= ConstructLibraryName(name
);
549 bool success
= FALSE
;
550 wxDllType handle
= wxDllLoader::LoadLibrary(libname
, &success
);
553 lib
= new wxLibrary(handle
);
554 wxClassInfo::sm_first
= old_sm_first
;
556 m_loaded
.Append(name
.GetData(), lib
);
563 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
565 wxNode
*node
= m_loaded
.First();
569 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
578 #endif // WXWIN_COMPATIBILITY_2_2
581 #endif // wxUSE_DYNAMIC_LOADER