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 // TODO should be done by configure
31 #if defined(__UNIX__) && !(defined(HAVE_DLOPEN) || defined(HAVE_SHLLOAD))
32 #if defined(__LINUX__) || defined(__SOLARIS__) || defined(__SUNOS__)
36 #elif defined(__HPUX__)
40 #endif // Unix flavour
41 #endif // !Unix or already have some HAVE_xxx defined
43 #include "wx/dynlib.h"
44 #include "wx/filefn.h"
48 // ----------------------------------------------------------------------------
49 // conditional compilation
50 // ----------------------------------------------------------------------------
52 #if defined(HAVE_DLOPEN)
53 #define wxDllOpen(lib) dlopen(lib, RTLD_LAZY)
54 #define wxDllGetSymbol(handle, name) dlsym(handle, (char *)name)
55 #define wxDllClose dlclose
56 #elif defined(HAVE_SHLLOAD)
57 #define wxDllOpen(lib) shl_open(lib, BIND_DEFERRED, 0)
58 #define wxDllClose shl_unload
60 static inline void *wxDllGetSymbol(shl_t
*handle
, const char *name
)
63 if ( shl_findsym(handle
, name
, TYPE_UNDEFINED
, &sym
) == 0 )
68 #elif defined(__WINDOWS__)
69 #define wxDllOpen(lib) ::LoadLibrary(lib)
70 #define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
71 #define wxDllClose ::FreeLibrary
73 #error "Don't know how to load shared libraries on this platform."
76 // ---------------------------------------------------------------------------
78 // ---------------------------------------------------------------------------
80 wxLibraries wxTheLibraries
;
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 // construct the full name from the base shared object name: adds a .dll
87 // suffix under Windows or .so under Unix
88 static wxString
ConstructLibraryName(const wxString
& basename
)
90 wxString
fullname(basename
);
94 #elif defined(__WINDOWS__)
101 // ============================================================================
103 // ============================================================================
105 // ---------------------------------------------------------------------------
106 // wxLibrary (one instance per dynamic library)
107 // ---------------------------------------------------------------------------
109 wxLibrary::wxLibrary(wxDllType handle
)
111 typedef wxClassInfo
*(*t_get_first
)(void);
112 t_get_first get_first
;
116 // Some system may use a local heap for library.
117 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
118 // It is a wxWindows DLL.
120 PrepareClasses(get_first());
123 wxLibrary::~wxLibrary()
127 wxDllClose(m_handle
);
131 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
133 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
138 return info
->CreateObject();
141 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
143 // Index all class infos by their class name
144 wxClassInfo
*info
= first
;
147 if (info
->m_className
)
148 classTable
.Put(info
->m_className
, (wxObject
*)info
);
149 info
= info
->GetNext();
152 // Set base pointers for each wxClassInfo
156 if (info
->GetBaseClassName1())
157 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
158 if (info
->GetBaseClassName2())
159 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
164 void *wxLibrary::GetSymbol(const wxString
& symbname
)
166 void *symbol
= NULL
; // return value
168 #if defined( __WXMAC__ )
170 CFragSymbolClass symClass
;
173 strcpy( (char*) symName
, symbname
) ;
174 c2pstr( (char*) symName
) ;
176 if ( FindSymbol( m_handle
, symName
, &symAddress
, &symClass
) == noErr
)
178 symbol
= (void *)symAddress
;
181 // VZ: hmm... why is WXSTRINGCAST needed? if it's really modified, we
182 // should make a copy of it
183 symbol
= wxDllGetSymbol(m_handle
, WXSTRINGCAST symbname
);
188 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
195 // ---------------------------------------------------------------------------
196 // wxLibraries (only one instance should normally exist)
197 // ---------------------------------------------------------------------------
199 wxLibraries::wxLibraries()
203 wxLibraries::~wxLibraries()
205 wxNode
*node
= m_loaded
.First();
208 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
215 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
219 wxClassInfo
*old_sm_first
;
221 if ( (node
= m_loaded
.Find(name
.GetData())) )
222 return ((wxLibrary
*)node
->Data());
224 // If DLL shares data, this is necessary.
225 old_sm_first
= wxClassInfo::sm_first
;
226 wxClassInfo::sm_first
= NULL
;
228 wxString lib_name
= ConstructLibraryName(name
);
230 #if defined(__UNIX__)
231 // TODO use LD_LIBRARY_PATH!
232 lib_name
.Prepend("/lib");
237 #if defined(__WXMAC__)
242 wxMacPathToFSSpec( lib_name
, &myFSSpec
) ;
243 if (GetDiskFragment( &myFSSpec
, 0 , kCFragGoesToEOF
, "\p" , kPrivateCFragCopy
, &handle
, &myMainAddr
,
244 myErrName
) != noErr
)
246 p2cstr( myErrName
) ;
247 wxASSERT_MSG( 1 , (char*)myErrName
) ;
251 handle
= wxDllOpen(lib_name
);
256 wxLogSysError(_("Failed to load shared library '%s'"),
262 lib
= new wxLibrary(handle
);
264 wxClassInfo::sm_first
= old_sm_first
;
266 m_loaded
.Append(name
.GetData(), lib
);
271 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
273 wxNode
*node
= m_loaded
.First();
277 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);