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 #include "wx/dynlib.h"
31 #include "wx/filefn.h"
35 // ----------------------------------------------------------------------------
36 // conditional compilation
37 // ----------------------------------------------------------------------------
39 #if defined(HAVE_DLOPEN)
40 #define wxDllOpen(lib) dlopen(lib, RTLD_LAZY)
41 #define wxDllGetSymbol(handle, name) dlsym(handle, (char *)name)
42 #define wxDllClose dlclose
43 #elif defined(HAVE_SHLLOAD)
44 #define wxDllOpen(lib) shl_open(lib, BIND_DEFERRED, 0)
45 #define wxDllClose shl_unload
47 static inline void *wxDllGetSymbol(shl_t
*handle
, const char *name
)
50 if ( shl_findsym(handle
, name
, TYPE_UNDEFINED
, &sym
) == 0 )
55 #elif defined(__WINDOWS__)
56 #define wxDllOpen(lib) ::LoadLibrary(lib)
57 #define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
58 #define wxDllClose ::FreeLibrary
60 #error "Don't know how to load shared libraries on this platform."
63 // ---------------------------------------------------------------------------
65 // ---------------------------------------------------------------------------
67 wxLibraries wxTheLibraries
;
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // construct the full name from the base shared object name: adds a .dll
74 // suffix under Windows or .so under Unix
75 static wxString
ConstructLibraryName(const wxString
& basename
)
77 wxString
fullname(basename
);
81 #elif defined(__WINDOWS__)
88 // ============================================================================
90 // ============================================================================
92 // ---------------------------------------------------------------------------
93 // wxLibrary (one instance per dynamic library)
94 // ---------------------------------------------------------------------------
96 wxLibrary::wxLibrary(wxDllType handle
)
98 typedef wxClassInfo
*(*t_get_first
)(void);
99 t_get_first get_first
;
103 // Some system may use a local heap for library.
104 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
105 // It is a wxWindows DLL.
107 PrepareClasses(get_first());
110 wxLibrary::~wxLibrary()
114 wxDllClose(m_handle
);
118 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
120 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
125 return info
->CreateObject();
128 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
130 // Index all class infos by their class name
131 wxClassInfo
*info
= first
;
134 if (info
->m_className
)
135 classTable
.Put(info
->m_className
, (wxObject
*)info
);
136 info
= info
->GetNext();
139 // Set base pointers for each wxClassInfo
143 if (info
->GetBaseClassName1())
144 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
145 if (info
->GetBaseClassName2())
146 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
151 void *wxLibrary::GetSymbol(const wxString
& symbname
)
153 void *symbol
= NULL
; // return value
155 #if defined( __WXMAC__ )
157 CFragSymbolClass symClass
;
160 strcpy( (char*) symName
, symbname
) ;
161 c2pstr( (char*) symName
) ;
163 if ( FindSymbol( m_handle
, symName
, &symAddress
, &symClass
) == noErr
)
165 symbol
= (void *)symAddress
;
168 // VZ: hmm... why is WXSTRINGCAST needed? if it's really modified, we
169 // should make a copy of it
170 symbol
= wxDllGetSymbol(m_handle
, WXSTRINGCAST symbname
);
175 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
182 // ---------------------------------------------------------------------------
183 // wxLibraries (only one instance should normally exist)
184 // ---------------------------------------------------------------------------
186 wxLibraries::wxLibraries()
190 wxLibraries::~wxLibraries()
192 wxNode
*node
= m_loaded
.First();
195 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
202 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
206 wxClassInfo
*old_sm_first
;
208 if ( (node
= m_loaded
.Find(name
.GetData())) )
209 return ((wxLibrary
*)node
->Data());
211 // If DLL shares data, this is necessary.
212 old_sm_first
= wxClassInfo::sm_first
;
213 wxClassInfo::sm_first
= NULL
;
215 wxString lib_name
= ConstructLibraryName(name
);
217 #if defined(__UNIX__)
218 // TODO use LD_LIBRARY_PATH!
219 lib_name
.Prepend("/lib");
224 #if defined(__WXMAC__)
229 wxMacPathToFSSpec( lib_name
, &myFSSpec
) ;
230 if (GetDiskFragment( &myFSSpec
, 0 , kCFragGoesToEOF
, "\p" , kPrivateCFragCopy
, &handle
, &myMainAddr
,
231 myErrName
) != noErr
)
233 p2cstr( myErrName
) ;
234 wxASSERT_MSG( 1 , (char*)myErrName
) ;
238 handle
= wxDllOpen(lib_name
);
243 wxLogSysError(_("Failed to load shared library '%s'"),
249 lib
= new wxLibrary(handle
);
251 wxClassInfo::sm_first
= old_sm_first
;
253 m_loaded
.Append(name
.GetData(), lib
);
258 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
260 wxNode
*node
= m_loaded
.First();
264 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);