]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
Improved selection mode handling in wxGrid::SelectBlock
[wxWidgets.git] / src / common / dynlib.cpp
CommitLineData
7a4b9130
GL
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
7b0bfbb2
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
7a4b9130 20#ifdef __GNUG__
a585ca5c 21# pragma implementation "dynlib.h"
7a4b9130
GL
22#endif
23
0c32066b 24#include "wx/wxprec.h"
ff0ea71c
GT
25#if defined(__WINDOWS__)
26#include "wx/msw/private.h"
27#endif
0c32066b 28
2df7be7f
RR
29#ifdef __BORLANDC__
30 #pragma hdrstop
31#endif
8a0d4cf6
VZ
32#if wxUSE_DYNLIB_CLASS
33
7b0bfbb2
VZ
34#include "wx/dynlib.h"
35#include "wx/filefn.h"
36#include "wx/intl.h"
37#include "wx/log.h"
8a0d4cf6 38#include "wx/tokenzr.h"
7b0bfbb2
VZ
39
40// ----------------------------------------------------------------------------
41// conditional compilation
42// ----------------------------------------------------------------------------
43
1a787c5d
SN
44#if defined(__WXPM__) || defined(__EMX__)
45# define INCL_DOS
46# include <os2.h>
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)
0868079c 51# define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_NOW/*RTLD_LAZY*/)
a585ca5c
KB
52# define wxDllGetSymbol(handle, name) dlsym(handle, name.mb_str())
53# define wxDllClose dlclose
8a0d4cf6 54#elif defined(HAVE_SHL_LOAD)
a585ca5c
KB
55# define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
56# define wxDllClose shl_unload
846e1424 57
50920146 58 static inline void *wxDllGetSymbol(shl_t handle, const wxString& name)
7b0bfbb2
VZ
59 {
60 void *sym;
50920146 61 if ( shl_findsym(&handle, name.mb_str(), TYPE_UNDEFINED, &sym) == 0 )
7b0bfbb2
VZ
62 return sym;
63 else
64 return (void *)0;
65 }
66#elif defined(__WINDOWS__)
90022ddc 67 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
a585ca5c
KB
68# ifdef __WIN32__
69# define wxDllOpen(lib) ::LoadLibraryEx(lib, 0, 0)
70# else // Win16
71# define wxDllOpen(lib) ::LoadLibrary(lib)
72# endif // Win32/16
73# define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
74# define wxDllClose ::FreeLibrary
7b0bfbb2 75#else
a585ca5c 76# error "Don't know how to load shared libraries on this platform."
7b0bfbb2 77#endif // OS
7a4b9130
GL
78
79// ---------------------------------------------------------------------------
7b0bfbb2 80// Global variables
7a4b9130
GL
81// ---------------------------------------------------------------------------
82
7b0bfbb2 83wxLibraries wxTheLibraries;
7a4b9130 84
7b0bfbb2
VZ
85// ----------------------------------------------------------------------------
86// private functions
87// ----------------------------------------------------------------------------
123a7fdd 88
7b0bfbb2
VZ
89// construct the full name from the base shared object name: adds a .dll
90// suffix under Windows or .so under Unix
91static wxString ConstructLibraryName(const wxString& basename)
92{
93 wxString fullname(basename);
94
1a787c5d
SN
95#if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
96 fullname << ".dll";
97#elif defined(__UNIX__)
a585ca5c 98# if defined(__HPUX__)
8a0d4cf6 99 fullname << ".sl";
a585ca5c 100# else //__HPUX__
8a0d4cf6 101 fullname << ".so";
a585ca5c 102# endif //__HPUX__
27529614
JS
103#endif
104
7b0bfbb2
VZ
105 return fullname;
106}
7a4b9130 107
7b0bfbb2
VZ
108// ============================================================================
109// implementation
110// ============================================================================
7a4b9130 111
a585ca5c 112
7a4b9130 113// ---------------------------------------------------------------------------
7b0bfbb2 114// wxLibrary (one instance per dynamic library)
7a4b9130
GL
115// ---------------------------------------------------------------------------
116
7b0bfbb2 117wxLibrary::wxLibrary(wxDllType handle)
7a4b9130 118{
7b0bfbb2
VZ
119 typedef wxClassInfo *(*t_get_first)(void);
120 t_get_first get_first;
7a4b9130 121
7b0bfbb2 122 m_handle = handle;
7a4b9130 123
7b0bfbb2
VZ
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());
7a4b9130
GL
129}
130
131wxLibrary::~wxLibrary()
132{
7b0bfbb2
VZ
133 if ( m_handle )
134 {
135 wxDllClose(m_handle);
136 }
7a4b9130
GL
137}
138
139wxObject *wxLibrary::CreateObject(const wxString& name)
140{
7b0bfbb2 141 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
f4a8c29f 142
7b0bfbb2
VZ
143 if (!info)
144 return NULL;
f4a8c29f 145
7b0bfbb2 146 return info->CreateObject();
f4a8c29f
GL
147}
148
856d2e52 149void wxLibrary::PrepareClasses(wxClassInfo *first)
f4a8c29f 150{
7b0bfbb2
VZ
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 }
7a4b9130
GL
170}
171
172void *wxLibrary::GetSymbol(const wxString& symbname)
173{
a585ca5c
KB
174 return wxDllLoader::GetSymbol(m_handle, symbname);
175}
7b0bfbb2 176
a585ca5c
KB
177// ---------------------------------------------------------------------------
178// wxDllLoader
179// ---------------------------------------------------------------------------
7b0bfbb2 180
0868079c
KB
181/* static */
182wxDllType
183wxDllLoader::GetProgramHandle(void)
184{
1a787c5d 185#if defined( HAVE_DLOPEN ) && !defined(__EMX__)
7742efff
KB
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
190 return 0;
0868079c 191#else
58c837a4 192 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
7742efff 193 return 0;
7cc98b3e 194#endif
0868079c
KB
195}
196
a585ca5c
KB
197/* static */
198wxDllType
7cc98b3e 199wxDllLoader::LoadLibrary(const wxString & libname, bool *success)
a585ca5c 200{
7cc98b3e 201 wxDllType handle;
7b0bfbb2 202
a585ca5c 203#if defined(__WXMAC__)
7cc98b3e
VZ
204 FSSpec myFSSpec ;
205 Ptr myMainAddr ;
206 Str255 myErrName ;
207
208 wxMacPathToFSSpec( libname , &myFSSpec ) ;
209 if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr ,
210 myErrName ) != noErr )
211 {
212 p2cstr( myErrName ) ;
213 wxASSERT_MSG( 1 , (char*)myErrName ) ;
214 return NULL ;
215 }
1a787c5d 216#elif defined(__WXPM__) || defined(__EMX__)
c2ff79b1
DW
217 char zError[256] = "";
218 wxDllOpen(zError, libname, handle);
a585ca5c 219#else // !Mac
7cc98b3e 220 handle = wxDllOpen(libname);
a585ca5c
KB
221#endif // OS
222
7cc98b3e
VZ
223 if ( !handle )
224 {
225 wxLogSysError(_("Failed to load shared library '%s'"), libname.c_str());
226 }
227
228 if ( success )
229 {
230 *success = handle != 0;
231 }
232
233 return handle;
a585ca5c
KB
234}
235
752c7d6b
KB
236
237/* static */
238void
239wxDllLoader::UnloadLibrary(wxDllType handle)
240{
241 wxDllClose(handle);
242}
243
a585ca5c
KB
244/* static */
245void *
246wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name)
247{
7cc98b3e 248 void *symbol = NULL; // return value
a585ca5c
KB
249
250#if defined( __WXMAC__ )
7cc98b3e
VZ
251 Ptr symAddress ;
252 CFragSymbolClass symClass ;
253 Str255 symName ;
254
255 strcpy( (char*) symName , name ) ;
256 c2pstr( (char*) symName ) ;
257
258 if ( FindSymbol( dllHandle , symName , &symAddress , &symClass ) == noErr )
259 symbol = (void *)symAddress ;
1a787c5d 260#elif defined( __WXPM__ ) || defined(__EMX__)
c2ff79b1 261 wxDllGetSymbol(dllHandle, symbol);
7b0bfbb2 262#else
a585ca5c 263 symbol = wxDllGetSymbol(dllHandle, name);
123a7fdd 264#endif
7b0bfbb2
VZ
265
266 if ( !symbol )
267 {
7cc98b3e
VZ
268 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
269 name.c_str());
7b0bfbb2 270 }
7b0bfbb2 271 return symbol;
7a4b9130
GL
272}
273
274// ---------------------------------------------------------------------------
275// wxLibraries (only one instance should normally exist)
276// ---------------------------------------------------------------------------
277
65fd5cb0 278wxLibraries::wxLibraries():m_loaded(wxKEY_STRING)
7a4b9130
GL
279{
280}
281
282wxLibraries::~wxLibraries()
283{
7b0bfbb2 284 wxNode *node = m_loaded.First();
7a4b9130 285
7b0bfbb2
VZ
286 while (node) {
287 wxLibrary *lib = (wxLibrary *)node->Data();
288 delete lib;
7a4b9130 289
7b0bfbb2
VZ
290 node = node->Next();
291 }
7a4b9130
GL
292}
293
294wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
295{
7b0bfbb2
VZ
296 wxNode *node;
297 wxLibrary *lib;
298 wxClassInfo *old_sm_first;
7a4b9130 299
c2ff79b1
DW
300#if defined(__VISAGECPP__)
301 node = m_loaded.Find(name.GetData());
302 if (node != NULL)
303 return ((wxLibrary *)node->Data());
304#else // !OS/2
7b0bfbb2
VZ
305 if ( (node = m_loaded.Find(name.GetData())) )
306 return ((wxLibrary *)node->Data());
c2ff79b1 307#endif
7b0bfbb2
VZ
308 // If DLL shares data, this is necessary.
309 old_sm_first = wxClassInfo::sm_first;
310 wxClassInfo::sm_first = NULL;
311
7cc98b3e 312 wxString libname = ConstructLibraryName(name);
856d2e52 313
a585ca5c
KB
314/*
315 Unix automatically builds that library name, at least for dlopen()
316*/
317#if 0
9e4b2f1c 318#if defined(__UNIX__)
8a0d4cf6
VZ
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");
322 if ( envLibPath )
58c837a4 323 libPath << wxT(':') << envLibPath;
223d09f6 324 wxStringTokenizer tokenizer(libPath, wxT(':'));
8a0d4cf6
VZ
325 while ( tokenizer.HasMoreToken() )
326 {
327 wxString fullname(tokenizer.NextToken());
328
58c837a4 329 fullname << wxT('/') << libname;
8a0d4cf6
VZ
330 if ( wxFileExists(fullname) )
331 {
7cc98b3e 332 libname = fullname;
8a0d4cf6
VZ
333
334 // found the library
335 break;
336 }
337 }
338 //else: not found in the path, leave the name as is (secutiry risk?)
339
7b0bfbb2 340#endif // __UNIX__
a585ca5c 341#endif
7a4b9130 342
4dc2c3bb 343 bool success = FALSE;
7cc98b3e 344 wxDllType handle = wxDllLoader::LoadLibrary(libname, &success);
a585ca5c 345 if(success)
7b0bfbb2 346 {
a585ca5c
KB
347 lib = new wxLibrary(handle);
348 wxClassInfo::sm_first = old_sm_first;
349 m_loaded.Append(name.GetData(), lib);
7b0bfbb2 350 }
a585ca5c
KB
351 else
352 lib = NULL;
7b0bfbb2 353 return lib;
7a4b9130
GL
354}
355
356wxObject *wxLibraries::CreateObject(const wxString& path)
357{
7b0bfbb2
VZ
358 wxNode *node = m_loaded.First();
359 wxObject *obj;
7a4b9130 360
7b0bfbb2
VZ
361 while (node) {
362 obj = ((wxLibrary *)node->Data())->CreateObject(path);
363 if (obj)
364 return obj;
7a4b9130 365
7b0bfbb2
VZ
366 node = node->Next();
367 }
368 return NULL;
7a4b9130 369}
8a0d4cf6
VZ
370
371#endif // wxUSE_DYNLIB_CLASS