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(__WXPM__) || defined(__EMX__)
47 # define wxDllOpen(error, lib, handle) DosLoadModule(error, sizeof(error), lib, &handle)
48 # define wxDllGetSymbol(handle, modaddr) DosQueryProcAddr(handle, 1L, NULL, (PFN*)modaddr)
49 # define wxDllClose(handle) DosFreeModule(handle)
50 #elif defined(HAVE_DLOPEN)
51 # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_NOW/*RTLD_LAZY*/)
52 # define wxDllGetSymbol(handle, name) dlsym(handle, name.mb_str())
53 # define wxDllClose dlclose
54 #elif defined(HAVE_SHL_LOAD)
55 # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
56 # define wxDllClose shl_unload
58 static inline void *wxDllGetSymbol(shl_t handle
, const wxString
& name
)
61 if ( shl_findsym(&handle
, name
.mb_str(), TYPE_UNDEFINED
, &sym
) == 0 )
66 #elif defined(__WINDOWS__)
67 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
69 # define wxDllOpen(lib) ::LoadLibraryEx(lib, 0, 0)
71 # define wxDllOpen(lib) ::LoadLibrary(lib)
73 # define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
74 # define wxDllClose ::FreeLibrary
76 # error "Don't know how to load shared libraries on this platform."
79 // ---------------------------------------------------------------------------
81 // ---------------------------------------------------------------------------
83 wxLibraries wxTheLibraries
;
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 // construct the full name from the base shared object name: adds a .dll
90 // suffix under Windows or .so under Unix
91 static wxString
ConstructLibraryName(const wxString
& basename
)
93 wxString
fullname(basename
);
95 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
97 #elif defined(__UNIX__)
98 # if defined(__HPUX__)
108 // ============================================================================
110 // ============================================================================
113 // ---------------------------------------------------------------------------
114 // wxLibrary (one instance per dynamic library)
115 // ---------------------------------------------------------------------------
117 wxLibrary::wxLibrary(wxDllType handle
)
119 typedef wxClassInfo
*(*t_get_first
)(void);
120 t_get_first get_first
;
124 // Some system may use a local heap for library.
125 get_first
= (t_get_first
)GetSymbol("wxGetClassFirst");
126 // It is a wxWindows DLL.
128 PrepareClasses(get_first());
131 wxLibrary::~wxLibrary()
135 wxDllClose(m_handle
);
139 wxObject
*wxLibrary::CreateObject(const wxString
& name
)
141 wxClassInfo
*info
= (wxClassInfo
*)classTable
.Get(name
);
146 return info
->CreateObject();
149 void wxLibrary::PrepareClasses(wxClassInfo
*first
)
151 // Index all class infos by their class name
152 wxClassInfo
*info
= first
;
155 if (info
->m_className
)
156 classTable
.Put(info
->m_className
, (wxObject
*)info
);
157 info
= info
->GetNext();
160 // Set base pointers for each wxClassInfo
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());
172 void *wxLibrary::GetSymbol(const wxString
& symbname
)
174 return wxDllLoader::GetSymbol(m_handle
, symbname
);
177 // ---------------------------------------------------------------------------
179 // ---------------------------------------------------------------------------
183 wxDllLoader::GetProgramHandle(void)
185 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
186 // optain handle for main program
187 return dlopen(NULL
, RTLD_NOW
/*RTLD_LAZY*/);
188 #elif defined (HAVE_SHL_LOAD)
189 // shl_findsymbol with NULL handle looks up in main program
192 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
199 wxDllLoader::LoadLibrary(const wxString
& libname
, bool *success
)
203 #if defined(__WXMAC__)
208 wxMacPathToFSSpec( libname
, &myFSSpec
) ;
209 if (GetDiskFragment( &myFSSpec
, 0 , kCFragGoesToEOF
, "\p" , kPrivateCFragCopy
, &handle
, &myMainAddr
,
210 myErrName
) != noErr
)
212 p2cstr( myErrName
) ;
213 wxASSERT_MSG( 1 , (char*)myErrName
) ;
216 #elif defined(__WXPM__) || defined(__EMX__)
217 char zError
[256] = "";
218 wxDllOpen(zError
, libname
, handle
);
220 handle
= wxDllOpen(libname
);
225 wxLogSysError(_("Failed to load shared library '%s'"), libname
.c_str());
230 *success
= handle
!= 0;
239 wxDllLoader::UnloadLibrary(wxDllType handle
)
246 wxDllLoader::GetSymbol(wxDllType dllHandle
, const wxString
&name
)
248 void *symbol
= NULL
; // return value
250 #if defined( __WXMAC__ )
252 CFragSymbolClass symClass
;
255 strcpy( (char*) symName
, name
) ;
256 c2pstr( (char*) symName
) ;
258 if ( FindSymbol( dllHandle
, symName
, &symAddress
, &symClass
) == noErr
)
259 symbol
= (void *)symAddress
;
260 #elif defined( __WXPM__ ) || defined(__EMX__)
261 wxDllGetSymbol(dllHandle
, symbol
);
263 symbol
= wxDllGetSymbol(dllHandle
, name
);
268 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
274 // ---------------------------------------------------------------------------
275 // wxLibraries (only one instance should normally exist)
276 // ---------------------------------------------------------------------------
278 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING
)
282 wxLibraries::~wxLibraries()
284 wxNode
*node
= m_loaded
.First();
287 wxLibrary
*lib
= (wxLibrary
*)node
->Data();
294 wxLibrary
*wxLibraries::LoadLibrary(const wxString
& name
)
298 wxClassInfo
*old_sm_first
;
300 #if defined(__VISAGECPP__)
301 node
= m_loaded
.Find(name
.GetData());
303 return ((wxLibrary
*)node
->Data());
305 if ( (node
= m_loaded
.Find(name
.GetData())) )
306 return ((wxLibrary
*)node
->Data());
308 // If DLL shares data, this is necessary.
309 old_sm_first
= wxClassInfo::sm_first
;
310 wxClassInfo::sm_first
= NULL
;
312 wxString libname
= ConstructLibraryName(name
);
315 Unix automatically builds that library name, at least for dlopen()
318 #if defined(__UNIX__)
319 // found the first file in LD_LIBRARY_PATH with this name
320 wxString
libPath("/lib:/usr/lib"); // system path first
321 const char *envLibPath
= getenv("LD_LIBRARY_PATH");
323 libPath
<< wxT(':') << envLibPath
;
324 wxStringTokenizer
tokenizer(libPath
, wxT(':'));
325 while ( tokenizer
.HasMoreToken() )
327 wxString
fullname(tokenizer
.NextToken());
329 fullname
<< wxT('/') << libname
;
330 if ( wxFileExists(fullname
) )
338 //else: not found in the path, leave the name as is (secutiry risk?)
343 bool success
= FALSE
;
344 wxDllType handle
= wxDllLoader::LoadLibrary(libname
, &success
);
347 lib
= new wxLibrary(handle
);
348 wxClassInfo::sm_first
= old_sm_first
;
349 m_loaded
.Append(name
.GetData(), lib
);
356 wxObject
*wxLibraries::CreateObject(const wxString
& path
)
358 wxNode
*node
= m_loaded
.First();
362 obj
= ((wxLibrary
*)node
->Data())->CreateObject(path
);
371 #endif // wxUSE_DYNLIB_CLASS