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
48 #elif defined(HAVE_SHL_LOAD)
49 # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
50 # define wxDllClose shl_unload
52 static inline void *wxDllGetSymbol(shl_t handle
, const wxString
& name
)
55 if ( shl_findsym(&handle
, name
.mb_str(), TYPE_UNDEFINED
, &sym
) == 0 )
60 #elif defined(__WINDOWS__)
61 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
63 # define wxDllOpen(lib) ::LoadLibraryEx(lib, 0, 0)
65 # define wxDllOpen(lib) ::LoadLibrary(lib)
67 # define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
68 # define wxDllClose ::FreeLibrary
70 #elif defined(__OS2__)
74 # define wxDllOpen(error, lib, handle) DosLoadModule(error, sizeof(error), lib, &handle)
75 # define wxDllGetSymbol(handle, modaddr) DosQueryProcAddr(handle, 1L, NULL, (PFN*)modaddr)
76 # define wxDllClose(handle) DosFreeModule(handle)
78 # error "Don't know how to load shared libraries on this platform."
81 // ---------------------------------------------------------------------------
83 // ---------------------------------------------------------------------------
85 wxLibraries wxTheLibraries
;
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 // construct the full name from the base shared object name: adds a .dll
92 // suffix under Windows or .so under Unix
93 static wxString
ConstructLibraryName(const wxString
& basename
)
95 wxString
fullname(basename
);
98 # if defined(__HPUX__)
103 #elif defined(__WINDOWS__) || defined(__OS2__)
110 // ============================================================================
112 // ============================================================================
115 // ---------------------------------------------------------------------------
116 // wxLibrary (one instance per dynamic library)
117 // ---------------------------------------------------------------------------
119 wxLibrary::wxLibrary(wxDllType handle
)
121 typedef wxClassInfo
*(*t_get_first
)(void);
122 t_get_first get_first
;
126 // Some system may use a local heap for library.
127 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
128 // It is a wxWindows DLL.
130 PrepareClasses(get_first());
133 wxLibrary::~wxLibrary()
137 wxDllClose(m_handle
);
141 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
143 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
148 return info
->CreateObject();
151 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
153 // Index all class infos by their class name
154 wxClassInfo
*info
= first
;
157 if (info
->m_className
)
158 classTable
.Put(info
->m_className
, (wxObject
*)info
);
159 info
= info
->GetNext();
162 // Set base pointers for each wxClassInfo
166 if (info
->GetBaseClassName1())
167 info
->m_baseInfo1
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName1());
168 if (info
->GetBaseClassName2())
169 info
->m_baseInfo2
= (wxClassInfo
*)classTable
.Get(info
->GetBaseClassName2());
174 void *wxLibrary::GetSymbol(const wxString
& symbname
)
176 return wxDllLoader::GetSymbol(m_handle
, symbname
);
179 // ---------------------------------------------------------------------------
181 // ---------------------------------------------------------------------------
185 wxDllLoader::GetProgramHandle(void)
187 #if defined( HAVE_DLOPEN )
188 // optain handle for main program
189 return dlopen(NULL
, RTLD_NOW
/*RTLD_LAZY*/);
190 #elif defined (HAVE_SHL_LOAD)
191 // shl_findsymbol with NULL handle looks up in main program
194 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
201 wxDllLoader::LoadLibrary(const wxString
& libname
, bool *success
)
205 #if defined(__WXMAC__)
210 wxMacPathToFSSpec( libname
, &myFSSpec
) ;
211 if (GetDiskFragment( &myFSSpec
, 0 , kCFragGoesToEOF
, "\p" , kPrivateCFragCopy
, &handle
, &myMainAddr
,
212 myErrName
) != noErr
)
214 p2cstr( myErrName
) ;
215 wxASSERT_MSG( 1 , (char*)myErrName
) ;
218 #elif defined(__OS2__)
219 char zError
[256] = "";
220 wxDllOpen(zError
, libname
, handle
);
222 handle
= wxDllOpen(libname
);
227 wxLogSysError(_("Failed to load shared library '%s'"), libname
.c_str());
232 *success
= handle
!= 0;
241 wxDllLoader::UnloadLibrary(wxDllType handle
)
248 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
)
250 void *symbol
= NULL
; // return value
252 #if defined( __WXMAC__ )
254 CFragSymbolClass symClass
;
257 strcpy( (char*) symName
, name
) ;
258 c2pstr( (char*) symName
) ;
260 if ( FindSymbol( dllHandle
, symName
, &symAddress
, &symClass
) == noErr
)
261 symbol
= (void *)symAddress
;
262 #elif defined( __OS2__ )
263 wxDllGetSymbol(dllHandle
, symbol
);
265 symbol
= wxDllGetSymbol(dllHandle
, name
);
270 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
276 // ---------------------------------------------------------------------------
277 // wxLibraries (only one instance should normally exist)
278 // ---------------------------------------------------------------------------
280 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING
)
284 wxLibraries::~wxLibraries()
286 wxNode
*node
= m_loaded
.First();
289 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
296 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
300 wxClassInfo
*old_sm_first
;
302 #if defined(__VISAGECPP__)
303 node
= m_loaded
.Find(name
.GetData());
305 return ((wxLibrary
*)node
->Data());
307 if ( (node
= m_loaded
.Find(name
.GetData())) )
308 return ((wxLibrary
*)node
->Data());
310 // If DLL shares data, this is necessary.
311 old_sm_first
= wxClassInfo::sm_first
;
312 wxClassInfo::sm_first
= NULL
;
314 wxString libname
= ConstructLibraryName(name
);
317 Unix automatically builds that library name, at least for dlopen()
320 #if defined(__UNIX__)
321 // found the first file in LD_LIBRARY_PATH with this name
322 wxString
libPath("/lib:/usr/lib"); // system path first
323 const char *envLibPath
= getenv("LD_LIBRARY_PATH");
325 libPath
<< wxT(':') << envLibPath
;
326 wxStringTokenizer
tokenizer(libPath
, wxT(':'));
327 while ( tokenizer
.HasMoreToken() )
329 wxString
fullname(tokenizer
.NextToken());
331 fullname
<< wxT('/') << libname
;
332 if ( wxFileExists(fullname
) )
340 //else: not found in the path, leave the name as is (secutiry risk?)
345 bool success
= FALSE
;
346 wxDllType handle
= wxDllLoader::LoadLibrary(libname
, &success
);
349 lib
= new wxLibrary(handle
);
350 wxClassInfo::sm_first
= old_sm_first
;
351 m_loaded
.Append(name
.GetData(), lib
);
358 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
360 wxNode
*node
= m_loaded
.First();
364 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
373 #endif // wxUSE_DYNLIB_CLASS