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
;
94 // Flag us for deletion
99 wxPluginLibrary::~wxPluginLibrary()
107 wxPluginLibrary
*wxPluginLibrary::RefLib()
109 wxCHECK_MSG( m_linkcount
> 0, NULL
,
110 _T("Library had been already deleted!") );
116 bool wxPluginLibrary::UnrefLib()
118 wxASSERT_MSG( m_objcount
== 0,
119 _T("Library unloaded before all objects were destroyed") );
121 if ( --m_linkcount
== 0 )
130 // ------------------------
132 // ------------------------
134 void wxPluginLibrary::RegisterModules()
136 // Plugin libraries might have wxModules, Register and initialise them if
139 // Note that these classes are NOT included in the reference counting since
140 // it's implicit that they will be unloaded if and when the last handle to
141 // the library is. We do have to keep a copy of the module's pointer
142 // though, as there is currently no way to Unregister it without it.
144 wxASSERT_MSG( m_linkcount
== 1,
145 _T("RegisterModules should only be called for the first load") );
147 for ( wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->m_next
)
149 if( info
->IsKindOf(CLASSINFO(wxModule
)) )
151 wxModule
*m
= wxDynamicCast(info
->CreateObject(), wxModule
);
153 wxASSERT_MSG( m
, _T("wxDynamicCast of wxModule failed") );
155 m_wxmodules
.push_back(m
);
156 wxModule::RegisterModule(m
);
160 // FIXME: Likewise this is (well was) very similar to InitializeModules()
162 for ( wxModuleList::iterator it
= m_wxmodules
.begin();
163 it
!= m_wxmodules
.end();
168 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
170 // XXX: Watch this, a different hash implementation might break it,
171 // a good hash implementation would let us fix it though.
173 // The name of the game is to remove any uninitialised modules and
174 // let the dtor Exit the rest on shutdown, (which we'll initiate
177 wxModuleList::iterator oldNode
= m_wxmodules
.end();
180 if( oldNode
!= m_wxmodules
.end() )
181 m_wxmodules
.erase(oldNode
);
182 wxModule::UnregisterModule( *it
);
184 } while( it
!= m_wxmodules
.end() );
186 --m_linkcount
; // Flag us for deletion
192 void wxPluginLibrary::UnregisterModules()
194 wxModuleList::iterator it
;
196 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
199 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
200 wxModule::UnregisterModule( *it
);
202 WX_CLEAR_LIST(wxModuleList
, m_wxmodules
);
206 // ---------------------------------------------------------------------------
208 // ---------------------------------------------------------------------------
210 wxDLManifest
* wxPluginManager::ms_manifest
= NULL
;
212 // ------------------------
214 // ------------------------
217 wxPluginManager::LoadLibrary(const wxString
&libname
, int flags
)
219 wxString
realname(libname
);
221 if( !(flags
& wxDL_VERBATIM
) )
222 realname
+= wxDynamicLibrary::GetDllExt();
224 wxPluginLibrary
*entry
;
226 if ( flags
& wxDL_NOSHARE
)
232 entry
= FindByName(realname
);
237 wxLogTrace(_T("dll"),
238 _T("LoadLibrary(%s): already loaded."), realname
.c_str());
244 entry
= new wxPluginLibrary( libname
, flags
);
246 if ( entry
->IsLoaded() )
248 (*ms_manifest
)[realname
] = entry
;
250 wxLogTrace(_T("dll"),
251 _T("LoadLibrary(%s): loaded ok."), realname
.c_str());
256 wxLogTrace(_T("dll"),
257 _T("LoadLibrary(%s): failed to load."), realname
.c_str());
259 // we have created entry just above
260 if ( !entry
->UnrefLib() )
262 // ... so UnrefLib() is supposed to delete it
263 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
273 bool wxPluginManager::UnloadLibrary(const wxString
& libname
)
275 wxString realname
= libname
;
277 wxPluginLibrary
*entry
= FindByName(realname
);
281 realname
+= wxDynamicLibrary::GetDllExt();
283 entry
= FindByName(realname
);
288 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
294 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname
.c_str());
296 if ( !entry
->UnrefLib() )
298 // not really unloaded yet
302 ms_manifest
->erase(ms_manifest
->find(realname
));
307 // ------------------------
308 // Class implementation
309 // ------------------------
311 bool wxPluginManager::Load(const wxString
&libname
, int flags
)
313 m_entry
= wxPluginManager::LoadLibrary(libname
, flags
);
318 void wxPluginManager::Unload()
320 wxCHECK_RET( m_entry
, _T("unloading an invalid wxPluginManager?") );
322 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
323 i
!= ms_manifest
->end();
326 if ( i
->second
== m_entry
)
328 ms_manifest
->erase(i
);
340 #if WXWIN_COMPATIBILITY_2_2
342 wxPluginLibrary
*wxPluginManager::GetObjectFromHandle(wxDllType handle
)
344 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
345 i
!= ms_manifest
->end();
348 wxPluginLibrary
* const lib
= i
->second
;
350 if ( lib
->GetLibHandle() == handle
)
357 // ---------------------------------------------------------------------------
358 // wxDllLoader (all these methods are static)
359 // ---------------------------------------------------------------------------
362 wxDllType
wxDllLoader::LoadLibrary(const wxString
&name
, bool *success
)
364 wxPluginLibrary
*p
= wxPluginManager::LoadLibrary
367 wxDL_DEFAULT
| wxDL_VERBATIM
| wxDL_NOSHARE
371 *success
= p
!= NULL
;
373 return p
? p
->GetLibHandle() : 0;
376 void wxDllLoader::UnloadLibrary(wxDllType handle
)
378 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(handle
);
380 wxCHECK_RET( p
, _T("Unloading a library not loaded with wxDllLoader?") );
386 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
)
388 wxPluginLibrary
*p
= wxPluginManager::GetObjectFromHandle(dllHandle
);
392 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
400 return p
->GetSymbol(name
, success
);
404 // ---------------------------------------------------------------------------
406 // ---------------------------------------------------------------------------
408 wxLibraries wxTheLibraries
;
410 // ============================================================================
412 // ============================================================================
414 // construct the full name from the base shared object name: adds a .dll
415 // suffix under Windows or .so under Unix
416 static wxString
ConstructLibraryName(const wxString
& basename
)
419 fullname
<< basename
<< wxDllLoader::GetDllExt();
424 // ---------------------------------------------------------------------------
425 // wxLibrary (one instance per dynamic library)
426 // ---------------------------------------------------------------------------
428 wxLibrary::wxLibrary(wxDllType handle
)
430 typedef wxClassInfo
*(*t_get_first
)(void);
431 t_get_first get_first
;
435 // Some system may use a local heap for library.
436 get_first
= (t_get_first
)GetSymbol(_T("wxGetClassFirst"));
437 // It is a wxWindows DLL.
439 PrepareClasses(get_first());
442 wxLibrary::~wxLibrary()
446 wxDllLoader::UnloadLibrary(m_handle
);
450 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
452 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
457 return info
->CreateObject();
460 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
462 // Index all class infos by their class name
463 wxClassInfo
*info
= first
;
466 if (info
->m_className
)
467 classTable
.Put(info
->m_className
, (wxObject
*)info
);
471 #if !wxUSE_EXTENDED_RTTI
472 // Set base pointers for each wxClassInfo
476 if (info
->GetBaseClassName1())
477 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
478 if (info
->GetBaseClassName2())
479 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
485 void *wxLibrary::GetSymbol(const wxString
& symbname
)
487 return wxDllLoader::GetSymbol(m_handle
, symbname
);
491 // ---------------------------------------------------------------------------
492 // wxLibraries (only one instance should normally exist)
493 // ---------------------------------------------------------------------------
495 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING
)
499 wxLibraries::~wxLibraries()
501 wxNode
*node
= m_loaded
.First();
504 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
511 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
514 wxClassInfo
*old_sm_first
;
515 wxNode
*node
= m_loaded
.Find(name
.GetData());
518 return ((wxLibrary
*)node
->Data());
520 // If DLL shares data, this is necessary.
521 old_sm_first
= wxClassInfo::sm_first
;
522 wxClassInfo::sm_first
= NULL
;
524 wxString libname
= ConstructLibraryName(name
);
526 bool success
= FALSE
;
527 wxDllType handle
= wxDllLoader::LoadLibrary(libname
, &success
);
530 lib
= new wxLibrary(handle
);
531 wxClassInfo::sm_first
= old_sm_first
;
533 m_loaded
.Append(name
.GetData(), lib
);
540 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
542 wxNode
*node
= m_loaded
.First();
546 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
555 #endif // WXWIN_COMPATIBILITY_2_2
558 #endif // wxUSE_DYNAMIC_LOADER