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 #include "wx/dynlib.h"
33 #include "wx/filefn.h"
36 #include "wx/tokenzr.h"
38 // ----------------------------------------------------------------------------
39 // conditional compilation
40 // ----------------------------------------------------------------------------
42 #if defined(HAVE_DLOPEN)
43 #define wxDllOpen(lib) dlopen(lib, RTLD_LAZY)
44 #define wxDllGetSymbol(handle, name) dlsym(handle, (char *)name)
45 #define wxDllClose dlclose
46 #elif defined(HAVE_SHL_LOAD)
47 #define wxDllOpen(lib) shl_load(lib, BIND_DEFERRED, 0)
48 #define wxDllClose shl_unload
50 static inline void *wxDllGetSymbol(shl_t handle
, const char *name
)
53 if ( shl_findsym(&handle
, name
, TYPE_UNDEFINED
, &sym
) == 0 )
58 #elif defined(__WINDOWS__)
61 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
63 #define wxDllOpen(lib) ::LoadLibraryEx(lib, 0, 0)
65 #define wxDllOpen(lib) ::LoadLibrary(lib)
67 #define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
68 #define wxDllClose ::FreeLibrary
70 #error "Don't know how to load shared libraries on this platform."
73 // ---------------------------------------------------------------------------
75 // ---------------------------------------------------------------------------
77 wxLibraries wxTheLibraries
;
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // construct the full name from the base shared object name: adds a .dll
84 // suffix under Windows or .so under Unix
85 static wxString
ConstructLibraryName(const wxString
& basename
)
87 wxString
fullname(basename
);
95 #elif defined(__WINDOWS__)
102 // ============================================================================
104 // ============================================================================
106 // ---------------------------------------------------------------------------
107 // wxLibrary (one instance per dynamic library)
108 // ---------------------------------------------------------------------------
110 wxLibrary::wxLibrary(wxDllType handle
)
112 typedef wxClassInfo
*(*t_get_first
)(void);
113 t_get_first get_first
;
117 // Some system may use a local heap for library.
118 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
119 // It is a wxWindows DLL.
121 PrepareClasses(get_first());
124 wxLibrary::~wxLibrary()
128 wxDllClose(m_handle
);
132 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
134 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
139 return info
->CreateObject();
142 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
144 // Index all class infos by their class name
145 wxClassInfo
*info
= first
;
148 if (info
->m_className
)
149 classTable
.Put(info
->m_className
, (wxObject
*)info
);
150 info
= info
->GetNext();
153 // Set base pointers for each wxClassInfo
157 if (info
->GetBaseClassName1())
158 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
159 if (info
->GetBaseClassName2())
160 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
165 void *wxLibrary::GetSymbol(const wxString
& symbname
)
167 void *symbol
= NULL
; // return value
169 #if defined( __WXMAC__ )
171 CFragSymbolClass symClass
;
174 strcpy( (char*) symName
, symbname
) ;
175 c2pstr( (char*) symName
) ;
177 if ( FindSymbol( m_handle
, symName
, &symAddress
, &symClass
) == noErr
)
179 symbol
= (void *)symAddress
;
182 // VZ: hmm... why is WXSTRINGCAST needed? if it's really modified, we
183 // should make a copy of it
184 symbol
= wxDllGetSymbol(m_handle
, WXSTRINGCAST symbname
);
189 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
196 // ---------------------------------------------------------------------------
197 // wxLibraries (only one instance should normally exist)
198 // ---------------------------------------------------------------------------
200 wxLibraries::wxLibraries()
204 wxLibraries::~wxLibraries()
206 wxNode
*node
= m_loaded
.First();
209 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
216 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
220 wxClassInfo
*old_sm_first
;
222 if ( (node
= m_loaded
.Find(name
.GetData())) )
223 return ((wxLibrary
*)node
->Data());
225 // If DLL shares data, this is necessary.
226 old_sm_first
= wxClassInfo::sm_first
;
227 wxClassInfo::sm_first
= NULL
;
229 wxString lib_name
= ConstructLibraryName(name
);
231 #if defined(__UNIX__)
232 // found the first file in LD_LIBRARY_PATH with this name
233 wxString
libPath("/lib:/usr/lib"); // system path first
234 const char *envLibPath
= getenv("LD_LIBRARY_PATH");
236 libPath
<< ':' << envLibPath
;
237 wxStringTokenizer
tokenizer(libPath
, ':');
238 while ( tokenizer
.HasMoreToken() )
240 wxString
fullname(tokenizer
.NextToken());
242 fullname
<< '/' << lib_name
;
243 if ( wxFileExists(fullname
) )
251 //else: not found in the path, leave the name as is (secutiry risk?)
257 #if defined(__WXMAC__)
262 wxMacPathToFSSpec( lib_name
, &myFSSpec
) ;
263 if (GetDiskFragment( &myFSSpec
, 0 , kCFragGoesToEOF
, "\p" , kPrivateCFragCopy
, &handle
, &myMainAddr
,
264 myErrName
) != noErr
)
266 p2cstr( myErrName
) ;
267 wxASSERT_MSG( 1 , (char*)myErrName
) ;
271 handle
= wxDllOpen(lib_name
);
276 wxLogSysError(_("Failed to load shared library '%s'"),
282 lib
= new wxLibrary(handle
);
284 wxClassInfo::sm_first
= old_sm_first
;
286 m_loaded
.Append(name
.GetData(), lib
);
291 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
293 wxNode
*node
= m_loaded
.First();
297 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
306 #endif // wxUSE_DYNLIB_CLASS