| 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 | #if defined(__WINDOWS__) |
| 33 | #include "wx/msw/private.h" |
| 34 | #endif |
| 35 | |
| 36 | #include "wx/dynlib.h" |
| 37 | #include "wx/filefn.h" |
| 38 | #include "wx/intl.h" |
| 39 | #include "wx/log.h" |
| 40 | #include "wx/tokenzr.h" |
| 41 | |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | // conditional compilation |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | |
| 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) |
| 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 |
| 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) |
| 63 | # define wxDllClose dlclose |
| 64 | #elif defined(HAVE_SHL_LOAD) |
| 65 | # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0) |
| 66 | # define wxDllClose shl_unload |
| 67 | |
| 68 | static inline void *wxDllGetSymbol(shl_t handle, const wxString& name) |
| 69 | { |
| 70 | void *sym; |
| 71 | if ( shl_findsym(&handle, name.mb_str(), TYPE_UNDEFINED, &sym) == 0 ) |
| 72 | return sym; |
| 73 | else |
| 74 | return (void *)0; |
| 75 | } |
| 76 | #elif defined(__WINDOWS__) |
| 77 | // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary |
| 78 | # ifdef __WIN32__ |
| 79 | #ifdef _UNICODE |
| 80 | # define wxDllOpen(lib) ::LoadLibraryExW(lib, 0, 0) |
| 81 | #else |
| 82 | # define wxDllOpen(lib) ::LoadLibraryExA(lib, 0, 0) |
| 83 | #endif |
| 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 |
| 89 | #else |
| 90 | # error "Don't know how to load shared libraries on this platform." |
| 91 | #endif // OS |
| 92 | |
| 93 | // --------------------------------------------------------------------------- |
| 94 | // Global variables |
| 95 | // --------------------------------------------------------------------------- |
| 96 | |
| 97 | wxLibraries wxTheLibraries; |
| 98 | |
| 99 | // ============================================================================ |
| 100 | // implementation |
| 101 | // ============================================================================ |
| 102 | |
| 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 | { |
| 107 | wxString fullname; |
| 108 | fullname << basename << wxDllLoader::GetDllExt(); |
| 109 | |
| 110 | return fullname; |
| 111 | } |
| 112 | |
| 113 | // --------------------------------------------------------------------------- |
| 114 | // wxLibrary (one instance per dynamic library) |
| 115 | // --------------------------------------------------------------------------- |
| 116 | |
| 117 | wxLibrary::wxLibrary(wxDllType handle) |
| 118 | { |
| 119 | typedef wxClassInfo *(*t_get_first)(void); |
| 120 | t_get_first get_first; |
| 121 | |
| 122 | m_handle = handle; |
| 123 | |
| 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()); |
| 129 | } |
| 130 | |
| 131 | wxLibrary::~wxLibrary() |
| 132 | { |
| 133 | if ( m_handle ) |
| 134 | { |
| 135 | wxDllClose(m_handle); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | wxObject *wxLibrary::CreateObject(const wxString& name) |
| 140 | { |
| 141 | wxClassInfo *info = (wxClassInfo *)classTable.Get(name); |
| 142 | |
| 143 | if (!info) |
| 144 | return NULL; |
| 145 | |
| 146 | return info->CreateObject(); |
| 147 | } |
| 148 | |
| 149 | void wxLibrary::PrepareClasses(wxClassInfo *first) |
| 150 | { |
| 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 | } |
| 170 | } |
| 171 | |
| 172 | void *wxLibrary::GetSymbol(const wxString& symbname) |
| 173 | { |
| 174 | return wxDllLoader::GetSymbol(m_handle, symbname); |
| 175 | } |
| 176 | |
| 177 | // --------------------------------------------------------------------------- |
| 178 | // wxDllLoader |
| 179 | // --------------------------------------------------------------------------- |
| 180 | |
| 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 | |
| 199 | /* static */ |
| 200 | wxDllType |
| 201 | wxDllLoader::GetProgramHandle(void) |
| 202 | { |
| 203 | #if defined( HAVE_DLOPEN ) && !defined(__EMX__) |
| 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; |
| 209 | #else |
| 210 | wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2")); |
| 211 | return 0; |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | /* static */ |
| 216 | wxDllType |
| 217 | wxDllLoader::LoadLibrary(const wxString & libname, bool *success) |
| 218 | { |
| 219 | wxDllType handle; |
| 220 | |
| 221 | #if defined(__WXMAC__) |
| 222 | FSSpec myFSSpec ; |
| 223 | Ptr myMainAddr ; |
| 224 | Str255 myErrName ; |
| 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 | } |
| 234 | #elif defined(__WXPM__) || defined(__EMX__) |
| 235 | char zError[256] = ""; |
| 236 | wxDllOpen(zError, libname, handle); |
| 237 | #else // !Mac |
| 238 | handle = wxDllOpen(libname); |
| 239 | #endif // OS |
| 240 | |
| 241 | if ( !handle ) |
| 242 | { |
| 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 | } |
| 270 | } |
| 271 | |
| 272 | if ( success ) |
| 273 | { |
| 274 | *success = handle != 0; |
| 275 | } |
| 276 | |
| 277 | return handle; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /* static */ |
| 282 | void |
| 283 | wxDllLoader::UnloadLibrary(wxDllType handle) |
| 284 | { |
| 285 | wxDllClose(handle); |
| 286 | } |
| 287 | |
| 288 | /* static */ |
| 289 | void * |
| 290 | wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name) |
| 291 | { |
| 292 | void *symbol = NULL; // return value |
| 293 | |
| 294 | #if defined( __WXMAC__ ) |
| 295 | Ptr symAddress ; |
| 296 | CFragSymbolClass symClass ; |
| 297 | Str255 symName ; |
| 298 | |
| 299 | strcpy( (char*) symName , name ) ; |
| 300 | c2pstr( (char*) symName ) ; |
| 301 | |
| 302 | if ( FindSymbol( dllHandle , symName , &symAddress , &symClass ) == noErr ) |
| 303 | symbol = (void *)symAddress ; |
| 304 | #elif defined( __WXPM__ ) || defined(__EMX__) |
| 305 | wxDllGetSymbol(dllHandle, symbol); |
| 306 | #else |
| 307 | // mb_str() is necessary in Unicode build |
| 308 | symbol = wxDllGetSymbol(dllHandle, name.mb_str()); |
| 309 | #endif |
| 310 | |
| 311 | if ( !symbol ) |
| 312 | { |
| 313 | wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"), |
| 314 | name.c_str()); |
| 315 | } |
| 316 | return symbol; |
| 317 | } |
| 318 | |
| 319 | // --------------------------------------------------------------------------- |
| 320 | // wxLibraries (only one instance should normally exist) |
| 321 | // --------------------------------------------------------------------------- |
| 322 | |
| 323 | wxLibraries::wxLibraries():m_loaded(wxKEY_STRING) |
| 324 | { |
| 325 | } |
| 326 | |
| 327 | wxLibraries::~wxLibraries() |
| 328 | { |
| 329 | wxNode *node = m_loaded.First(); |
| 330 | |
| 331 | while (node) { |
| 332 | wxLibrary *lib = (wxLibrary *)node->Data(); |
| 333 | delete lib; |
| 334 | |
| 335 | node = node->Next(); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | wxLibrary *wxLibraries::LoadLibrary(const wxString& name) |
| 340 | { |
| 341 | wxNode *node; |
| 342 | wxLibrary *lib; |
| 343 | wxClassInfo *old_sm_first; |
| 344 | |
| 345 | #if defined(__VISAGECPP__) |
| 346 | node = m_loaded.Find(name.GetData()); |
| 347 | if (node != NULL) |
| 348 | return ((wxLibrary *)node->Data()); |
| 349 | #else // !OS/2 |
| 350 | if ( (node = m_loaded.Find(name.GetData())) ) |
| 351 | return ((wxLibrary *)node->Data()); |
| 352 | #endif |
| 353 | // If DLL shares data, this is necessary. |
| 354 | old_sm_first = wxClassInfo::sm_first; |
| 355 | wxClassInfo::sm_first = NULL; |
| 356 | |
| 357 | wxString libname = ConstructLibraryName(name); |
| 358 | |
| 359 | /* |
| 360 | Unix automatically builds that library name, at least for dlopen() |
| 361 | */ |
| 362 | #if 0 |
| 363 | #if defined(__UNIX__) |
| 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 ) |
| 368 | libPath << wxT(':') << envLibPath; |
| 369 | wxStringTokenizer tokenizer(libPath, wxT(':')); |
| 370 | while ( tokenizer.HasMoreToken() ) |
| 371 | { |
| 372 | wxString fullname(tokenizer.NextToken()); |
| 373 | |
| 374 | fullname << wxT('/') << libname; |
| 375 | if ( wxFileExists(fullname) ) |
| 376 | { |
| 377 | libname = fullname; |
| 378 | |
| 379 | // found the library |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | //else: not found in the path, leave the name as is (secutiry risk?) |
| 384 | |
| 385 | #endif // __UNIX__ |
| 386 | #endif |
| 387 | |
| 388 | bool success = FALSE; |
| 389 | wxDllType handle = wxDllLoader::LoadLibrary(libname, &success); |
| 390 | if(success) |
| 391 | { |
| 392 | lib = new wxLibrary(handle); |
| 393 | wxClassInfo::sm_first = old_sm_first; |
| 394 | m_loaded.Append(name.GetData(), lib); |
| 395 | } |
| 396 | else |
| 397 | lib = NULL; |
| 398 | return lib; |
| 399 | } |
| 400 | |
| 401 | wxObject *wxLibraries::CreateObject(const wxString& path) |
| 402 | { |
| 403 | wxNode *node = m_loaded.First(); |
| 404 | wxObject *obj; |
| 405 | |
| 406 | while (node) { |
| 407 | obj = ((wxLibrary *)node->Data())->CreateObject(path); |
| 408 | if (obj) |
| 409 | return obj; |
| 410 | |
| 411 | node = node->Next(); |
| 412 | } |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | #endif // wxUSE_DYNLIB_CLASS |