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 VMS the second argument on dlopen is ignored.
58 # define wxDllOpen(lib) dlopen(lib.fn_str(), 0 )
60 # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_LAZY | RTLD_GLOBAL)
62 #define wxDllGetSymbol(handle, name) dlsym(handle, name)
63 # define wxDllClose dlclose
64 #elif defined(HAVE_SHL_LOAD)
65 # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
66 # define wxDllClose shl_unload
68 static inline void *wxDllGetSymbol(shl_t handle
, const wxString
& name
)
71 if ( shl_findsym(&handle
, name
.mb_str(), TYPE_UNDEFINED
, &sym
) == 0 )
76 #elif defined(__WINDOWS__)
77 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
80 # define wxDllOpen(lib) ::LoadLibraryExW(lib, 0, 0)
82 # define wxDllOpen(lib) ::LoadLibraryExA(lib, 0, 0)
85 # define wxDllOpen(lib) ::LoadLibrary(lib)
87 # define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
88 # define wxDllClose ::FreeLibrary
90 # error "Don't know how to load shared libraries on this platform."
93 // ---------------------------------------------------------------------------
95 // ---------------------------------------------------------------------------
97 wxLibraries wxTheLibraries
;
99 // ============================================================================
101 // ============================================================================
103 // construct the full name from the base shared object name: adds a .dll
104 // suffix under Windows or .so under Unix
105 static wxString
ConstructLibraryName(const wxString
& basename
)
108 fullname
<< basename
<< wxDllLoader::GetDllExt();
113 // ---------------------------------------------------------------------------
114 // wxLibrary (one instance per dynamic library)
115 // ---------------------------------------------------------------------------
117 wxLibrary::wxLibrary(wxDllType handle
)
119 typedef wxClassInfo
*(*t_get_first
)(void);
120 t_get_first get_first
;
124 // Some system may use a local heap for library.
125 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
126 // It is a wxWindows DLL.
128 PrepareClasses(get_first());
131 wxLibrary::~wxLibrary()
135 wxDllClose(m_handle
);
139 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
141 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
146 return info
->CreateObject();
149 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
151 // Index all class infos by their class name
152 wxClassInfo
*info
= first
;
155 if (info
->m_className
)
156 classTable
.Put(info
->m_className
, (wxObject
*)info
);
157 info
= info
->GetNext();
160 // Set base pointers for each wxClassInfo
164 if (info
->GetBaseClassName1())
165 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
166 if (info
->GetBaseClassName2())
167 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
172 void *wxLibrary::GetSymbol(const wxString
& symbname
)
174 return wxDllLoader::GetSymbol(m_handle
, symbname
);
177 // ---------------------------------------------------------------------------
179 // ---------------------------------------------------------------------------
182 wxString
wxDllLoader::GetDllExt()
186 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
188 #elif defined(__UNIX__)
189 # if defined(__HPUX__)
201 wxDllLoader::GetProgramHandle(void)
203 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
204 // optain handle for main program
205 return dlopen(NULL
, RTLD_NOW
/*RTLD_LAZY*/);
206 #elif defined (HAVE_SHL_LOAD)
207 // shl_findsymbol with NULL handle looks up in main program
210 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
217 wxDllLoader::LoadLibrary(const wxString
& libname
, bool *success
)
221 #if defined(__WXMAC__)
226 wxMacPathToFSSpec( libname
, &myFSSpec
) ;
227 if (GetDiskFragment( &myFSSpec
, 0 , kCFragGoesToEOF
, "\p" , kPrivateCFragCopy
, &handle
, &myMainAddr
,
228 myErrName
) != noErr
)
230 p2cstr( myErrName
) ;
231 wxASSERT_MSG( 1 , (char*)myErrName
) ;
234 #elif defined(__WXPM__) || defined(__EMX__)
235 char zError
[256] = "";
236 wxDllOpen(zError
, libname
, handle
);
238 handle
= wxDllOpen(libname
);
243 wxString
msg(_("Failed to load shared library '%s'"));
246 const char *errmsg
= dlerror();
249 // the error string format is "libname: ...", but we already have
250 // libname, so cut it off
251 const char *p
= strchr(errmsg
, ':');
263 wxLogError(msg
, libname
.c_str(), p
);
266 #endif // HAVE_DLERROR
268 wxLogSysError(msg
, libname
.c_str());
274 *success
= handle
!= 0;
283 wxDllLoader::UnloadLibrary(wxDllType handle
)
290 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
)
292 void *symbol
= NULL
; // return value
294 #if defined( __WXMAC__ )
296 CFragSymbolClass symClass
;
299 strcpy( (char*) symName
, name
) ;
300 c2pstr( (char*) symName
) ;
302 if ( FindSymbol( dllHandle
, symName
, &symAddress
, &symClass
) == noErr
)
303 symbol
= (void *)symAddress
;
304 #elif defined( __WXPM__ ) || defined(__EMX__)
305 wxDllGetSymbol(dllHandle
, symbol
);
307 // mb_str() is necessary in Unicode build
308 symbol
= wxDllGetSymbol(dllHandle
, name
.mb_str());
313 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
319 // ---------------------------------------------------------------------------
320 // wxLibraries (only one instance should normally exist)
321 // ---------------------------------------------------------------------------
323 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING
)
327 wxLibraries::~wxLibraries()
329 wxNode
*node
= m_loaded
.First();
332 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
339 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
343 wxClassInfo
*old_sm_first
;
345 #if defined(__VISAGECPP__)
346 node
= m_loaded
.Find(name
.GetData());
348 return ((wxLibrary
*)node
->Data());
350 if ( (node
= m_loaded
.Find(name
.GetData())) )
351 return ((wxLibrary
*)node
->Data());
353 // If DLL shares data, this is necessary.
354 old_sm_first
= wxClassInfo::sm_first
;
355 wxClassInfo::sm_first
= NULL
;
357 wxString libname
= ConstructLibraryName(name
);
360 Unix automatically builds that library name, at least for dlopen()
363 #if defined(__UNIX__)
364 // found the first file in LD_LIBRARY_PATH with this name
365 wxString
libPath("/lib:/usr/lib"); // system path first
366 const char *envLibPath
= getenv("LD_LIBRARY_PATH");
368 libPath
<< wxT(':') << envLibPath
;
369 wxStringTokenizer
tokenizer(libPath
, wxT(':'));
370 while ( tokenizer
.HasMoreToken() )
372 wxString
fullname(tokenizer
.NextToken());
374 fullname
<< wxT('/') << libname
;
375 if ( wxFileExists(fullname
) )
383 //else: not found in the path, leave the name as is (secutiry risk?)
388 bool success
= FALSE
;
389 wxDllType handle
= wxDllLoader::LoadLibrary(libname
, &success
);
392 lib
= new wxLibrary(handle
);
393 wxClassInfo::sm_first
= old_sm_first
;
394 m_loaded
.Append(name
.GetData(), lib
);
401 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
403 wxNode
*node
= m_loaded
.First();
407 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
416 #endif // wxUSE_DYNLIB_CLASS