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