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__)
59 #define wxDllOpen(lib) ::LoadLibrary(lib)
60 #define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
61 #define wxDllClose ::FreeLibrary
63 #error "Don't know how to load shared libraries on this platform."
66 // ---------------------------------------------------------------------------
68 // ---------------------------------------------------------------------------
70 wxLibraries wxTheLibraries
;
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 // construct the full name from the base shared object name: adds a .dll
77 // suffix under Windows or .so under Unix
78 static wxString
ConstructLibraryName(const wxString
& basename
)
80 wxString
fullname(basename
);
88 #elif defined(__WINDOWS__)
95 // ============================================================================
97 // ============================================================================
99 // ---------------------------------------------------------------------------
100 // wxLibrary (one instance per dynamic library)
101 // ---------------------------------------------------------------------------
103 wxLibrary::wxLibrary(wxDllType handle
)
105 typedef wxClassInfo
*(*t_get_first
)(void);
106 t_get_first get_first
;
110 // Some system may use a local heap for library.
111 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
112 // It is a wxWindows DLL.
114 PrepareClasses(get_first());
117 wxLibrary::~wxLibrary()
121 wxDllClose(m_handle
);
125 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
127 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
132 return info
->CreateObject();
135 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
137 // Index all class infos by their class name
138 wxClassInfo
*info
= first
;
141 if (info
->m_className
)
142 classTable
.Put(info
->m_className
, (wxObject
*)info
);
143 info
= info
->GetNext();
146 // Set base pointers for each wxClassInfo
150 if (info
->GetBaseClassName1())
151 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
152 if (info
->GetBaseClassName2())
153 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
158 void *wxLibrary::GetSymbol(const wxString
& symbname
)
160 void *symbol
= NULL
; // return value
162 #if defined( __WXMAC__ )
164 CFragSymbolClass symClass
;
167 strcpy( (char*) symName
, symbname
) ;
168 c2pstr( (char*) symName
) ;
170 if ( FindSymbol( m_handle
, symName
, &symAddress
, &symClass
) == noErr
)
172 symbol
= (void *)symAddress
;
175 // VZ: hmm... why is WXSTRINGCAST needed? if it's really modified, we
176 // should make a copy of it
177 symbol
= wxDllGetSymbol(m_handle
, WXSTRINGCAST symbname
);
182 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
189 // ---------------------------------------------------------------------------
190 // wxLibraries (only one instance should normally exist)
191 // ---------------------------------------------------------------------------
193 wxLibraries::wxLibraries()
197 wxLibraries::~wxLibraries()
199 wxNode
*node
= m_loaded
.First();
202 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
209 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
213 wxClassInfo
*old_sm_first
;
215 if ( (node
= m_loaded
.Find(name
.GetData())) )
216 return ((wxLibrary
*)node
->Data());
218 // If DLL shares data, this is necessary.
219 old_sm_first
= wxClassInfo::sm_first
;
220 wxClassInfo::sm_first
= NULL
;
222 wxString lib_name
= ConstructLibraryName(name
);
224 #if defined(__UNIX__)
225 // found the first file in LD_LIBRARY_PATH with this name
226 wxString
libPath("/lib:/usr/lib"); // system path first
227 const char *envLibPath
= getenv("LD_LIBRARY_PATH");
229 libPath
<< ':' << envLibPath
;
230 wxStringTokenizer
tokenizer(libPath
, ':');
231 while ( tokenizer
.HasMoreToken() )
233 wxString
fullname(tokenizer
.NextToken());
235 fullname
<< '/' << lib_name
;
236 if ( wxFileExists(fullname
) )
244 //else: not found in the path, leave the name as is (secutiry risk?)
250 #if defined(__WXMAC__)
255 wxMacPathToFSSpec( lib_name
, &myFSSpec
) ;
256 if (GetDiskFragment( &myFSSpec
, 0 , kCFragGoesToEOF
, "\p" , kPrivateCFragCopy
, &handle
, &myMainAddr
,
257 myErrName
) != noErr
)
259 p2cstr( myErrName
) ;
260 wxASSERT_MSG( 1 , (char*)myErrName
) ;
264 handle
= wxDllOpen(lib_name
);
269 wxLogSysError(_("Failed to load shared library '%s'"),
275 lib
= new wxLibrary(handle
);
277 wxClassInfo::sm_first
= old_sm_first
;
279 m_loaded
.Append(name
.GetData(), lib
);
284 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
286 wxNode
*node
= m_loaded
.First();
290 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
299 #endif // wxUSE_DYNLIB_CLASS