]>
Commit | Line | Data |
---|---|---|
7a4b9130 GL |
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 | ||
7b0bfbb2 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
7a4b9130 | 20 | #ifdef __GNUG__ |
a585ca5c | 21 | # pragma implementation "dynlib.h" |
7a4b9130 GL |
22 | #endif |
23 | ||
0c32066b JS |
24 | #include "wx/wxprec.h" |
25 | ||
2df7be7f RR |
26 | #ifdef __BORLANDC__ |
27 | #pragma hdrstop | |
28 | #endif | |
f6bcfd97 | 29 | |
8a0d4cf6 VZ |
30 | #if wxUSE_DYNLIB_CLASS |
31 | ||
f6bcfd97 BP |
32 | #if defined(__WINDOWS__) |
33 | #include "wx/msw/private.h" | |
34 | #endif | |
35 | ||
7b0bfbb2 VZ |
36 | #include "wx/dynlib.h" |
37 | #include "wx/filefn.h" | |
38 | #include "wx/intl.h" | |
39 | #include "wx/log.h" | |
8a0d4cf6 | 40 | #include "wx/tokenzr.h" |
7b0bfbb2 VZ |
41 | |
42 | // ---------------------------------------------------------------------------- | |
43 | // conditional compilation | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
1a787c5d SN |
46 | #if defined(__WXPM__) || defined(__EMX__) |
47 | # define INCL_DOS | |
48 | # include <os2.h> | |
49 | # define wxDllOpen(error, lib, handle) DosLoadModule(error, sizeof(error), lib, &handle) | |
50 | # define wxDllGetSymbol(handle, modaddr) DosQueryProcAddr(handle, 1L, NULL, (PFN*)modaddr) | |
51 | # define wxDllClose(handle) DosFreeModule(handle) | |
52 | #elif defined(HAVE_DLOPEN) | |
237c5c02 VZ |
53 | // note about dlopen() flags: we use RTLD_NOW to have more Windows-like |
54 | // behaviour (Win won't let you load a library with missing symbols) and | |
55 | // RTLD_GLOBAL because it is needed sometimes and probably doesn't hurt | |
99d96a2b JJ |
56 | // otherwise. On VMS the second argument on dlopen is ignored. |
57 | #ifdef __VMS | |
58 | # define wxDllOpen(lib) dlopen(lib.fn_str(), 0 ) | |
59 | #else | |
60 | # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_LAZY | RTLD_GLOBAL) | |
61 | #endif | |
62 | #define wxDllGetSymbol(handle, name) dlsym(handle, name) | |
a585ca5c | 63 | # define wxDllClose dlclose |
8a0d4cf6 | 64 | #elif defined(HAVE_SHL_LOAD) |
a585ca5c | 65 | # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0) |
237c5c02 | 66 | # define wxDllClose shl_unload |
846e1424 | 67 | |
50920146 | 68 | static inline void *wxDllGetSymbol(shl_t handle, const wxString& name) |
7b0bfbb2 VZ |
69 | { |
70 | void *sym; | |
50920146 | 71 | if ( shl_findsym(&handle, name.mb_str(), TYPE_UNDEFINED, &sym) == 0 ) |
7b0bfbb2 VZ |
72 | return sym; |
73 | else | |
74 | return (void *)0; | |
75 | } | |
76 | #elif defined(__WINDOWS__) | |
90022ddc | 77 | // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary |
a585ca5c | 78 | # ifdef __WIN32__ |
f6bcfd97 BP |
79 | #ifdef _UNICODE |
80 | # define wxDllOpen(lib) ::LoadLibraryExW(lib, 0, 0) | |
81 | #else | |
82 | # define wxDllOpen(lib) ::LoadLibraryExA(lib, 0, 0) | |
83 | #endif | |
a585ca5c KB |
84 | # else // Win16 |
85 | # define wxDllOpen(lib) ::LoadLibrary(lib) | |
86 | # endif // Win32/16 | |
87 | # define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name) | |
88 | # define wxDllClose ::FreeLibrary | |
7b0bfbb2 | 89 | #else |
a585ca5c | 90 | # error "Don't know how to load shared libraries on this platform." |
7b0bfbb2 | 91 | #endif // OS |
7a4b9130 GL |
92 | |
93 | // --------------------------------------------------------------------------- | |
7b0bfbb2 | 94 | // Global variables |
7a4b9130 GL |
95 | // --------------------------------------------------------------------------- |
96 | ||
7b0bfbb2 | 97 | wxLibraries wxTheLibraries; |
7a4b9130 | 98 | |
f6bcfd97 BP |
99 | // ============================================================================ |
100 | // implementation | |
101 | // ============================================================================ | |
123a7fdd | 102 | |
7b0bfbb2 VZ |
103 | // construct the full name from the base shared object name: adds a .dll |
104 | // suffix under Windows or .so under Unix | |
105 | static wxString ConstructLibraryName(const wxString& basename) | |
106 | { | |
f6bcfd97 BP |
107 | wxString fullname; |
108 | fullname << basename << wxDllLoader::GetDllExt(); | |
27529614 | 109 | |
7b0bfbb2 VZ |
110 | return fullname; |
111 | } | |
7a4b9130 | 112 | |
7a4b9130 | 113 | // --------------------------------------------------------------------------- |
7b0bfbb2 | 114 | // wxLibrary (one instance per dynamic library) |
7a4b9130 GL |
115 | // --------------------------------------------------------------------------- |
116 | ||
7b0bfbb2 | 117 | wxLibrary::wxLibrary(wxDllType handle) |
7a4b9130 | 118 | { |
7b0bfbb2 VZ |
119 | typedef wxClassInfo *(*t_get_first)(void); |
120 | t_get_first get_first; | |
7a4b9130 | 121 | |
7b0bfbb2 | 122 | m_handle = handle; |
7a4b9130 | 123 | |
7b0bfbb2 VZ |
124 | // Some system may use a local heap for library. |
125 | get_first = (t_get_first)GetSymbol("wxGetClassFirst"); | |
126 | // It is a wxWindows DLL. | |
127 | if (get_first) | |
128 | PrepareClasses(get_first()); | |
7a4b9130 GL |
129 | } |
130 | ||
131 | wxLibrary::~wxLibrary() | |
132 | { | |
7b0bfbb2 VZ |
133 | if ( m_handle ) |
134 | { | |
135 | wxDllClose(m_handle); | |
136 | } | |
7a4b9130 GL |
137 | } |
138 | ||
139 | wxObject *wxLibrary::CreateObject(const wxString& name) | |
140 | { | |
7b0bfbb2 | 141 | wxClassInfo *info = (wxClassInfo *)classTable.Get(name); |
f4a8c29f | 142 | |
7b0bfbb2 VZ |
143 | if (!info) |
144 | return NULL; | |
f4a8c29f | 145 | |
7b0bfbb2 | 146 | return info->CreateObject(); |
f4a8c29f GL |
147 | } |
148 | ||
856d2e52 | 149 | void wxLibrary::PrepareClasses(wxClassInfo *first) |
f4a8c29f | 150 | { |
7b0bfbb2 VZ |
151 | // Index all class infos by their class name |
152 | wxClassInfo *info = first; | |
153 | while (info) | |
154 | { | |
155 | if (info->m_className) | |
156 | classTable.Put(info->m_className, (wxObject *)info); | |
157 | info = info->GetNext(); | |
158 | } | |
159 | ||
160 | // Set base pointers for each wxClassInfo | |
161 | info = first; | |
162 | while (info) | |
163 | { | |
164 | if (info->GetBaseClassName1()) | |
165 | info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1()); | |
166 | if (info->GetBaseClassName2()) | |
167 | info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2()); | |
168 | info = info->m_next; | |
169 | } | |
7a4b9130 GL |
170 | } |
171 | ||
172 | void *wxLibrary::GetSymbol(const wxString& symbname) | |
173 | { | |
a585ca5c KB |
174 | return wxDllLoader::GetSymbol(m_handle, symbname); |
175 | } | |
7b0bfbb2 | 176 | |
a585ca5c KB |
177 | // --------------------------------------------------------------------------- |
178 | // wxDllLoader | |
179 | // --------------------------------------------------------------------------- | |
7b0bfbb2 | 180 | |
f6bcfd97 BP |
181 | /* static */ |
182 | wxString wxDllLoader::GetDllExt() | |
183 | { | |
184 | wxString ext; | |
185 | ||
186 | #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__) | |
187 | ext = _T(".dll"); | |
188 | #elif defined(__UNIX__) | |
189 | # if defined(__HPUX__) | |
190 | ext = _T(".sl"); | |
191 | # else //__HPUX__ | |
192 | ext = _T(".so"); | |
193 | # endif //__HPUX__ | |
194 | #endif | |
195 | ||
196 | return ext; | |
197 | } | |
198 | ||
0868079c KB |
199 | /* static */ |
200 | wxDllType | |
201 | wxDllLoader::GetProgramHandle(void) | |
202 | { | |
1a787c5d | 203 | #if defined( HAVE_DLOPEN ) && !defined(__EMX__) |
7742efff KB |
204 | // optain handle for main program |
205 | return dlopen(NULL, RTLD_NOW/*RTLD_LAZY*/); | |
206 | #elif defined (HAVE_SHL_LOAD) | |
207 | // shl_findsymbol with NULL handle looks up in main program | |
208 | return 0; | |
0868079c | 209 | #else |
58c837a4 | 210 | wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2")); |
7742efff | 211 | return 0; |
7cc98b3e | 212 | #endif |
0868079c KB |
213 | } |
214 | ||
a585ca5c KB |
215 | /* static */ |
216 | wxDllType | |
7cc98b3e | 217 | wxDllLoader::LoadLibrary(const wxString & libname, bool *success) |
a585ca5c | 218 | { |
7cc98b3e | 219 | wxDllType handle; |
7b0bfbb2 | 220 | |
a585ca5c | 221 | #if defined(__WXMAC__) |
7cc98b3e | 222 | FSSpec myFSSpec ; |
f6bcfd97 BP |
223 | Ptr myMainAddr ; |
224 | Str255 myErrName ; | |
7cc98b3e VZ |
225 | |
226 | wxMacPathToFSSpec( libname , &myFSSpec ) ; | |
227 | if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr , | |
228 | myErrName ) != noErr ) | |
229 | { | |
230 | p2cstr( myErrName ) ; | |
231 | wxASSERT_MSG( 1 , (char*)myErrName ) ; | |
232 | return NULL ; | |
233 | } | |
1a787c5d | 234 | #elif defined(__WXPM__) || defined(__EMX__) |
f6bcfd97 | 235 | char zError[256] = ""; |
c2ff79b1 | 236 | wxDllOpen(zError, libname, handle); |
a585ca5c | 237 | #else // !Mac |
7cc98b3e | 238 | handle = wxDllOpen(libname); |
a585ca5c KB |
239 | #endif // OS |
240 | ||
7cc98b3e VZ |
241 | if ( !handle ) |
242 | { | |
f6bcfd97 BP |
243 | wxString msg(_("Failed to load shared library '%s'")); |
244 | ||
245 | #ifdef HAVE_DLERROR | |
246 | const char *errmsg = dlerror(); | |
247 | if ( errmsg ) | |
248 | { | |
249 | // the error string format is "libname: ...", but we already have | |
250 | // libname, so cut it off | |
251 | const char *p = strchr(errmsg, ':'); | |
252 | if ( p ) | |
253 | { | |
254 | if ( *++p == ' ' ) | |
255 | p++; | |
256 | } | |
257 | else | |
258 | { | |
259 | p = errmsg; | |
260 | } | |
261 | ||
262 | msg += _T(" (%s)"); | |
263 | wxLogError(msg, libname.c_str(), p); | |
264 | } | |
265 | else | |
266 | #endif // HAVE_DLERROR | |
267 | { | |
268 | wxLogSysError(msg, libname.c_str()); | |
269 | } | |
7cc98b3e VZ |
270 | } |
271 | ||
272 | if ( success ) | |
273 | { | |
274 | *success = handle != 0; | |
275 | } | |
276 | ||
277 | return handle; | |
a585ca5c KB |
278 | } |
279 | ||
752c7d6b KB |
280 | |
281 | /* static */ | |
282 | void | |
283 | wxDllLoader::UnloadLibrary(wxDllType handle) | |
284 | { | |
285 | wxDllClose(handle); | |
286 | } | |
287 | ||
a585ca5c KB |
288 | /* static */ |
289 | void * | |
290 | wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name) | |
291 | { | |
7cc98b3e | 292 | void *symbol = NULL; // return value |
a585ca5c KB |
293 | |
294 | #if defined( __WXMAC__ ) | |
7cc98b3e VZ |
295 | Ptr symAddress ; |
296 | CFragSymbolClass symClass ; | |
f6bcfd97 | 297 | Str255 symName ; |
7cc98b3e VZ |
298 | |
299 | strcpy( (char*) symName , name ) ; | |
300 | c2pstr( (char*) symName ) ; | |
301 | ||
302 | if ( FindSymbol( dllHandle , symName , &symAddress , &symClass ) == noErr ) | |
303 | symbol = (void *)symAddress ; | |
1a787c5d | 304 | #elif defined( __WXPM__ ) || defined(__EMX__) |
c2ff79b1 | 305 | wxDllGetSymbol(dllHandle, symbol); |
7b0bfbb2 | 306 | #else |
f6bcfd97 BP |
307 | // mb_str() is necessary in Unicode build |
308 | symbol = wxDllGetSymbol(dllHandle, name.mb_str()); | |
123a7fdd | 309 | #endif |
7b0bfbb2 VZ |
310 | |
311 | if ( !symbol ) | |
312 | { | |
7cc98b3e VZ |
313 | wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"), |
314 | name.c_str()); | |
7b0bfbb2 | 315 | } |
7b0bfbb2 | 316 | return symbol; |
7a4b9130 GL |
317 | } |
318 | ||
319 | // --------------------------------------------------------------------------- | |
320 | // wxLibraries (only one instance should normally exist) | |
321 | // --------------------------------------------------------------------------- | |
322 | ||
65fd5cb0 | 323 | wxLibraries::wxLibraries():m_loaded(wxKEY_STRING) |
7a4b9130 GL |
324 | { |
325 | } | |
326 | ||
327 | wxLibraries::~wxLibraries() | |
328 | { | |
7b0bfbb2 | 329 | wxNode *node = m_loaded.First(); |
7a4b9130 | 330 | |
7b0bfbb2 VZ |
331 | while (node) { |
332 | wxLibrary *lib = (wxLibrary *)node->Data(); | |
333 | delete lib; | |
7a4b9130 | 334 | |
7b0bfbb2 VZ |
335 | node = node->Next(); |
336 | } | |
7a4b9130 GL |
337 | } |
338 | ||
339 | wxLibrary *wxLibraries::LoadLibrary(const wxString& name) | |
340 | { | |
7b0bfbb2 VZ |
341 | wxNode *node; |
342 | wxLibrary *lib; | |
343 | wxClassInfo *old_sm_first; | |
7a4b9130 | 344 | |
c2ff79b1 DW |
345 | #if defined(__VISAGECPP__) |
346 | node = m_loaded.Find(name.GetData()); | |
347 | if (node != NULL) | |
348 | return ((wxLibrary *)node->Data()); | |
349 | #else // !OS/2 | |
7b0bfbb2 VZ |
350 | if ( (node = m_loaded.Find(name.GetData())) ) |
351 | return ((wxLibrary *)node->Data()); | |
c2ff79b1 | 352 | #endif |
7b0bfbb2 VZ |
353 | // If DLL shares data, this is necessary. |
354 | old_sm_first = wxClassInfo::sm_first; | |
355 | wxClassInfo::sm_first = NULL; | |
356 | ||
7cc98b3e | 357 | wxString libname = ConstructLibraryName(name); |
856d2e52 | 358 | |
a585ca5c KB |
359 | /* |
360 | Unix automatically builds that library name, at least for dlopen() | |
361 | */ | |
362 | #if 0 | |
9e4b2f1c | 363 | #if defined(__UNIX__) |
8a0d4cf6 VZ |
364 | // found the first file in LD_LIBRARY_PATH with this name |
365 | wxString libPath("/lib:/usr/lib"); // system path first | |
366 | const char *envLibPath = getenv("LD_LIBRARY_PATH"); | |
367 | if ( envLibPath ) | |
58c837a4 | 368 | libPath << wxT(':') << envLibPath; |
223d09f6 | 369 | wxStringTokenizer tokenizer(libPath, wxT(':')); |
8a0d4cf6 VZ |
370 | while ( tokenizer.HasMoreToken() ) |
371 | { | |
372 | wxString fullname(tokenizer.NextToken()); | |
373 | ||
58c837a4 | 374 | fullname << wxT('/') << libname; |
8a0d4cf6 VZ |
375 | if ( wxFileExists(fullname) ) |
376 | { | |
7cc98b3e | 377 | libname = fullname; |
8a0d4cf6 VZ |
378 | |
379 | // found the library | |
380 | break; | |
381 | } | |
382 | } | |
383 | //else: not found in the path, leave the name as is (secutiry risk?) | |
384 | ||
7b0bfbb2 | 385 | #endif // __UNIX__ |
a585ca5c | 386 | #endif |
7a4b9130 | 387 | |
4dc2c3bb | 388 | bool success = FALSE; |
7cc98b3e | 389 | wxDllType handle = wxDllLoader::LoadLibrary(libname, &success); |
a585ca5c | 390 | if(success) |
7b0bfbb2 | 391 | { |
a585ca5c KB |
392 | lib = new wxLibrary(handle); |
393 | wxClassInfo::sm_first = old_sm_first; | |
394 | m_loaded.Append(name.GetData(), lib); | |
7b0bfbb2 | 395 | } |
a585ca5c KB |
396 | else |
397 | lib = NULL; | |
7b0bfbb2 | 398 | return lib; |
7a4b9130 GL |
399 | } |
400 | ||
401 | wxObject *wxLibraries::CreateObject(const wxString& path) | |
402 | { | |
7b0bfbb2 VZ |
403 | wxNode *node = m_loaded.First(); |
404 | wxObject *obj; | |
7a4b9130 | 405 | |
7b0bfbb2 VZ |
406 | while (node) { |
407 | obj = ((wxLibrary *)node->Data())->CreateObject(path); | |
408 | if (obj) | |
409 | return obj; | |
7a4b9130 | 410 | |
7b0bfbb2 VZ |
411 | node = node->Next(); |
412 | } | |
413 | return NULL; | |
7a4b9130 | 414 | } |
8a0d4cf6 VZ |
415 | |
416 | #endif // wxUSE_DYNLIB_CLASS |