]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
moved DB classes into wxODBC library
[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
55d99c7a 9// Licence: wxWindows licence
7a4b9130
GL
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
JS
24#include "wx/wxprec.h"
25
2df7be7f
RR
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
f6bcfd97 29
1948bb32 30#if wxUSE_DYNLIB_CLASS
8a0d4cf6 31
f6bcfd97
BP
32#if defined(__WINDOWS__)
33 #include "wx/msw/private.h"
34#endif
35
7b0bfbb2
VZ
36#include "wx/dynlib.h"
37#include "wx/filefn.h"
38#include "wx/intl.h"
39#include "wx/log.h"
1948bb32
VS
40#include "wx/utils.h"
41#include "wx/filename.h" // for SplitPath()
a8eaaeb2
VS
42#include "wx/app.h"
43#include "wx/apptrait.h"
7b0bfbb2 44
76a5e5d2
SC
45#if defined(__WXMAC__)
46 #include "wx/mac/private.h"
47#endif
48
7b0bfbb2 49
1948bb32
VS
50// ============================================================================
51// implementation
52// ============================================================================
846e1424 53
1948bb32
VS
54#if defined(__DARWIN__)
55// ---------------------------------------------------------------------------
56// For Darwin/Mac OS X
57// supply the sun style dlopen functions in terms of Darwin NS*
58// ---------------------------------------------------------------------------
0b9ab0bd 59
f11bdd03
GD
60/* Porting notes:
61 * The dlopen port is a port from dl_next.xs by Anno Siegel.
62 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
63 * The method used here is just to supply the sun style dlopen etc.
64 * functions in terms of Darwin NS*.
65 */
7a4b9130 66
1948bb32
VS
67#include <stdio.h>
68#include <mach-o/dyld.h>
7a4b9130 69
1948bb32 70static char dl_last_error[1024];
123a7fdd 71
1948bb32
VS
72static
73void TranslateError(const char *path, int number)
7b0bfbb2 74{
1948bb32
VS
75 unsigned int index;
76 static char *OFIErrorStrings[] =
77 {
78 "%s(%d): Object Image Load Failure\n",
79 "%s(%d): Object Image Load Success\n",
80 "%s(%d): Not an recognisable object file\n",
81 "%s(%d): No valid architecture\n",
82 "%s(%d): Object image has an invalid format\n",
83 "%s(%d): Invalid access (permissions?)\n",
84 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
85 };
86#define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
27529614 87
1948bb32
VS
88 index = number;
89 if (index > NUM_OFI_ERRORS - 1) {
90 index = NUM_OFI_ERRORS - 1;
91 }
92 sprintf(dl_last_error, OFIErrorStrings[index], path, number);
7b0bfbb2 93}
7a4b9130 94
1948bb32 95const char *dlerror()
7a4b9130 96{
1948bb32 97 return dl_last_error;
7a4b9130
GL
98}
99
1948bb32 100void *dlopen(const char *path, int WXUNUSED(mode) /* mode is ignored */)
7a4b9130 101{
1948bb32
VS
102 int dyld_result;
103 NSObjectFileImage ofile;
104 NSModule handle = NULL;
105
106 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
107 if (dyld_result != NSObjectFileImageSuccess)
7b0bfbb2 108 {
1948bb32 109 TranslateError(path, dyld_result);
7b0bfbb2 110 }
1948bb32
VS
111 else
112 {
113 // NSLinkModule will cause the run to abort on any link error's
114 // not very friendly but the error recovery functionality is limited.
115 handle = NSLinkModule(ofile, path, NSLINKMODULE_OPTION_BINDNOW);
116 }
117
118 return handle;
7a4b9130
GL
119}
120
1948bb32 121int dlclose(void *handle)
7a4b9130 122{
1948bb32
VS
123 NSUnLinkModule( handle, NSUNLINKMODULE_OPTION_NONE);
124 return 0;
f4a8c29f
GL
125}
126
1948bb32 127void *dlsym(void *handle, const char *symbol)
f4a8c29f 128{
1948bb32
VS
129 void *addr;
130
131 NSSymbol nsSymbol = NSLookupSymbolInModule( handle , symbol ) ;
132
133 if ( nsSymbol)
7b0bfbb2 134 {
1948bb32 135 addr = NSAddressOfSymbol(nsSymbol);
7b0bfbb2 136 }
1948bb32 137 else
7b0bfbb2 138 {
1948bb32 139 addr = NULL;
7b0bfbb2 140 }
1948bb32 141 return addr;
7a4b9130
GL
142}
143
1948bb32
VS
144#endif // defined(__DARWIN__)
145
7b0bfbb2 146
a585ca5c 147// ---------------------------------------------------------------------------
1948bb32 148// wxDynamicLibrary
a585ca5c 149// ---------------------------------------------------------------------------
7b0bfbb2 150
1948bb32
VS
151//FIXME: This class isn't really common at all, it should be moved into
152// platform dependent files.
f6bcfd97
BP
153
154#if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
1948bb32 155 const wxChar *wxDynamicLibrary::ms_dllext = _T(".dll");
f6bcfd97 156#elif defined(__UNIX__)
1948bb32
VS
157 #if defined(__HPUX__)
158 const wxChar *wxDynamicLibrary::ms_dllext = _T(".sl");
159 #else
160 const wxChar *wxDynamicLibrary::ms_dllext = _T(".so");
161 #endif
f6bcfd97 162#endif
f6bcfd97 163
1948bb32 164wxDllType wxDynamicLibrary::GetProgramHandle()
0868079c 165{
1a787c5d 166#if defined( HAVE_DLOPEN ) && !defined(__EMX__)
1948bb32 167 return dlopen(0, RTLD_LAZY);
7742efff 168#elif defined (HAVE_SHL_LOAD)
1948bb32 169 return PROG_HANDLE;
0868079c 170#else
58c837a4 171 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
7742efff 172 return 0;
7cc98b3e 173#endif
0868079c
KB
174}
175
1948bb32 176bool wxDynamicLibrary::Load(wxString libname, int flags)
a585ca5c 177{
1948bb32
VS
178 wxASSERT_MSG(m_handle == 0, _T("Library already loaded."));
179
180 // add the proper extension for the DLL ourselves unless told not to
181 if ( !(flags & wxDL_VERBATIM) )
182 {
183 // and also check that the libname doesn't already have it
184 wxString ext;
185 wxFileName::SplitPath(libname, NULL, NULL, &ext);
186 if ( ext.empty() )
187 {
188 libname += GetDllExt();
189 }
190 }
7b0bfbb2 191
1948bb32
VS
192 // different ways to load a shared library
193 //
194 // FIXME: should go to the platform-specific files!
195#if defined(__WXMAC__) && !defined(__DARWIN__)
0b9ab0bd
RL
196 FSSpec myFSSpec;
197 Ptr myMainAddr;
198 Str255 myErrName;
199
200 wxMacFilename2FSSpec( libname , &myFSSpec );
201
202 if( GetDiskFragment( &myFSSpec,
203 0,
204 kCFragGoesToEOF,
205 "\p",
206 kPrivateCFragCopy,
1948bb32 207 &m_handle,
0b9ab0bd
RL
208 &myMainAddr,
209 myErrName ) != noErr )
7cc98b3e 210 {
0b9ab0bd
RL
211 p2cstr( myErrName );
212 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
213 libname.c_str(),
214 (char*)myErrName );
1948bb32 215 m_handle = 0;
7cc98b3e 216 }
0b9ab0bd 217
1a787c5d 218#elif defined(__WXPM__) || defined(__EMX__)
1948bb32
VS
219 char err[256] = "";
220 DosLoadModule(err, sizeof(err), libname.c_str(), &m_handle);
221
222#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
0b9ab0bd 223
1948bb32
VS
224#if defined(__VMS) || defined(__DARWIN__)
225 m_handle = dlopen(libname.c_str(), 0); // The second parameter is ignored
226#else // !__VMS && !__DARWIN__
227 int rtldFlags = 0;
228
229 if ( flags & wxDL_LAZY )
230 {
231 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
232 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
233#ifdef RTLD_LAZY
234 rtldFlags |= RTLD_LAZY;
235#else
236 wxLogDebug(_T("wxDL_LAZY is not supported on this platform"));
237#endif
238 }
239 else if ( flags & wxDL_NOW )
240 {
241#ifdef RTLD_NOW
242 rtldFlags |= RTLD_NOW;
0b9ab0bd 243#else
1948bb32
VS
244 wxLogDebug(_T("wxDL_NOW is not supported on this platform"));
245#endif
246 }
0b9ab0bd 247
1948bb32
VS
248 if ( flags & wxDL_GLOBAL )
249 {
250#ifdef RTLD_GLOBAL
251 rtldFlags |= RTLD_GLOBAL;
252#else
253 wxLogDebug(_T("RTLD_GLOBAL is not supported on this platform."));
0b9ab0bd 254#endif
1948bb32
VS
255 }
256
257 m_handle = dlopen(libname.fn_str(), rtldFlags);
258#endif // __VMS || __DARWIN__ ?
259
260#elif defined(HAVE_SHL_LOAD)
261 int shlFlags = 0;
a585ca5c 262
1948bb32
VS
263 if( flags & wxDL_LAZY )
264 {
265 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
266 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
267 shlFlags |= BIND_DEFERRED;
268 }
269 else if( flags & wxDL_NOW )
270 {
271 shlFlags |= BIND_IMMEDIATE;
272 }
273 m_handle = shl_load(libname.fn_str(), BIND_DEFERRED, 0);
274
275#elif defined(__WINDOWS__)
276 m_handle = ::LoadLibrary(libname.c_str());
277#else
278 #error "runtime shared lib support not implemented on this platform"
279#endif
280
281 if ( m_handle == 0 )
7cc98b3e 282 {
f6bcfd97 283 wxString msg(_("Failed to load shared library '%s'"));
1948bb32 284#if defined(HAVE_DLERROR) && !defined(__EMX__)
f6bcfd97 285
1948bb32
VS
286#if wxUSE_UNICODE
287 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
288 const wxChar *err = buffer;
289#else
d0166913 290 const wxChar *err = dlerror();
1948bb32
VS
291#endif
292
0b9ab0bd 293 if( err )
0b9ab0bd 294 wxLogError( msg, err );
0b9ab0bd 295#else
0b9ab0bd
RL
296 wxLogSysError( msg, libname.c_str() );
297#endif
7cc98b3e
VZ
298 }
299
1948bb32 300 return IsLoaded();
a585ca5c
KB
301}
302
1948bb32 303void wxDynamicLibrary::Unload()
752c7d6b 304{
1948bb32
VS
305 if( IsLoaded() )
306 {
307#if defined(__WXPM__) || defined(__EMX__)
308 DosFreeModule( m_handle );
309#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
310 dlclose( m_handle );
311#elif defined(HAVE_SHL_LOAD)
312 shl_unload( m_handle );
313#elif defined(__WINDOWS__)
314 ::FreeLibrary( m_handle );
315#elif defined(__WXMAC__) && !defined(__DARWIN__)
316 CloseConnection( (CFragConnectionID*) &m_handle );
317#else
318#error "runtime shared lib support not implemented"
319#endif
320 m_handle = 0;
321 }
752c7d6b
KB
322}
323
1948bb32 324void *wxDynamicLibrary::GetSymbol(const wxString &name, bool *success) const
a585ca5c 325{
1948bb32
VS
326 wxCHECK_MSG( IsLoaded(), NULL,
327 _T("Can't load symbol from unloaded library") );
328
329 bool failed = FALSE;
0b9ab0bd 330 void *symbol = 0;
a585ca5c 331
1948bb32 332#if defined(__WXMAC__) && !defined(__DARWIN__)
0b9ab0bd
RL
333 Ptr symAddress;
334 CFragSymbolClass symClass;
335 Str255 symName;
1948bb32
VS
336#if TARGET_CARBON
337 c2pstrcpy( (StringPtr) symName, name );
338#else
339 strcpy( (char *)symName, name );
340 c2pstr( (char *)symName );
341#endif
342 if( FindSymbol( dllHandle, symName, &symAddress, &symClass ) == noErr )
0b9ab0bd 343 symbol = (void *)symAddress;
7cc98b3e 344
0b9ab0bd 345#elif defined(__WXPM__) || defined(__EMX__)
1948bb32 346 DosQueryProcAddr( m_handle, 1L, name.c_str(), (PFN*)symbol );
0b9ab0bd 347
1948bb32
VS
348#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
349 symbol = dlsym( m_handle, name.fn_str() );
350
351#elif defined(HAVE_SHL_LOAD)
352 // use local variable since shl_findsym modifies the handle argument
353 // to indicate where the symbol was found (GD)
354 wxDllType the_handle = m_handle;
355 if( shl_findsym( &the_handle, name.fn_str(), TYPE_UNDEFINED, &symbol ) != 0 )
356 symbol = 0;
357
358#elif defined(__WINDOWS__)
359 symbol = (void*) ::GetProcAddress( m_handle, name.mb_str() );
b7b35e50 360
1c193821 361#else
1948bb32 362#error "runtime shared lib support not implemented"
1c193821 363#endif
0b9ab0bd 364
7b0bfbb2
VZ
365 if ( !symbol )
366 {
1948bb32
VS
367#if defined(HAVE_DLERROR) && !defined(__EMX__)
368
369#if wxUSE_UNICODE
370 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
371 const wxChar *err = buffer;
372#else
d0166913 373 const wxChar *err = dlerror();
1948bb32
VS
374#endif
375
0b9ab0bd
RL
376 if( err )
377 {
ee682a94 378 wxLogError(wxT("%s"), err);
0b9ab0bd
RL
379 }
380#else
381 failed = TRUE;
7cc98b3e
VZ
382 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
383 name.c_str());
0b9ab0bd 384#endif
7b0bfbb2 385 }
0b9ab0bd
RL
386 if( success )
387 *success = !failed;
388
7b0bfbb2 389 return symbol;
7a4b9130 390}
1948bb32 391
7a4b9130 392
1948bb32
VS
393/*static*/
394wxString wxDynamicLibrary::CanonicalizeName(const wxString& name,
395 wxDynamicLibraryCategory cat)
7a4b9130 396{
1948bb32
VS
397#ifdef __UNIX__
398 if ( cat == wxDL_MODULE )
399 return name + GetDllExt();
a585ca5c 400 else
1948bb32
VS
401 return wxString(_T("lib")) + name + GetDllExt();
402#else
403 return name + GetDllExt();
404#endif
7a4b9130 405}
8a0d4cf6 406
1948bb32
VS
407/*static*/
408wxString wxDynamicLibrary::CanonicalizePluginName(const wxString& name,
409 wxPluginCategory cat)
f11bdd03 410{
1948bb32
VS
411 wxString suffix;
412 if ( cat == wxDL_PLUGIN_GUI )
f11bdd03 413 {
a8eaaeb2
VS
414 wxAppTraits *traits = wxAppConsole::GetInstance() ?
415 wxAppConsole::GetInstance()->GetTraits() : NULL;
416 wxASSERT_MSG( traits,
417 _("can't query for GUI plugins name in console applications") );
324899f6 418 suffix = traits->GetToolkitInfo().shortName;
f11bdd03 419 }
1948bb32
VS
420#if wxUSE_UNICODE
421 suffix << _T('u');
422#endif
423#ifdef __WXDEBUG__
424 suffix << _T('d');
425#endif
f11bdd03 426
1948bb32
VS
427 if ( !suffix.empty() )
428 suffix = wxString(_T("_")) + suffix;
f11bdd03 429
1948bb32
VS
430#ifdef __UNIX__
431 #if (wxMINOR_VERSION % 2) == 0
432 #define wxDLLVER(x,y,z) "-" #x "." #y
433 #else
434 #define wxDLLVER(x,y,z) "-" #x "." #y "." #z
435 #endif
436#else
437 #if (wxMINOR_VERSION % 2) == 0
438 #define wxDLLVER(x,y,z) #x #y
439 #else
440 #define wxDLLVER(x,y,z) #x #y #z
441 #endif
442#endif
443 suffix << wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION, wxMINOR_VERSION,
444 wxRELEASE_NUMBER));
445#undef wxDLLVER
f11bdd03 446
1948bb32 447 return CanonicalizeName(name + suffix, wxDL_MODULE);
f11bdd03 448}
1948bb32
VS
449
450/*static*/
451wxString wxDynamicLibrary::GetPluginsDirectory()
f11bdd03 452{
1948bb32
VS
453#ifdef __UNIX__
454 wxString format = wxGetInstallPrefix();
455 format << wxFILE_SEP_PATH
456 << wxT("lib") << wxFILE_SEP_PATH
457 << wxT("wx") << wxFILE_SEP_PATH
458 << wxT("%i.%i");
459 wxString dir;
460 dir.Printf(format.c_str(), wxMAJOR_VERSION, wxMINOR_VERSION);
461 return dir;
462#else
463 return wxEmptyString;
464#endif
f11bdd03
GD
465}
466
f11bdd03 467
1948bb32 468#endif // wxUSE_DYNLIB_CLASS