1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Dynamic library management
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 # pragma implementation "dynlib.h"
24 #include "wx/wxprec.h"
30 #if wxUSE_DYNLIB_CLASS
32 #if defined(__WINDOWS__)
33 #include "wx/msw/private.h"
36 #include "wx/dynlib.h"
37 #include "wx/filefn.h"
40 #include "wx/tokenzr.h"
42 // ----------------------------------------------------------------------------
43 // conditional compilation
44 // ----------------------------------------------------------------------------
46 #if defined(__WXPM__) || defined(__EMX__)
49 # define wxDllOpen(error, lib, handle) DosLoadModule(error, sizeof(error), lib, &handle)
50 # define wxDllGetSymbol(handle, modaddr) DosQueryProcAddr(handle, 1L, NULL, (PFN*)modaddr)
51 # define wxDllClose(handle) DosFreeModule(handle)
52 #elif defined(HAVE_DLOPEN)
53 // note about dlopen() flags: we use RTLD_NOW to have more Windows-like
54 // behaviour (Win won't let you load a library with missing symbols) and
55 // RTLD_GLOBAL because it is needed sometimes and probably doesn't hurt
56 // otherwise. On True64-Unix RTLD_GLOBAL is not allowed and on VMS the
57 // second argument on dlopen is ignored.
59 # define wxDllOpen(lib) dlopen(lib.fn_str(), 0 )
60 #elif defined( __osf__ )
61 # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_LAZY )
63 # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_LAZY | RTLD_GLOBAL)
65 #define wxDllGetSymbol(handle, name) dlsym(handle, name)
66 # define wxDllClose dlclose
67 #elif defined(HAVE_SHL_LOAD)
68 # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
69 # define wxDllClose shl_unload
71 static inline void *wxDllGetSymbol(shl_t handle
, const wxString
& name
)
74 if ( shl_findsym(&handle
, name
.mb_str(), TYPE_UNDEFINED
, &sym
) == 0 )
79 #elif defined(__APPLE__) && defined(__UNIX__)
80 void *dlopen(const char *path
, int mode
/* mode is ignored */);
81 void *dlsym(void *handle
, const char *symbol
);
82 int dlclose(void *handle
);
83 const char *dlerror(void);
85 # define wxDllOpen(lib) dlopen(lib.fn_str(), 0)
86 # define wxDllGetSymbol(handle, name) dlsym(handle, name)
87 # define wxDllClose dlclose
88 #elif defined(__WINDOWS__)
89 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
92 # define wxDllOpen(lib) ::LoadLibraryExW(lib, 0, 0)
94 # define wxDllOpen(lib) ::LoadLibraryExA(lib, 0, 0)
97 # define wxDllOpen(lib) ::LoadLibrary(lib)
99 # define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
100 # define wxDllClose ::FreeLibrary
102 # error "Don't know how to load shared libraries on this platform."
105 // ---------------------------------------------------------------------------
107 // ---------------------------------------------------------------------------
109 wxLibraries wxTheLibraries
;
111 // ============================================================================
113 // ============================================================================
115 // construct the full name from the base shared object name: adds a .dll
116 // suffix under Windows or .so under Unix
117 static wxString
ConstructLibraryName(const wxString
& basename
)
120 fullname
<< basename
<< wxDllLoader::GetDllExt();
125 // ---------------------------------------------------------------------------
126 // wxLibrary (one instance per dynamic library)
127 // ---------------------------------------------------------------------------
129 wxLibrary::wxLibrary(wxDllType handle
)
131 typedef wxClassInfo
*(*t_get_first
)(void);
132 t_get_first get_first
;
136 // Some system may use a local heap for library.
137 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
138 // It is a wxWindows DLL.
140 PrepareClasses(get_first());
143 wxLibrary::~wxLibrary()
147 wxDllClose(m_handle
);
151 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
153 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
158 return info
->CreateObject();
161 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
163 // Index all class infos by their class name
164 wxClassInfo
*info
= first
;
167 if (info
->m_className
)
168 classTable
.Put(info
->m_className
, (wxObject
*)info
);
169 info
= (wxClassInfo
*)info
->GetNext();
172 // Set base pointers for each wxClassInfo
176 if (info
->GetBaseClassName1())
177 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
178 if (info
->GetBaseClassName2())
179 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
184 void *wxLibrary::GetSymbol(const wxString
& symbname
)
186 return wxDllLoader::GetSymbol(m_handle
, symbname
);
189 // ---------------------------------------------------------------------------
191 // ---------------------------------------------------------------------------
194 wxString
wxDllLoader::GetDllExt()
198 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
200 #elif defined(__UNIX__)
201 # if defined(__HPUX__)
213 wxDllLoader::GetProgramHandle(void)
215 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
216 // optain handle for main program
217 return dlopen(NULL
, RTLD_NOW
/*RTLD_LAZY*/);
218 #elif defined (HAVE_SHL_LOAD)
219 // shl_findsymbol with NULL handle looks up in main program
222 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
229 wxDllLoader::LoadLibrary(const wxString
& libname
, bool *success
)
233 #if defined(__WXMAC__) && !defined(__UNIX__)
238 wxMacPathToFSSpec( libname
, &myFSSpec
) ;
239 if (GetDiskFragment( &myFSSpec
, 0 , kCFragGoesToEOF
, "\p" , kPrivateCFragCopy
, &handle
, &myMainAddr
,
240 myErrName
) != noErr
)
242 p2cstr( myErrName
) ;
243 wxASSERT_MSG( 1 , (char*)myErrName
) ;
246 #elif defined(__WXPM__) || defined(__EMX__)
247 char zError
[256] = "";
248 wxDllOpen(zError
, libname
, handle
);
250 handle
= wxDllOpen(libname
);
255 wxString
msg(_("Failed to load shared library '%s'"));
258 const char *errmsg
= dlerror();
261 // the error string format is "libname: ...", but we already have
262 // libname, so cut it off
263 const char *p
= strchr(errmsg
, ':');
275 wxLogError(msg
, libname
.c_str(), p
);
278 #endif // HAVE_DLERROR
280 wxLogSysError(msg
, libname
.c_str());
286 *success
= handle
!= 0;
295 wxDllLoader::UnloadLibrary(wxDllType handle
)
302 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
)
304 void *symbol
= NULL
; // return value
306 #if defined(__WXMAC__) && !defined(__UNIX__)
308 CFragSymbolClass symClass
;
312 c2pstrcpy( (StringPtr
) symName
, name
) ;
314 strcpy( (char *) symName
, name
) ;
315 c2pstr( (char *) symName
) ;
318 if ( FindSymbol( dllHandle
, symName
, &symAddress
, &symClass
) == noErr
)
319 symbol
= (void *)symAddress
;
320 #elif defined( __WXPM__ ) || defined(__EMX__)
321 wxDllGetSymbol(dllHandle
, symbol
);
323 // mb_str() is necessary in Unicode build
324 symbol
= wxDllGetSymbol(dllHandle
, name
.mb_str());
329 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
335 // ---------------------------------------------------------------------------
336 // wxLibraries (only one instance should normally exist)
337 // ---------------------------------------------------------------------------
339 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING
)
343 wxLibraries::~wxLibraries()
345 wxNode
*node
= m_loaded
.First();
348 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
355 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
359 wxClassInfo
*old_sm_first
;
361 #if defined(__VISAGECPP__)
362 node
= m_loaded
.Find(name
.GetData());
364 return ((wxLibrary
*)node
->Data());
366 node
= m_loaded
.Find(name
.GetData());
368 return ((wxLibrary
*)node
->Data());
370 // If DLL shares data, this is necessary.
371 old_sm_first
= wxClassInfo::sm_first
;
372 wxClassInfo::sm_first
= NULL
;
374 wxString libname
= ConstructLibraryName(name
);
377 Unix automatically builds that library name, at least for dlopen()
380 #if defined(__UNIX__)
381 // found the first file in LD_LIBRARY_PATH with this name
382 wxString
libPath("/lib:/usr/lib"); // system path first
383 const char *envLibPath
= getenv("LD_LIBRARY_PATH");
385 libPath
<< wxT(':') << envLibPath
;
386 wxStringTokenizer
tokenizer(libPath
, wxT(':'));
387 while ( tokenizer
.HasMoreToken() )
389 wxString
fullname(tokenizer
.NextToken());
391 fullname
<< wxT('/') << libname
;
392 if ( wxFileExists(fullname
) )
400 //else: not found in the path, leave the name as is (secutiry risk?)
405 bool success
= FALSE
;
406 wxDllType handle
= wxDllLoader::LoadLibrary(libname
, &success
);
409 lib
= new wxLibrary(handle
);
410 wxClassInfo::sm_first
= old_sm_first
;
411 m_loaded
.Append(name
.GetData(), lib
);
418 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
420 wxNode
*node
= m_loaded
.First();
424 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
433 #endif // wxUSE_DYNLIB_CLASS