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 #if defined(__WINDOWS__)
33 #include "wx/msw/private.h"
36 #include "wx/dynlib.h"
37 #include "wx/filefn.h"
40 #include "wx/tokenzr.h"
42 // ----------------------------------------------------------------------------
43 // conditional compilation
44 // ----------------------------------------------------------------------------
46 #if defined(__WXPM__) || defined(__EMX__)
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 # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_NOW/*RTLD_LAZY*/)
54 # define wxDllGetSymbol(handle, name) dlsym(handle, name)
55 # define wxDllClose dlclose
56 #elif defined(HAVE_SHL_LOAD)
57 # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
58 # define wxDllClose shl_unload
60 static inline void *wxDllGetSymbol(shl_t handle
, const wxString
& name
)
63 if ( shl_findsym(&handle
, name
.mb_str(), TYPE_UNDEFINED
, &sym
) == 0 )
68 #elif defined(__WINDOWS__)
69 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
72 # define wxDllOpen(lib) ::LoadLibraryExW(lib, 0, 0)
74 # define wxDllOpen(lib) ::LoadLibraryExA(lib, 0, 0)
77 # define wxDllOpen(lib) ::LoadLibrary(lib)
79 # define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
80 # define wxDllClose ::FreeLibrary
82 # error "Don't know how to load shared libraries on this platform."
85 // ---------------------------------------------------------------------------
87 // ---------------------------------------------------------------------------
89 wxLibraries wxTheLibraries
;
91 // ============================================================================
93 // ============================================================================
95 // construct the full name from the base shared object name: adds a .dll
96 // suffix under Windows or .so under Unix
97 static wxString
ConstructLibraryName(const wxString
& basename
)
100 fullname
<< basename
<< wxDllLoader::GetDllExt();
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 return wxDllLoader::GetSymbol(m_handle
, symbname
);
169 // ---------------------------------------------------------------------------
171 // ---------------------------------------------------------------------------
174 wxString
wxDllLoader::GetDllExt()
178 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
180 #elif defined(__UNIX__)
181 # if defined(__HPUX__)
193 wxDllLoader::GetProgramHandle(void)
195 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
196 // optain handle for main program
197 return dlopen(NULL
, RTLD_NOW
/*RTLD_LAZY*/);
198 #elif defined (HAVE_SHL_LOAD)
199 // shl_findsymbol with NULL handle looks up in main program
202 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
209 wxDllLoader::LoadLibrary(const wxString
& libname
, bool *success
)
213 #if defined(__WXMAC__)
218 wxMacPathToFSSpec( libname
, &myFSSpec
) ;
219 if (GetDiskFragment( &myFSSpec
, 0 , kCFragGoesToEOF
, "\p" , kPrivateCFragCopy
, &handle
, &myMainAddr
,
220 myErrName
) != noErr
)
222 p2cstr( myErrName
) ;
223 wxASSERT_MSG( 1 , (char*)myErrName
) ;
226 #elif defined(__WXPM__) || defined(__EMX__)
227 char zError
[256] = "";
228 wxDllOpen(zError
, libname
, handle
);
230 handle
= wxDllOpen(libname
);
235 wxString
msg(_("Failed to load shared library '%s'"));
238 const char *errmsg
= dlerror();
241 // the error string format is "libname: ...", but we already have
242 // libname, so cut it off
243 const char *p
= strchr(errmsg
, ':');
255 wxLogError(msg
, libname
.c_str(), p
);
258 #endif // HAVE_DLERROR
260 wxLogSysError(msg
, libname
.c_str());
266 *success
= handle
!= 0;
275 wxDllLoader::UnloadLibrary(wxDllType handle
)
282 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
)
284 void *symbol
= NULL
; // return value
286 #if defined( __WXMAC__ )
288 CFragSymbolClass symClass
;
291 strcpy( (char*) symName
, name
) ;
292 c2pstr( (char*) symName
) ;
294 if ( FindSymbol( dllHandle
, symName
, &symAddress
, &symClass
) == noErr
)
295 symbol
= (void *)symAddress
;
296 #elif defined( __WXPM__ ) || defined(__EMX__)
297 wxDllGetSymbol(dllHandle
, symbol
);
299 // mb_str() is necessary in Unicode build
300 symbol
= wxDllGetSymbol(dllHandle
, name
.mb_str());
305 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
311 // ---------------------------------------------------------------------------
312 // wxLibraries (only one instance should normally exist)
313 // ---------------------------------------------------------------------------
315 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING
)
319 wxLibraries::~wxLibraries()
321 wxNode
*node
= m_loaded
.First();
324 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
331 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
335 wxClassInfo
*old_sm_first
;
337 #if defined(__VISAGECPP__)
338 node
= m_loaded
.Find(name
.GetData());
340 return ((wxLibrary
*)node
->Data());
342 if ( (node
= m_loaded
.Find(name
.GetData())) )
343 return ((wxLibrary
*)node
->Data());
345 // If DLL shares data, this is necessary.
346 old_sm_first
= wxClassInfo::sm_first
;
347 wxClassInfo::sm_first
= NULL
;
349 wxString libname
= ConstructLibraryName(name
);
352 Unix automatically builds that library name, at least for dlopen()
355 #if defined(__UNIX__)
356 // found the first file in LD_LIBRARY_PATH with this name
357 wxString
libPath("/lib:/usr/lib"); // system path first
358 const char *envLibPath
= getenv("LD_LIBRARY_PATH");
360 libPath
<< wxT(':') << envLibPath
;
361 wxStringTokenizer
tokenizer(libPath
, wxT(':'));
362 while ( tokenizer
.HasMoreToken() )
364 wxString
fullname(tokenizer
.NextToken());
366 fullname
<< wxT('/') << libname
;
367 if ( wxFileExists(fullname
) )
375 //else: not found in the path, leave the name as is (secutiry risk?)
380 bool success
= FALSE
;
381 wxDllType handle
= wxDllLoader::LoadLibrary(libname
, &success
);
384 lib
= new wxLibrary(handle
);
385 wxClassInfo::sm_first
= old_sm_first
;
386 m_loaded
.Append(name
.GetData(), lib
);
393 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
395 wxNode
*node
= m_loaded
.First();
399 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
408 #endif // wxUSE_DYNLIB_CLASS