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
57 # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_LAZY | RTLD_GLOBAL)
58 # define wxDllGetSymbol(handle, name) dlsym(handle, name)
59 # define wxDllClose dlclose
60 #elif defined(HAVE_SHL_LOAD)
61 # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
62 # define wxDllClose shl_unload
64 static inline void *wxDllGetSymbol(shl_t handle
, const wxString
& name
)
67 if ( shl_findsym(&handle
, name
.mb_str(), TYPE_UNDEFINED
, &sym
) == 0 )
72 #elif defined(__WINDOWS__)
73 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
76 # define wxDllOpen(lib) ::LoadLibraryExW(lib, 0, 0)
78 # define wxDllOpen(lib) ::LoadLibraryExA(lib, 0, 0)
81 # define wxDllOpen(lib) ::LoadLibrary(lib)
83 # define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
84 # define wxDllClose ::FreeLibrary
86 # error "Don't know how to load shared libraries on this platform."
89 // ---------------------------------------------------------------------------
91 // ---------------------------------------------------------------------------
93 wxLibraries wxTheLibraries
;
95 // ============================================================================
97 // ============================================================================
99 // construct the full name from the base shared object name: adds a .dll
100 // suffix under Windows or .so under Unix
101 static wxString
ConstructLibraryName(const wxString
& basename
)
104 fullname
<< basename
<< wxDllLoader::GetDllExt();
109 // ---------------------------------------------------------------------------
110 // wxLibrary (one instance per dynamic library)
111 // ---------------------------------------------------------------------------
113 wxLibrary::wxLibrary(wxDllType handle
)
115 typedef wxClassInfo
*(*t_get_first
)(void);
116 t_get_first get_first
;
120 // Some system may use a local heap for library.
121 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
122 // It is a wxWindows DLL.
124 PrepareClasses(get_first());
127 wxLibrary::~wxLibrary()
131 wxDllClose(m_handle
);
135 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
137 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
142 return info
->CreateObject();
145 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
147 // Index all class infos by their class name
148 wxClassInfo
*info
= first
;
151 if (info
->m_className
)
152 classTable
.Put(info
->m_className
, (wxObject
*)info
);
153 info
= info
->GetNext();
156 // Set base pointers for each wxClassInfo
160 if (info
->GetBaseClassName1())
161 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
162 if (info
->GetBaseClassName2())
163 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
168 void *wxLibrary::GetSymbol(const wxString
& symbname
)
170 return wxDllLoader::GetSymbol(m_handle
, symbname
);
173 // ---------------------------------------------------------------------------
175 // ---------------------------------------------------------------------------
178 wxString
wxDllLoader::GetDllExt()
182 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
184 #elif defined(__UNIX__)
185 # if defined(__HPUX__)
197 wxDllLoader::GetProgramHandle(void)
199 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
200 // optain handle for main program
201 return dlopen(NULL
, RTLD_NOW
/*RTLD_LAZY*/);
202 #elif defined (HAVE_SHL_LOAD)
203 // shl_findsymbol with NULL handle looks up in main program
206 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
213 wxDllLoader::LoadLibrary(const wxString
& libname
, bool *success
)
217 #if defined(__WXMAC__)
222 wxMacPathToFSSpec( libname
, &myFSSpec
) ;
223 if (GetDiskFragment( &myFSSpec
, 0 , kCFragGoesToEOF
, "\p" , kPrivateCFragCopy
, &handle
, &myMainAddr
,
224 myErrName
) != noErr
)
226 p2cstr( myErrName
) ;
227 wxASSERT_MSG( 1 , (char*)myErrName
) ;
230 #elif defined(__WXPM__) || defined(__EMX__)
231 char zError
[256] = "";
232 wxDllOpen(zError
, libname
, handle
);
234 handle
= wxDllOpen(libname
);
239 wxString
msg(_("Failed to load shared library '%s'"));
242 const char *errmsg
= dlerror();
245 // the error string format is "libname: ...", but we already have
246 // libname, so cut it off
247 const char *p
= strchr(errmsg
, ':');
259 wxLogError(msg
, libname
.c_str(), p
);
262 #endif // HAVE_DLERROR
264 wxLogSysError(msg
, libname
.c_str());
270 *success
= handle
!= 0;
279 wxDllLoader::UnloadLibrary(wxDllType handle
)
286 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
)
288 void *symbol
= NULL
; // return value
290 #if defined( __WXMAC__ )
292 CFragSymbolClass symClass
;
295 strcpy( (char*) symName
, name
) ;
296 c2pstr( (char*) symName
) ;
298 if ( FindSymbol( dllHandle
, symName
, &symAddress
, &symClass
) == noErr
)
299 symbol
= (void *)symAddress
;
300 #elif defined( __WXPM__ ) || defined(__EMX__)
301 wxDllGetSymbol(dllHandle
, symbol
);
303 // mb_str() is necessary in Unicode build
304 symbol
= wxDllGetSymbol(dllHandle
, name
.mb_str());
309 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
315 // ---------------------------------------------------------------------------
316 // wxLibraries (only one instance should normally exist)
317 // ---------------------------------------------------------------------------
319 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING
)
323 wxLibraries::~wxLibraries()
325 wxNode
*node
= m_loaded
.First();
328 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
335 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
339 wxClassInfo
*old_sm_first
;
341 #if defined(__VISAGECPP__)
342 node
= m_loaded
.Find(name
.GetData());
344 return ((wxLibrary
*)node
->Data());
346 if ( (node
= m_loaded
.Find(name
.GetData())) )
347 return ((wxLibrary
*)node
->Data());
349 // If DLL shares data, this is necessary.
350 old_sm_first
= wxClassInfo::sm_first
;
351 wxClassInfo::sm_first
= NULL
;
353 wxString libname
= ConstructLibraryName(name
);
356 Unix automatically builds that library name, at least for dlopen()
359 #if defined(__UNIX__)
360 // found the first file in LD_LIBRARY_PATH with this name
361 wxString
libPath("/lib:/usr/lib"); // system path first
362 const char *envLibPath
= getenv("LD_LIBRARY_PATH");
364 libPath
<< wxT(':') << envLibPath
;
365 wxStringTokenizer
tokenizer(libPath
, wxT(':'));
366 while ( tokenizer
.HasMoreToken() )
368 wxString
fullname(tokenizer
.NextToken());
370 fullname
<< wxT('/') << libname
;
371 if ( wxFileExists(fullname
) )
379 //else: not found in the path, leave the name as is (secutiry risk?)
384 bool success
= FALSE
;
385 wxDllType handle
= wxDllLoader::LoadLibrary(libname
, &success
);
388 lib
= new wxLibrary(handle
);
389 wxClassInfo::sm_first
= old_sm_first
;
390 m_loaded
.Append(name
.GetData(), lib
);
397 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
399 wxNode
*node
= m_loaded
.First();
403 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
412 #endif // wxUSE_DYNLIB_CLASS