]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: dynlib.cpp | |
3 | // Purpose: Dynamic library management | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 20/07/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | # pragma implementation "dynlib.h" | |
22 | #endif | |
23 | ||
24 | #include "wx/wxprec.h" | |
25 | ||
26 | #ifdef __BORLANDC__ | |
27 | #pragma hdrstop | |
28 | #endif | |
29 | ||
30 | #if wxUSE_DYNLIB_CLASS | |
31 | ||
32 | #include "wx/dynlib.h" | |
33 | #include "wx/filefn.h" | |
34 | #include "wx/intl.h" | |
35 | #include "wx/log.h" | |
36 | #include "wx/tokenzr.h" | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // conditional compilation | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | #if defined(HAVE_DLOPEN) | |
43 | # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_NOW/*RTLD_LAZY*/) | |
44 | # define wxDllGetSymbol(handle, name) dlsym(handle, name.mb_str()) | |
45 | # define wxDllClose dlclose | |
46 | #elif defined(HAVE_SHL_LOAD) | |
47 | # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0) | |
48 | # define wxDllClose shl_unload | |
49 | ||
50 | static inline void *wxDllGetSymbol(shl_t handle, const wxString& name) | |
51 | { | |
52 | void *sym; | |
53 | if ( shl_findsym(&handle, name.mb_str(), TYPE_UNDEFINED, &sym) == 0 ) | |
54 | return sym; | |
55 | else | |
56 | return (void *)0; | |
57 | } | |
58 | #elif defined(__WINDOWS__) | |
59 | # include <windows.h> | |
60 | ||
61 | // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary | |
62 | # ifdef __WIN32__ | |
63 | # define wxDllOpen(lib) ::LoadLibraryEx(lib, 0, 0) | |
64 | # else // Win16 | |
65 | # define wxDllOpen(lib) ::LoadLibrary(lib) | |
66 | # endif // Win32/16 | |
67 | # define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name) | |
68 | # define wxDllClose ::FreeLibrary | |
69 | #else | |
70 | # error "Don't know how to load shared libraries on this platform." | |
71 | #endif // OS | |
72 | ||
73 | // --------------------------------------------------------------------------- | |
74 | // Global variables | |
75 | // --------------------------------------------------------------------------- | |
76 | ||
77 | wxLibraries wxTheLibraries; | |
78 | ||
79 | // ---------------------------------------------------------------------------- | |
80 | // private functions | |
81 | // ---------------------------------------------------------------------------- | |
82 | ||
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) | |
86 | { | |
87 | wxString fullname(basename); | |
88 | ||
89 | #if defined(__UNIX__) | |
90 | # if defined(__HPUX__) | |
91 | fullname << ".sl"; | |
92 | # else //__HPUX__ | |
93 | fullname << ".so"; | |
94 | # endif //__HPUX__ | |
95 | #elif defined(__WINDOWS__) | |
96 | fullname << ".dll"; | |
97 | #endif | |
98 | ||
99 | return fullname; | |
100 | } | |
101 | ||
102 | // ============================================================================ | |
103 | // implementation | |
104 | // ============================================================================ | |
105 | ||
106 | ||
107 | // --------------------------------------------------------------------------- | |
108 | // wxLibrary (one instance per dynamic library) | |
109 | // --------------------------------------------------------------------------- | |
110 | ||
111 | wxLibrary::wxLibrary(wxDllType handle) | |
112 | { | |
113 | typedef wxClassInfo *(*t_get_first)(void); | |
114 | t_get_first get_first; | |
115 | ||
116 | m_handle = handle; | |
117 | ||
118 | // Some system may use a local heap for library. | |
119 | get_first = (t_get_first)GetSymbol("wxGetClassFirst"); | |
120 | // It is a wxWindows DLL. | |
121 | if (get_first) | |
122 | PrepareClasses(get_first()); | |
123 | } | |
124 | ||
125 | wxLibrary::~wxLibrary() | |
126 | { | |
127 | if ( m_handle ) | |
128 | { | |
129 | wxDllClose(m_handle); | |
130 | } | |
131 | } | |
132 | ||
133 | wxObject *wxLibrary::CreateObject(const wxString& name) | |
134 | { | |
135 | wxClassInfo *info = (wxClassInfo *)classTable.Get(name); | |
136 | ||
137 | if (!info) | |
138 | return NULL; | |
139 | ||
140 | return info->CreateObject(); | |
141 | } | |
142 | ||
143 | void wxLibrary::PrepareClasses(wxClassInfo *first) | |
144 | { | |
145 | // Index all class infos by their class name | |
146 | wxClassInfo *info = first; | |
147 | while (info) | |
148 | { | |
149 | if (info->m_className) | |
150 | classTable.Put(info->m_className, (wxObject *)info); | |
151 | info = info->GetNext(); | |
152 | } | |
153 | ||
154 | // Set base pointers for each wxClassInfo | |
155 | info = first; | |
156 | while (info) | |
157 | { | |
158 | if (info->GetBaseClassName1()) | |
159 | info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1()); | |
160 | if (info->GetBaseClassName2()) | |
161 | info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2()); | |
162 | info = info->m_next; | |
163 | } | |
164 | } | |
165 | ||
166 | void *wxLibrary::GetSymbol(const wxString& symbname) | |
167 | { | |
168 | return wxDllLoader::GetSymbol(m_handle, symbname); | |
169 | } | |
170 | ||
171 | // --------------------------------------------------------------------------- | |
172 | // wxDllLoader | |
173 | // --------------------------------------------------------------------------- | |
174 | ||
175 | /* static */ | |
176 | wxDllType | |
177 | wxDllLoader::GetProgramHandle(void) | |
178 | { | |
179 | #ifdef __UNIX__ | |
180 | return dlopen(NULL, RTLD_NOW/*RTLD_LAZY*/); | |
181 | #else | |
182 | wxFAIL_MSG(_("This method is not implemented under Windows")); | |
183 | ||
184 | return 0; | |
185 | #endif | |
186 | } | |
187 | ||
188 | ||
189 | /* static */ | |
190 | wxDllType | |
191 | wxDllLoader::LoadLibrary(const wxString & lib_name, bool *success) | |
192 | { | |
193 | wxASSERT(success); | |
194 | ||
195 | wxDllType handle; | |
196 | ||
197 | #if defined(__WXMAC__) | |
198 | FSSpec myFSSpec ; | |
199 | Ptr myMainAddr ; | |
200 | Str255 myErrName ; | |
201 | ||
202 | wxMacPathToFSSpec( lib_name , &myFSSpec ) ; | |
203 | if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr , | |
204 | myErrName ) != noErr ) | |
205 | { | |
206 | p2cstr( myErrName ) ; | |
207 | wxASSERT_MSG( 1 , (char*)myErrName ) ; | |
208 | return NULL ; | |
209 | } | |
210 | #else // !Mac | |
211 | handle = wxDllOpen(lib_name); | |
212 | #endif // OS | |
213 | ||
214 | if ( !handle ) | |
215 | { | |
216 | wxLogSysError(_("Failed to load shared library '%s'"), | |
217 | lib_name.c_str()); | |
218 | *success = FALSE; | |
219 | return NULL; | |
220 | } | |
221 | *success = TRUE; | |
222 | return handle; | |
223 | } | |
224 | ||
225 | ||
226 | /* static */ | |
227 | void | |
228 | wxDllLoader::UnloadLibrary(wxDllType handle) | |
229 | { | |
230 | wxDllClose(handle); | |
231 | } | |
232 | ||
233 | /* static */ | |
234 | void * | |
235 | wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name) | |
236 | { | |
237 | void *symbol = NULL; // return value | |
238 | ||
239 | #if defined( __WXMAC__ ) | |
240 | Ptr symAddress ; | |
241 | CFragSymbolClass symClass ; | |
242 | Str255 symName ; | |
243 | ||
244 | strcpy( (char*) symName , name ) ; | |
245 | c2pstr( (char*) symName ) ; | |
246 | ||
247 | if ( FindSymbol( dllHandle , symName , &symAddress , &symClass ) == noErr ) | |
248 | symbol = (void *)symAddress ; | |
249 | #else | |
250 | symbol = wxDllGetSymbol(dllHandle, name); | |
251 | #endif | |
252 | ||
253 | if ( !symbol ) | |
254 | { | |
255 | wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"), | |
256 | name.c_str()); | |
257 | } | |
258 | return symbol; | |
259 | } | |
260 | ||
261 | // --------------------------------------------------------------------------- | |
262 | // wxLibraries (only one instance should normally exist) | |
263 | // --------------------------------------------------------------------------- | |
264 | ||
265 | wxLibraries::wxLibraries() | |
266 | { | |
267 | } | |
268 | ||
269 | wxLibraries::~wxLibraries() | |
270 | { | |
271 | wxNode *node = m_loaded.First(); | |
272 | ||
273 | while (node) { | |
274 | wxLibrary *lib = (wxLibrary *)node->Data(); | |
275 | delete lib; | |
276 | ||
277 | node = node->Next(); | |
278 | } | |
279 | } | |
280 | ||
281 | wxLibrary *wxLibraries::LoadLibrary(const wxString& name) | |
282 | { | |
283 | wxNode *node; | |
284 | wxLibrary *lib; | |
285 | wxClassInfo *old_sm_first; | |
286 | ||
287 | if ( (node = m_loaded.Find(name.GetData())) ) | |
288 | return ((wxLibrary *)node->Data()); | |
289 | ||
290 | // If DLL shares data, this is necessary. | |
291 | old_sm_first = wxClassInfo::sm_first; | |
292 | wxClassInfo::sm_first = NULL; | |
293 | ||
294 | wxString lib_name = ConstructLibraryName(name); | |
295 | ||
296 | /* | |
297 | Unix automatically builds that library name, at least for dlopen() | |
298 | */ | |
299 | #if 0 | |
300 | #if defined(__UNIX__) | |
301 | // found the first file in LD_LIBRARY_PATH with this name | |
302 | wxString libPath("/lib:/usr/lib"); // system path first | |
303 | const char *envLibPath = getenv("LD_LIBRARY_PATH"); | |
304 | if ( envLibPath ) | |
305 | libPath << ':' << envLibPath; | |
306 | wxStringTokenizer tokenizer(libPath, _T(':')); | |
307 | while ( tokenizer.HasMoreToken() ) | |
308 | { | |
309 | wxString fullname(tokenizer.NextToken()); | |
310 | ||
311 | fullname << '/' << lib_name; | |
312 | if ( wxFileExists(fullname) ) | |
313 | { | |
314 | lib_name = fullname; | |
315 | ||
316 | // found the library | |
317 | break; | |
318 | } | |
319 | } | |
320 | //else: not found in the path, leave the name as is (secutiry risk?) | |
321 | ||
322 | #endif // __UNIX__ | |
323 | #endif | |
324 | ||
325 | bool success = FALSE; | |
326 | wxDllType handle = wxDllLoader::LoadLibrary(lib_name, &success); | |
327 | if(success) | |
328 | { | |
329 | lib = new wxLibrary(handle); | |
330 | wxClassInfo::sm_first = old_sm_first; | |
331 | m_loaded.Append(name.GetData(), lib); | |
332 | } | |
333 | else | |
334 | lib = NULL; | |
335 | return lib; | |
336 | } | |
337 | ||
338 | wxObject *wxLibraries::CreateObject(const wxString& path) | |
339 | { | |
340 | wxNode *node = m_loaded.First(); | |
341 | wxObject *obj; | |
342 | ||
343 | while (node) { | |
344 | obj = ((wxLibrary *)node->Data())->CreateObject(path); | |
345 | if (obj) | |
346 | return obj; | |
347 | ||
348 | node = node->Next(); | |
349 | } | |
350 | return NULL; | |
351 | } | |
352 | ||
353 | #endif // wxUSE_DYNLIB_CLASS |