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