]>
Commit | Line | Data |
---|---|---|
0b9ab0bd RL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dynload.cpp | |
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) | |
6 | // Modified by: | |
7 | // Created: 03/12/01 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 2001 Ron Lee <ron@debian.org> | |
55d99c7a | 10 | // Licence: wxWindows licence |
0b9ab0bd RL |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
13 | #ifdef __GNUG__ | |
14 | #pragma implementation "dynload.h" | |
15 | #endif | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
409bd320 | 24 | #pragma hdrstop |
0b9ab0bd RL |
25 | #endif |
26 | ||
27 | #if wxUSE_DYNAMIC_LOADER | |
28 | ||
29 | #ifdef __WINDOWS__ | |
409bd320 | 30 | #include "wx/msw/private.h" |
0b9ab0bd RL |
31 | #endif |
32 | ||
33 | #ifndef WX_PRECOMP | |
409bd320 VZ |
34 | #include "wx/log.h" |
35 | #include "wx/intl.h" | |
2b5f62a0 | 36 | #include "wx/hash.h" |
0b9ab0bd RL |
37 | #endif |
38 | ||
409bd320 | 39 | #include "wx/filename.h" // for SplitPath() |
401eb3de | 40 | #include "wx/strconv.h" |
0b9ab0bd | 41 | |
409bd320 | 42 | #include "wx/dynload.h" |
dabd1377 | 43 | #include "wx/module.h" |
0b9ab0bd | 44 | |
f9ee64b1 GD |
45 | #if defined(__DARWIN__) |
46 | /* Porting notes: | |
47 | * The dlopen port is a port from dl_next.xs by Anno Siegel. | |
48 | * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess. | |
49 | * The method used here is just to supply the sun style dlopen etc. | |
50 | * functions in terms of Darwin NS*. | |
51 | */ | |
52 | void *dlopen(const char *path, int mode /* mode is ignored */); | |
53 | void *dlsym(void *handle, const char *symbol); | |
54 | int dlclose(void *handle); | |
55 | const char *dlerror(void); | |
56 | #endif | |
57 | ||
0b9ab0bd RL |
58 | // ============================================================================ |
59 | // implementation | |
60 | // ============================================================================ | |
61 | ||
62 | // --------------------------------------------------------------------------- | |
4f89dbc4 | 63 | // wxDynamicLibrary |
0b9ab0bd RL |
64 | // --------------------------------------------------------------------------- |
65 | ||
4f89dbc4 RL |
66 | //FIXME: This class isn't really common at all, it should be moved into |
67 | // platform dependent files. | |
0b9ab0bd RL |
68 | |
69 | #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__) | |
478e0341 | 70 | const wxChar *wxDynamicLibrary::ms_dllext = _T(".dll"); |
0b9ab0bd | 71 | #elif defined(__UNIX__) |
d0fcccc4 | 72 | #if defined(__HPUX__) |
478e0341 | 73 | const wxChar *wxDynamicLibrary::ms_dllext = _T(".sl"); |
d0fcccc4 | 74 | #else |
478e0341 | 75 | const wxChar *wxDynamicLibrary::ms_dllext = _T(".so"); |
d0fcccc4 | 76 | #endif |
0b9ab0bd RL |
77 | #endif |
78 | ||
4f89dbc4 | 79 | wxDllType wxDynamicLibrary::GetProgramHandle() |
0b9ab0bd RL |
80 | { |
81 | #if defined( HAVE_DLOPEN ) && !defined(__EMX__) | |
4f89dbc4 | 82 | return dlopen(0, RTLD_LAZY); |
0b9ab0bd | 83 | #elif defined (HAVE_SHL_LOAD) |
4f89dbc4 | 84 | return PROG_HANDLE; |
0b9ab0bd RL |
85 | #else |
86 | wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2")); | |
87 | return 0; | |
88 | #endif | |
89 | } | |
90 | ||
23213f18 | 91 | bool wxDynamicLibrary::Load(wxString libname, int flags) |
0b9ab0bd | 92 | { |
4f89dbc4 RL |
93 | wxASSERT_MSG(m_handle == 0, _T("Library already loaded.")); |
94 | ||
409bd320 VZ |
95 | // add the proper extension for the DLL ourselves unless told not to |
96 | if ( !(flags & wxDL_VERBATIM) ) | |
97 | { | |
98 | // and also check that the libname doesn't already have it | |
99 | wxString ext; | |
100 | wxFileName::SplitPath(libname, NULL, NULL, &ext); | |
101 | if ( ext.empty() ) | |
102 | { | |
103 | libname += GetDllExt(); | |
104 | } | |
105 | } | |
0b9ab0bd | 106 | |
ee1107cf VZ |
107 | // different ways to load a shared library |
108 | // | |
109 | // FIXME: should go to the platform-specific files! | |
f9ee64b1 | 110 | #if defined(__WXMAC__) && !defined(__DARWIN__) |
0b9ab0bd RL |
111 | FSSpec myFSSpec; |
112 | Ptr myMainAddr; | |
113 | Str255 myErrName; | |
114 | ||
115 | wxMacFilename2FSSpec( libname , &myFSSpec ); | |
116 | ||
117 | if( GetDiskFragment( &myFSSpec, | |
118 | 0, | |
119 | kCFragGoesToEOF, | |
120 | "\p", | |
121 | kPrivateCFragCopy, | |
4f89dbc4 | 122 | &m_handle, |
0b9ab0bd RL |
123 | &myMainAddr, |
124 | myErrName ) != noErr ) | |
125 | { | |
126 | p2cstr( myErrName ); | |
127 | wxLogSysError( _("Failed to load shared library '%s' Error '%s'"), | |
128 | libname.c_str(), | |
129 | (char*)myErrName ); | |
4f89dbc4 | 130 | m_handle = 0; |
0b9ab0bd RL |
131 | } |
132 | ||
133 | #elif defined(__WXPM__) || defined(__EMX__) | |
4f89dbc4 | 134 | char err[256] = ""; |
c822ad5a | 135 | DosLoadModule(err, sizeof(err), libname.c_str(), &m_handle); |
4f89dbc4 | 136 | |
f9ee64b1 | 137 | #elif defined(HAVE_DLOPEN) || defined(__DARWIN__) |
4f89dbc4 | 138 | |
f9ee64b1 | 139 | #if defined(__VMS) || defined(__DARWIN__) |
4f89dbc4 | 140 | m_handle = dlopen(libname.c_str(), 0); // The second parameter is ignored |
ee1107cf | 141 | #else // !__VMS && !__DARWIN__ |
4f89dbc4 RL |
142 | int rtldFlags = 0; |
143 | ||
ee1107cf | 144 | if ( flags & wxDL_LAZY ) |
4f89dbc4 RL |
145 | { |
146 | wxASSERT_MSG( (flags & wxDL_NOW) == 0, | |
147 | _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") ); | |
ee1107cf | 148 | #ifdef RTLD_LAZY |
4f89dbc4 | 149 | rtldFlags |= RTLD_LAZY; |
ee1107cf VZ |
150 | #else |
151 | wxLogDebug(_T("wxDL_LAZY is not supported on this platform")); | |
152 | #endif | |
4f89dbc4 | 153 | } |
ee1107cf | 154 | else if ( flags & wxDL_NOW ) |
4f89dbc4 | 155 | { |
ee1107cf | 156 | #ifdef RTLD_NOW |
4f89dbc4 | 157 | rtldFlags |= RTLD_NOW; |
ee1107cf VZ |
158 | #else |
159 | wxLogDebug(_T("wxDL_NOW is not supported on this platform")); | |
160 | #endif | |
4f89dbc4 | 161 | } |
ee1107cf VZ |
162 | |
163 | if ( flags & wxDL_GLOBAL ) | |
4f89dbc4 | 164 | { |
ee1107cf | 165 | #ifdef RTLD_GLOBAL |
4f89dbc4 | 166 | rtldFlags |= RTLD_GLOBAL; |
ee1107cf VZ |
167 | #else |
168 | wxLogDebug(_T("RTLD_GLOBAL is not supported on this platform.")); | |
169 | #endif | |
4f89dbc4 RL |
170 | } |
171 | ||
401eb3de | 172 | m_handle = dlopen(libname.fn_str(), rtldFlags); |
ee1107cf | 173 | #endif // __VMS || __DARWIN__ ? |
4f89dbc4 RL |
174 | |
175 | #elif defined(HAVE_SHL_LOAD) | |
176 | int shlFlags = 0; | |
177 | ||
178 | if( flags & wxDL_LAZY ) | |
179 | { | |
180 | wxASSERT_MSG( (flags & wxDL_NOW) == 0, | |
181 | _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") ); | |
182 | shlFlags |= BIND_DEFERRED; | |
183 | } | |
184 | else if( flags & wxDL_NOW ) | |
185 | { | |
186 | shlFlags |= BIND_IMMEDIATE; | |
187 | } | |
401eb3de | 188 | m_handle = shl_load(libname.fn_str(), BIND_DEFERRED, 0); |
4f89dbc4 | 189 | |
4f89dbc4 RL |
190 | #elif defined(__WINDOWS__) |
191 | m_handle = ::LoadLibrary(libname.c_str()); | |
0b9ab0bd | 192 | #else |
ee1107cf | 193 | #error "runtime shared lib support not implemented on this platform" |
0b9ab0bd | 194 | #endif |
4f89dbc4 RL |
195 | |
196 | if ( m_handle == 0 ) | |
0b9ab0bd RL |
197 | { |
198 | wxString msg(_("Failed to load shared library '%s'")); | |
c822ad5a | 199 | #if defined(HAVE_DLERROR) && !defined(__EMX__) |
401eb3de | 200 | |
8e11fbb0 | 201 | #if wxUSE_UNICODE |
401eb3de RR |
202 | wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() ); |
203 | const wxChar *err = buffer; | |
204 | #else | |
205 | const wxChar *err = dlerror(); | |
206 | #endif | |
207 | ||
0b9ab0bd RL |
208 | if( err ) |
209 | wxLogError( msg, err ); | |
210 | #else | |
211 | wxLogSysError( msg, libname.c_str() ); | |
212 | #endif | |
213 | } | |
4f89dbc4 RL |
214 | |
215 | return IsLoaded(); | |
0b9ab0bd RL |
216 | } |
217 | ||
4f89dbc4 | 218 | void wxDynamicLibrary::Unload() |
0b9ab0bd | 219 | { |
4f89dbc4 RL |
220 | if( IsLoaded() ) |
221 | { | |
222 | #if defined(__WXPM__) || defined(__EMX__) | |
223 | DosFreeModule( m_handle ); | |
f9ee64b1 | 224 | #elif defined(HAVE_DLOPEN) || defined(__DARWIN__) |
4f89dbc4 RL |
225 | dlclose( m_handle ); |
226 | #elif defined(HAVE_SHL_LOAD) | |
227 | shl_unload( m_handle ); | |
228 | #elif defined(__WINDOWS__) | |
229 | ::FreeLibrary( m_handle ); | |
f9ee64b1 GD |
230 | #elif defined(__WXMAC__) && !defined(__DARWIN__) |
231 | CloseConnection( (CFragConnectionID*) &m_handle ); | |
4f89dbc4 RL |
232 | #else |
233 | #error "runtime shared lib support not implemented" | |
234 | #endif | |
235 | m_handle = 0; | |
236 | } | |
0b9ab0bd RL |
237 | } |
238 | ||
4f89dbc4 | 239 | void *wxDynamicLibrary::GetSymbol(const wxString &name, bool *success) const |
0b9ab0bd | 240 | { |
4f89dbc4 RL |
241 | wxCHECK_MSG( IsLoaded(), NULL, |
242 | _T("Can't load symbol from unloaded library") ); | |
243 | ||
0b9ab0bd RL |
244 | bool failed = FALSE; |
245 | void *symbol = 0; | |
246 | ||
f9ee64b1 | 247 | #if defined(__WXMAC__) && !defined(__DARWIN__) |
0b9ab0bd RL |
248 | Ptr symAddress; |
249 | CFragSymbolClass symClass; | |
250 | Str255 symName; | |
251 | #if TARGET_CARBON | |
252 | c2pstrcpy( (StringPtr) symName, name ); | |
253 | #else | |
4f89dbc4 RL |
254 | strcpy( (char *)symName, name ); |
255 | c2pstr( (char *)symName ); | |
0b9ab0bd RL |
256 | #endif |
257 | if( FindSymbol( dllHandle, symName, &symAddress, &symClass ) == noErr ) | |
258 | symbol = (void *)symAddress; | |
259 | ||
260 | #elif defined(__WXPM__) || defined(__EMX__) | |
4f89dbc4 RL |
261 | DosQueryProcAddr( m_handle, 1L, name.c_str(), (PFN*)symbol ); |
262 | ||
f9ee64b1 | 263 | #elif defined(HAVE_DLOPEN) || defined(__DARWIN__) |
401eb3de | 264 | symbol = dlsym( m_handle, name.fn_str() ); |
0b9ab0bd | 265 | |
4f89dbc4 | 266 | #elif defined(HAVE_SHL_LOAD) |
2b5f62a0 VZ |
267 | // use local variable since shl_findsym modifies the handle argument |
268 | // to indicate where the symbol was found (GD) | |
269 | wxDllType the_handle = m_handle; | |
270 | if( shl_findsym( &the_handle, name.fn_str(), TYPE_UNDEFINED, &symbol ) != 0 ) | |
4f89dbc4 RL |
271 | symbol = 0; |
272 | ||
4f89dbc4 | 273 | #elif defined(__WINDOWS__) |
7fc0bd1c | 274 | symbol = (void*) ::GetProcAddress( m_handle, name.mb_str() ); |
4f89dbc4 RL |
275 | |
276 | #else | |
277 | #error "runtime shared lib support not implemented" | |
0b9ab0bd RL |
278 | #endif |
279 | ||
280 | if ( !symbol ) | |
281 | { | |
c822ad5a | 282 | #if defined(HAVE_DLERROR) && !defined(__EMX__) |
401eb3de | 283 | |
8e11fbb0 | 284 | #if wxUSE_UNICODE |
401eb3de RR |
285 | wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() ); |
286 | const wxChar *err = buffer; | |
287 | #else | |
a1706592 | 288 | const wxChar *err = dlerror(); |
401eb3de RR |
289 | #endif |
290 | ||
0b9ab0bd RL |
291 | if( err ) |
292 | { | |
ee682a94 | 293 | wxLogError(wxT("%s"), err); |
0b9ab0bd RL |
294 | } |
295 | #else | |
296 | failed = TRUE; | |
297 | wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"), | |
298 | name.c_str()); | |
299 | #endif | |
300 | } | |
301 | if( success ) | |
302 | *success = !failed; | |
303 | ||
304 | return symbol; | |
305 | } | |
306 | ||
307 | ||
308 | // --------------------------------------------------------------------------- | |
4f89dbc4 | 309 | // wxPluginLibrary |
0b9ab0bd RL |
310 | // --------------------------------------------------------------------------- |
311 | ||
312 | ||
dabd1377 JS |
313 | wxDLImports* wxPluginLibrary::ms_classes = NULL; |
314 | ||
315 | class wxPluginLibraryModule : public wxModule | |
316 | { | |
317 | public: | |
d0fcccc4 VZ |
318 | wxPluginLibraryModule() { } |
319 | ||
320 | // TODO: create ms_classes on demand, why always preallocate it? | |
321 | virtual bool OnInit() | |
322 | { | |
323 | wxPluginLibrary::ms_classes = new wxDLImports(wxKEY_STRING); | |
324 | wxPluginManager::CreateManifest(); | |
325 | return TRUE; | |
326 | } | |
327 | ||
328 | virtual void OnExit() | |
329 | { | |
330 | delete wxPluginLibrary::ms_classes; | |
331 | wxPluginLibrary::ms_classes = NULL; | |
332 | wxPluginManager::ClearManifest(); | |
333 | } | |
dabd1377 JS |
334 | |
335 | private: | |
336 | DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule ) | |
337 | }; | |
338 | ||
339 | IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule, wxModule) | |
340 | ||
0b9ab0bd | 341 | |
23213f18 | 342 | wxPluginLibrary::wxPluginLibrary(const wxString &libname, int flags) |
4f89dbc4 | 343 | : m_linkcount(1) |
7c1e2b44 | 344 | , m_objcount(0) |
0b9ab0bd | 345 | { |
4f89dbc4 RL |
346 | m_before = wxClassInfo::sm_first; |
347 | Load( libname, flags ); | |
348 | m_after = wxClassInfo::sm_first; | |
349 | ||
0b9ab0bd RL |
350 | if( m_handle != 0 ) |
351 | { | |
352 | UpdateClassInfo(); | |
353 | RegisterModules(); | |
354 | } | |
355 | else | |
d0fcccc4 VZ |
356 | { |
357 | // Flag us for deletion | |
358 | --m_linkcount; | |
359 | } | |
0b9ab0bd RL |
360 | } |
361 | ||
4f89dbc4 | 362 | wxPluginLibrary::~wxPluginLibrary() |
0b9ab0bd | 363 | { |
01610529 RL |
364 | if( m_handle != 0 ) |
365 | { | |
366 | UnregisterModules(); | |
367 | RestoreClassInfo(); | |
01610529 | 368 | } |
0b9ab0bd RL |
369 | } |
370 | ||
d0fcccc4 VZ |
371 | wxPluginLibrary *wxPluginLibrary::RefLib() |
372 | { | |
373 | wxCHECK_MSG( m_linkcount > 0, NULL, | |
374 | _T("Library had been already deleted!") ); | |
375 | ||
376 | ++m_linkcount; | |
377 | return this; | |
378 | } | |
379 | ||
4f89dbc4 | 380 | bool wxPluginLibrary::UnrefLib() |
7c1e2b44 | 381 | { |
d0fcccc4 VZ |
382 | wxASSERT_MSG( m_objcount == 0, |
383 | _T("Library unloaded before all objects were destroyed") ); | |
384 | ||
385 | if ( --m_linkcount == 0 ) | |
7c1e2b44 RL |
386 | { |
387 | delete this; | |
388 | return TRUE; | |
389 | } | |
d0fcccc4 | 390 | |
7c1e2b44 RL |
391 | return FALSE; |
392 | } | |
0b9ab0bd RL |
393 | |
394 | // ------------------------ | |
395 | // Private methods | |
396 | // ------------------------ | |
397 | ||
4f89dbc4 | 398 | void wxPluginLibrary::UpdateClassInfo() |
0b9ab0bd RL |
399 | { |
400 | wxClassInfo *info; | |
401 | wxHashTable *t = wxClassInfo::sm_classTable; | |
402 | ||
403 | // FIXME: Below is simply a cut and paste specialisation of | |
404 | // wxClassInfo::InitializeClasses. Once this stabilises, | |
405 | // the two should probably be merged. | |
406 | // | |
407 | // Actually it's becoming questionable whether we should merge | |
408 | // this info with the main ClassInfo tables since we can nearly | |
409 | // handle this completely internally now and it does expose | |
410 | // certain (minimal % user_stupidy) risks. | |
411 | ||
412 | for(info = m_after; info != m_before; info = info->m_next) | |
413 | { | |
414 | if( info->m_className ) | |
415 | { | |
416 | if( t->Get(info->m_className) == 0 ) | |
417 | t->Put(info->m_className, (wxObject *)info); | |
418 | ||
419 | // Hash all the class names into a local table too so | |
420 | // we can quickly find the entry they correspond to. | |
2b5f62a0 | 421 | (*ms_classes)[info->m_className] = this; |
0b9ab0bd RL |
422 | } |
423 | } | |
424 | ||
30bfc425 | 425 | #if wxUSE_EXTENDED_RTTI == 0 |
0b9ab0bd RL |
426 | for(info = m_after; info != m_before; info = info->m_next) |
427 | { | |
428 | if( info->m_baseClassName1 ) | |
429 | info->m_baseInfo1 = (wxClassInfo *)t->Get(info->m_baseClassName1); | |
430 | if( info->m_baseClassName2 ) | |
431 | info->m_baseInfo2 = (wxClassInfo *)t->Get(info->m_baseClassName2); | |
432 | } | |
a095505c | 433 | #endif |
0b9ab0bd RL |
434 | } |
435 | ||
4f89dbc4 | 436 | void wxPluginLibrary::RestoreClassInfo() |
0b9ab0bd RL |
437 | { |
438 | wxClassInfo *info; | |
439 | ||
440 | for(info = m_after; info != m_before; info = info->m_next) | |
441 | { | |
442 | wxClassInfo::sm_classTable->Delete(info->m_className); | |
2b5f62a0 | 443 | ms_classes->erase(ms_classes->find(info->m_className)); |
0b9ab0bd RL |
444 | } |
445 | ||
446 | if( wxClassInfo::sm_first == m_after ) | |
447 | wxClassInfo::sm_first = m_before; | |
448 | else | |
449 | { | |
450 | info = wxClassInfo::sm_first; | |
451 | while( info->m_next && info->m_next != m_after ) info = info->m_next; | |
452 | ||
63fd5f0b | 453 | wxASSERT_MSG( info, _T("ClassInfo from wxPluginLibrary not found on purge")); |
0b9ab0bd RL |
454 | |
455 | info->m_next = m_before; | |
456 | } | |
457 | } | |
458 | ||
4f89dbc4 | 459 | void wxPluginLibrary::RegisterModules() |
0b9ab0bd RL |
460 | { |
461 | // Plugin libraries might have wxModules, Register and initialise them if | |
462 | // they do. | |
463 | // | |
464 | // Note that these classes are NOT included in the reference counting since | |
465 | // it's implicit that they will be unloaded if and when the last handle to | |
466 | // the library is. We do have to keep a copy of the module's pointer | |
467 | // though, as there is currently no way to Unregister it without it. | |
468 | ||
7c1e2b44 | 469 | wxASSERT_MSG( m_linkcount == 1, |
0b9ab0bd RL |
470 | _T("RegisterModules should only be called for the first load") ); |
471 | ||
6eccc0c4 | 472 | for ( wxClassInfo *info = m_after; info != m_before; info = info->m_next) |
0b9ab0bd RL |
473 | { |
474 | if( info->IsKindOf(CLASSINFO(wxModule)) ) | |
475 | { | |
476 | wxModule *m = wxDynamicCast(info->CreateObject(), wxModule); | |
477 | ||
478 | wxASSERT_MSG( m, _T("wxDynamicCast of wxModule failed") ); | |
479 | ||
df5168c4 | 480 | m_wxmodules.push_back(m); |
0b9ab0bd RL |
481 | wxModule::RegisterModule(m); |
482 | } | |
483 | } | |
484 | ||
485 | // FIXME: Likewise this is (well was) very similar to InitializeModules() | |
486 | ||
df5168c4 MB |
487 | for ( wxModuleList::iterator it = m_wxmodules.begin(); |
488 | it != m_wxmodules.end(); | |
489 | ++it) | |
0b9ab0bd | 490 | { |
df5168c4 | 491 | if( !(*it)->Init() ) |
0b9ab0bd | 492 | { |
4f89dbc4 | 493 | wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary")); |
0b9ab0bd RL |
494 | |
495 | // XXX: Watch this, a different hash implementation might break it, | |
496 | // a good hash implementation would let us fix it though. | |
497 | ||
498 | // The name of the game is to remove any uninitialised modules and | |
499 | // let the dtor Exit the rest on shutdown, (which we'll initiate | |
500 | // shortly). | |
501 | ||
df5168c4 | 502 | wxModuleList::iterator oldNode = m_wxmodules.end(); |
0b9ab0bd | 503 | do { |
df5168c4 MB |
504 | ++it; |
505 | if( oldNode != m_wxmodules.end() ) | |
506 | m_wxmodules.erase(oldNode); | |
507 | wxModule::UnregisterModule( *it ); | |
508 | oldNode = it; | |
509 | } while( it != m_wxmodules.end() ); | |
0b9ab0bd | 510 | |
7c1e2b44 | 511 | --m_linkcount; // Flag us for deletion |
0b9ab0bd RL |
512 | break; |
513 | } | |
514 | } | |
515 | } | |
516 | ||
4f89dbc4 | 517 | void wxPluginLibrary::UnregisterModules() |
0b9ab0bd | 518 | { |
df5168c4 | 519 | wxModuleList::iterator it; |
0b9ab0bd | 520 | |
df5168c4 MB |
521 | for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it ) |
522 | (*it)->Exit(); | |
0b9ab0bd | 523 | |
df5168c4 MB |
524 | for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it ) |
525 | wxModule::UnregisterModule( *it ); | |
0b9ab0bd | 526 | |
df5168c4 | 527 | WX_CLEAR_LIST(wxModuleList, m_wxmodules); |
0b9ab0bd RL |
528 | } |
529 | ||
530 | ||
531 | // --------------------------------------------------------------------------- | |
d0fcccc4 | 532 | // wxPluginManager |
0b9ab0bd RL |
533 | // --------------------------------------------------------------------------- |
534 | ||
dabd1377 | 535 | wxDLManifest* wxPluginManager::ms_manifest = NULL; |
0b9ab0bd RL |
536 | |
537 | // ------------------------ | |
538 | // Static accessors | |
539 | // ------------------------ | |
540 | ||
d0fcccc4 VZ |
541 | wxPluginLibrary * |
542 | wxPluginManager::LoadLibrary(const wxString &libname, int flags) | |
0b9ab0bd | 543 | { |
4f89dbc4 | 544 | wxString realname(libname); |
0b9ab0bd | 545 | |
4f89dbc4 RL |
546 | if( !(flags & wxDL_VERBATIM) ) |
547 | realname += wxDynamicLibrary::GetDllExt(); | |
548 | ||
d0fcccc4 VZ |
549 | wxPluginLibrary *entry; |
550 | ||
551 | if ( flags & wxDL_NOSHARE ) | |
552 | { | |
553 | entry = NULL; | |
554 | } | |
555 | else | |
556 | { | |
2b5f62a0 | 557 | entry = FindByName(realname); |
d0fcccc4 | 558 | } |
4f89dbc4 | 559 | |
d0fcccc4 | 560 | if ( entry ) |
0b9ab0bd | 561 | { |
d0fcccc4 VZ |
562 | wxLogTrace(_T("dll"), |
563 | _T("LoadLibrary(%s): already loaded."), realname.c_str()); | |
564 | ||
7c1e2b44 | 565 | entry->RefLib(); |
0b9ab0bd RL |
566 | } |
567 | else | |
568 | { | |
4f89dbc4 | 569 | entry = new wxPluginLibrary( libname, flags ); |
0b9ab0bd | 570 | |
d0fcccc4 | 571 | if ( entry->IsLoaded() ) |
0b9ab0bd | 572 | { |
2b5f62a0 | 573 | (*ms_manifest)[realname] = entry; |
d0fcccc4 VZ |
574 | |
575 | wxLogTrace(_T("dll"), | |
576 | _T("LoadLibrary(%s): loaded ok."), realname.c_str()); | |
577 | ||
0b9ab0bd RL |
578 | } |
579 | else | |
580 | { | |
d0fcccc4 VZ |
581 | wxLogTrace(_T("dll"), |
582 | _T("LoadLibrary(%s): failed to load."), realname.c_str()); | |
583 | ||
584 | // we have created entry just above | |
585 | if ( !entry->UnrefLib() ) | |
586 | { | |
587 | // ... so UnrefLib() is supposed to delete it | |
588 | wxFAIL_MSG( _T("Currently linked library is not loaded?") ); | |
589 | } | |
590 | ||
591 | entry = NULL; | |
0b9ab0bd RL |
592 | } |
593 | } | |
d0fcccc4 | 594 | |
0b9ab0bd RL |
595 | return entry; |
596 | } | |
597 | ||
d0fcccc4 | 598 | bool wxPluginManager::UnloadLibrary(const wxString& libname) |
0b9ab0bd | 599 | { |
d0fcccc4 | 600 | wxString realname = libname; |
4f89dbc4 | 601 | |
2b5f62a0 | 602 | wxPluginLibrary *entry = FindByName(realname); |
0b9ab0bd | 603 | |
d0fcccc4 VZ |
604 | if ( !entry ) |
605 | { | |
606 | realname += wxDynamicLibrary::GetDllExt(); | |
0b9ab0bd | 607 | |
2b5f62a0 | 608 | entry = FindByName(realname); |
d0fcccc4 VZ |
609 | } |
610 | ||
611 | if ( !entry ) | |
612 | { | |
613 | wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."), | |
614 | libname.c_str()); | |
615 | ||
616 | return FALSE; | |
617 | } | |
618 | ||
619 | wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname.c_str()); | |
620 | ||
621 | if ( !entry->UnrefLib() ) | |
622 | { | |
623 | // not really unloaded yet | |
624 | return FALSE; | |
625 | } | |
626 | ||
2b5f62a0 | 627 | ms_manifest->erase(ms_manifest->find(realname)); |
d0fcccc4 VZ |
628 | |
629 | return TRUE; | |
4f89dbc4 RL |
630 | } |
631 | ||
632 | #if WXWIN_COMPATIBILITY_2_2 | |
633 | wxPluginLibrary *wxPluginManager::GetObjectFromHandle(wxDllType handle) | |
634 | { | |
2b5f62a0 VZ |
635 | for ( wxDLManifest::iterator i = ms_manifest->begin(); |
636 | i != ms_manifest->end(); | |
637 | ++i ) | |
638 | { | |
639 | wxPluginLibrary * const lib = i->second; | |
4f89dbc4 | 640 | |
2b5f62a0 VZ |
641 | if ( lib->GetLibHandle() == handle ) |
642 | return lib; | |
643 | } | |
4f89dbc4 | 644 | |
d0fcccc4 | 645 | return NULL; |
0b9ab0bd | 646 | } |
d0fcccc4 | 647 | #endif // WXWIN_COMPATIBILITY_2_2 |
0b9ab0bd RL |
648 | |
649 | // ------------------------ | |
650 | // Class implementation | |
651 | // ------------------------ | |
652 | ||
23213f18 | 653 | bool wxPluginManager::Load(const wxString &libname, int flags) |
0b9ab0bd | 654 | { |
4f89dbc4 | 655 | m_entry = wxPluginManager::LoadLibrary(libname, flags); |
2b5f62a0 | 656 | |
4f89dbc4 | 657 | return IsLoaded(); |
0b9ab0bd RL |
658 | } |
659 | ||
4f89dbc4 | 660 | void wxPluginManager::Unload() |
0b9ab0bd | 661 | { |
2b5f62a0 | 662 | wxCHECK_RET( m_entry, _T("unloading an invalid wxPluginManager?") ); |
0b9ab0bd | 663 | |
2b5f62a0 VZ |
664 | for ( wxDLManifest::iterator i = ms_manifest->begin(); |
665 | i != ms_manifest->end(); | |
666 | ++i ) | |
4f89dbc4 | 667 | { |
2b5f62a0 VZ |
668 | if ( i->second == m_entry ) |
669 | { | |
670 | ms_manifest->erase(i); | |
46113053 | 671 | break; |
2b5f62a0 | 672 | } |
4f89dbc4 | 673 | } |
2b5f62a0 VZ |
674 | |
675 | m_entry->UnrefLib(); | |
676 | ||
677 | m_entry = NULL; | |
0b9ab0bd RL |
678 | } |
679 | ||
4f89dbc4 RL |
680 | // --------------------------------------------------------------------------- |
681 | // wxDllLoader (all these methods are static) | |
682 | // --------------------------------------------------------------------------- | |
683 | ||
684 | #if WXWIN_COMPATIBILITY_2_2 | |
685 | ||
3d7fa4e6 | 686 | wxDllType wxDllLoader::LoadLibrary(const wxString &name, bool *success) |
4f89dbc4 | 687 | { |
d0fcccc4 VZ |
688 | wxPluginLibrary *p = wxPluginManager::LoadLibrary |
689 | ( | |
690 | name, | |
691 | wxDL_DEFAULT | wxDL_VERBATIM | wxDL_NOSHARE | |
692 | ); | |
693 | ||
3d7fa4e6 VZ |
694 | if ( success ) |
695 | *success = p != NULL; | |
696 | ||
d0fcccc4 | 697 | return p ? p->GetLibHandle() : 0; |
0b9ab0bd RL |
698 | } |
699 | ||
4f89dbc4 | 700 | void wxDllLoader::UnloadLibrary(wxDllType handle) |
0b9ab0bd | 701 | { |
4f89dbc4 | 702 | wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(handle); |
d0fcccc4 VZ |
703 | |
704 | wxCHECK_RET( p, _T("Unloading a library not loaded with wxDllLoader?") ); | |
705 | ||
4f89dbc4 | 706 | p->UnrefLib(); |
0b9ab0bd RL |
707 | } |
708 | ||
d0fcccc4 VZ |
709 | void * |
710 | wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name, bool *success) | |
0b9ab0bd | 711 | { |
4f89dbc4 | 712 | wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(dllHandle); |
d0fcccc4 VZ |
713 | |
714 | if ( !p ) | |
715 | { | |
716 | wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") ); | |
717 | ||
718 | if ( success ) | |
719 | *success = FALSE; | |
720 | ||
721 | return NULL; | |
722 | } | |
723 | ||
4f89dbc4 | 724 | return p->GetSymbol(name, success); |
0b9ab0bd RL |
725 | } |
726 | ||
4f89dbc4 RL |
727 | #endif // WXWIN_COMPATIBILITY_2_2 |
728 | ||
0b9ab0bd RL |
729 | #endif // wxUSE_DYNAMIC_LOADER |
730 |