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"
25 #if defined(__WINDOWS__)
26 #include "wx/msw/private.h"
32 #if wxUSE_DYNLIB_CLASS
34 #include "wx/dynlib.h"
35 #include "wx/filefn.h"
38 #include "wx/tokenzr.h"
40 // ----------------------------------------------------------------------------
41 // conditional compilation
42 // ----------------------------------------------------------------------------
44 #if defined(HAVE_DLOPEN)
45 # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_NOW/*RTLD_LAZY*/)
46 # define wxDllGetSymbol(handle, name) dlsym(handle, name.mb_str())
47 # define wxDllClose dlclose
49 #elif defined(HAVE_SHL_LOAD)
50 # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
51 # define wxDllClose shl_unload
53 static inline void *wxDllGetSymbol(shl_t handle
, const wxString
& name
)
56 if ( shl_findsym(&handle
, name
.mb_str(), TYPE_UNDEFINED
, &sym
) == 0 )
61 #elif defined(__WINDOWS__)
62 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
64 # define wxDllOpen(lib) ::LoadLibraryEx(lib, 0, 0)
66 # define wxDllOpen(lib) ::LoadLibrary(lib)
68 # define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
69 # define wxDllClose ::FreeLibrary
71 #elif defined(__OS2__)
75 # define wxDllOpen(error, lib, handle) DosLoadModule(error, sizeof(error), lib, &handle)
76 # define wxDllGetSymbol(handle, modaddr) DosQueryProcAddr(handle, 1L, NULL, (PFN*)modaddr)
77 # define wxDllClose(handle) DosFreeModule(handle)
79 # error "Don't know how to load shared libraries on this platform."
82 // ---------------------------------------------------------------------------
84 // ---------------------------------------------------------------------------
86 wxLibraries wxTheLibraries
;
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 // construct the full name from the base shared object name: adds a .dll
93 // suffix under Windows or .so under Unix
94 static wxString
ConstructLibraryName(const wxString
& basename
)
96 wxString
fullname(basename
);
99 # if defined(__HPUX__)
104 #elif defined(__WINDOWS__) || defined(__OS2__)
111 // ============================================================================
113 // ============================================================================
116 // ---------------------------------------------------------------------------
117 // wxLibrary (one instance per dynamic library)
118 // ---------------------------------------------------------------------------
120 wxLibrary::wxLibrary(wxDllType handle
)
122 typedef wxClassInfo
*(*t_get_first
)(void);
123 t_get_first get_first
;
127 // Some system may use a local heap for library.
128 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
129 // It is a wxWindows DLL.
131 PrepareClasses(get_first());
134 wxLibrary::~wxLibrary()
138 wxDllClose(m_handle
);
142 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
144 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
149 return info
->CreateObject();
152 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
154 // Index all class infos by their class name
155 wxClassInfo
*info
= first
;
158 if (info
->m_className
)
159 classTable
.Put(info
->m_className
, (wxObject
*)info
);
160 info
= info
->GetNext();
163 // Set base pointers for each wxClassInfo
167 if (info
->GetBaseClassName1())
168 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
169 if (info
->GetBaseClassName2())
170 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
175 void *wxLibrary::GetSymbol(const wxString
& symbname
)
177 return wxDllLoader::GetSymbol(m_handle
, symbname
);
180 // ---------------------------------------------------------------------------
182 // ---------------------------------------------------------------------------
186 wxDllLoader::GetProgramHandle(void)
188 #if defined( HAVE_DLOPEN )
189 // optain handle for main program
190 return dlopen(NULL
, RTLD_NOW
/*RTLD_LAZY*/);
191 #elif defined (HAVE_SHL_LOAD)
192 // shl_findsymbol with NULL handle looks up in main program
195 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
202 wxDllLoader::LoadLibrary(const wxString
& libname
, bool *success
)
206 #if defined(__WXMAC__)
211 wxMacPathToFSSpec( libname
, &myFSSpec
) ;
212 if (GetDiskFragment( &myFSSpec
, 0 , kCFragGoesToEOF
, "\p" , kPrivateCFragCopy
, &handle
, &myMainAddr
,
213 myErrName
) != noErr
)
215 p2cstr( myErrName
) ;
216 wxASSERT_MSG( 1 , (char*)myErrName
) ;
219 #elif defined(__OS2__)
220 char zError
[256] = "";
221 wxDllOpen(zError
, libname
, handle
);
223 handle
= wxDllOpen(libname
);
228 wxLogSysError(_("Failed to load shared library '%s'"), libname
.c_str());
233 *success
= handle
!= 0;
242 wxDllLoader::UnloadLibrary(wxDllType handle
)
249 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
)
251 void *symbol
= NULL
; // return value
253 #if defined( __WXMAC__ )
255 CFragSymbolClass symClass
;
258 strcpy( (char*) symName
, name
) ;
259 c2pstr( (char*) symName
) ;
261 if ( FindSymbol( dllHandle
, symName
, &symAddress
, &symClass
) == noErr
)
262 symbol
= (void *)symAddress
;
263 #elif defined( __OS2__ )
264 wxDllGetSymbol(dllHandle
, symbol
);
266 symbol
= wxDllGetSymbol(dllHandle
, name
);
271 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
277 // ---------------------------------------------------------------------------
278 // wxLibraries (only one instance should normally exist)
279 // ---------------------------------------------------------------------------
281 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING
)
285 wxLibraries::~wxLibraries()
287 wxNode
*node
= m_loaded
.First();
290 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
297 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
301 wxClassInfo
*old_sm_first
;
303 #if defined(__VISAGECPP__)
304 node
= m_loaded
.Find(name
.GetData());
306 return ((wxLibrary
*)node
->Data());
308 if ( (node
= m_loaded
.Find(name
.GetData())) )
309 return ((wxLibrary
*)node
->Data());
311 // If DLL shares data, this is necessary.
312 old_sm_first
= wxClassInfo::sm_first
;
313 wxClassInfo::sm_first
= NULL
;
315 wxString libname
= ConstructLibraryName(name
);
318 Unix automatically builds that library name, at least for dlopen()
321 #if defined(__UNIX__)
322 // found the first file in LD_LIBRARY_PATH with this name
323 wxString
libPath("/lib:/usr/lib"); // system path first
324 const char *envLibPath
= getenv("LD_LIBRARY_PATH");
326 libPath
<< wxT(':') << envLibPath
;
327 wxStringTokenizer
tokenizer(libPath
, wxT(':'));
328 while ( tokenizer
.HasMoreToken() )
330 wxString
fullname(tokenizer
.NextToken());
332 fullname
<< wxT('/') << libname
;
333 if ( wxFileExists(fullname
) )
341 //else: not found in the path, leave the name as is (secutiry risk?)
346 bool success
= FALSE
;
347 wxDllType handle
= wxDllLoader::LoadLibrary(libname
, &success
);
350 lib
= new wxLibrary(handle
);
351 wxClassInfo::sm_first
= old_sm_first
;
352 m_loaded
.Append(name
.GetData(), lib
);
359 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
361 wxNode
*node
= m_loaded
.First();
365 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
374 #endif // wxUSE_DYNLIB_CLASS