1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Dynamic library loading classes
4 // Author: Guilhem Lavaux, Vadim Zeitlin, Vaclav Slavik
8 // Copyright: (c) 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_DYNLIB_H__
13 #define _WX_DYNLIB_H__
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 # pragma interface "dynlib.h"
21 #if wxUSE_DYNLIB_CLASS
23 #include "wx/string.h"
24 #include "wx/dynarray.h"
26 // FIXME: can this go in private.h or something too??
27 #if defined(__WXPM__) || defined(__EMX__)
33 #include "wx/msw/private.h"
36 #if defined(HAVE_DLERROR) && !defined(__EMX__)
37 #define wxHAVE_DYNLIB_ERROR
40 class WXDLLIMPEXP_BASE wxDynamicLibraryDetailsCreator
;
42 // ----------------------------------------------------------------------------
43 // conditional compilation
44 // ----------------------------------------------------------------------------
46 // Note: WXPM/EMX has to be tested first, since we want to use
47 // native version, even if configure detected presence of DLOPEN.
48 #if defined(__WXPM__) || defined(__EMX__) || defined(__WINDOWS__)
49 typedef HMODULE wxDllType
;
50 #elif defined(HAVE_DLOPEN)
52 typedef void *wxDllType
;
53 #elif defined(HAVE_SHL_LOAD)
55 typedef shl_t wxDllType
;
56 #elif defined(__DARWIN__)
57 typedef void *wxDllType
;
58 #elif defined(__WXMAC__)
59 #include <CodeFragments.h>
60 typedef CFragConnectionID wxDllType
;
62 #error "Dynamic Loading classes can't be compiled on this platform, sorry."
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
71 wxDL_LAZY
= 0x00000001, // resolve undefined symbols at first use
72 // (only works on some Unix versions)
73 wxDL_NOW
= 0x00000002, // resolve undefined symbols on load
74 // (default, always the case under Win32)
75 wxDL_GLOBAL
= 0x00000004, // export extern symbols to subsequently
77 wxDL_VERBATIM
= 0x00000008, // attempt to load the supplied library
78 // name without appending the usual dll
79 // filename extension.
80 wxDL_NOSHARE
= 0x00000010, // load new DLL, don't reuse already loaded
81 // (only for wxPluginManager)
83 wxDL_DEFAULT
= wxDL_NOW
// default flags correspond to Win32
86 enum wxDynamicLibraryCategory
88 wxDL_LIBRARY
, // standard library
89 wxDL_MODULE
// loadable module/plugin
94 wxDL_PLUGIN_GUI
, // plugin that uses GUI classes
95 wxDL_PLUGIN_BASE
// wxBase-only plugin
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // when loading a function from a DLL you always have to cast the returned
103 // "void *" pointer to the correct type and, even more annoyingly, you have to
104 // repeat this type twice if you want to declare and define a function pointer
107 // this macro makes this slightly less painful by allowing you to specify the
108 // type only once, as the first parameter, and creating a variable of this type
109 // called "pfn<name>" initialized with the "name" from the "dynlib"
110 #define wxDYNLIB_FUNCTION(type, name, dynlib) \
111 type pfn ## name = (type)(dynlib).GetSymbol(_T(#name))
113 // ----------------------------------------------------------------------------
114 // wxDynamicLibraryDetails: contains details about a loaded wxDynamicLibrary
115 // ----------------------------------------------------------------------------
117 class WXDLLIMPEXP_BASE wxDynamicLibraryDetails
120 // ctor, normally never used as these objects are only created by
121 // wxDynamicLibrary::ListLoaded()
122 wxDynamicLibraryDetails() { m_address
= NULL
; m_length
= 0; }
124 // get the (base) name
125 wxString
GetName() const { return m_name
; }
127 // get the full path of this object
128 wxString
GetPath() const { return m_path
; }
130 // get the load address and the extent, return true if this information is
132 bool GetAddress(void **addr
, size_t *len
) const
145 // return the version of the DLL (may be empty if no version info)
146 wxString
GetVersion() const
159 friend class wxDynamicLibraryDetailsCreator
;
162 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetails
,
163 wxDynamicLibraryDetailsArray
,
166 // ----------------------------------------------------------------------------
167 // wxDynamicLibrary: represents a handle to a DLL/shared object
168 // ----------------------------------------------------------------------------
170 class WXDLLIMPEXP_BASE wxDynamicLibrary
173 // return a valid handle for the main program itself or NULL if back
174 // linking is not supported by the current platform (e.g. Win32)
175 static wxDllType
GetProgramHandle();
177 // return the platform standard DLL extension (with leading dot)
178 static const wxChar
*GetDllExt() { return ms_dllext
; }
180 wxDynamicLibrary() : m_handle(0) { }
181 wxDynamicLibrary(const wxString
& libname
, int flags
= wxDL_DEFAULT
)
184 Load(libname
, flags
);
187 // NOTE: this class is (deliberately) not virtual, do not attempt
188 // to use it polymorphically.
189 ~wxDynamicLibrary() { Unload(); }
191 // return true if the library was loaded successfully
192 bool IsLoaded() const { return m_handle
!= 0; }
194 // load the library with the given name (full or not), return true if ok
195 bool Load(const wxString
& libname
, int flags
= wxDL_DEFAULT
);
197 // raw function for loading dynamic libs: always behaves as if
198 // wxDL_VERBATIM were specified and doesn't log error message if the
199 // library couldn't be loaded but simply returns NULL
200 static wxDllType
RawLoad(const wxString
& libname
, int flags
= wxDL_DEFAULT
);
202 // detach the library object from its handle, i.e. prevent the object from
203 // unloading the library in its dtor -- the caller is now responsible for
205 wxDllType
Detach() { wxDllType h
= m_handle
; m_handle
= 0; return h
; }
207 // unload the given library handle (presumably returned by Detach() before)
208 static void Unload(wxDllType handle
);
210 // unload the library, also done automatically in dtor
211 void Unload() { if ( IsLoaded() ) { Unload(m_handle
); m_handle
= 0; } }
213 // Return the raw handle from dlopen and friends.
214 wxDllType
GetLibHandle() const { return m_handle
; }
216 // check if the given symbol is present in the library, useful to verify if
217 // a loadable module is our plugin, for example, without provoking error
218 // messages from GetSymbol()
219 bool HasSymbol(const wxString
& name
) const
222 DoGetSymbol(name
, &ok
);
226 // resolve a symbol in a loaded DLL, such as a variable or function name.
227 // 'name' is the (possibly mangled) name of the symbol. (use extern "C" to
228 // export unmangled names)
230 // Since it is perfectly valid for the returned symbol to actually be NULL,
231 // that is not always indication of an error. Pass and test the parameter
232 // 'success' for a true indication of success or failure to load the
235 // Returns a pointer to the symbol on success, or NULL if an error occurred
236 // or the symbol wasn't found.
237 void *GetSymbol(const wxString
& name
, bool *success
= NULL
) const;
239 // low-level version of GetSymbol()
240 static void *RawGetSymbol(wxDllType handle
, const wxString
& name
);
241 void *RawGetSymbol(const wxString
& name
) const
243 #if defined (__WXPM__) || defined(__EMX__)
244 return GetSymbol(name
);
246 return RawGetSymbol(m_handle
, name
);
250 // return all modules/shared libraries in the address space of this process
252 // returns an empty array if not implemented or an error occured
253 static wxDynamicLibraryDetailsArray
ListLoaded();
255 // return platform-specific name of dynamic library with proper extension
256 // and prefix (e.g. "foo.dll" on Windows or "libfoo.so" on Linux)
257 static wxString
CanonicalizeName(const wxString
& name
,
258 wxDynamicLibraryCategory cat
= wxDL_LIBRARY
);
260 // return name of wxWidgets plugin (adds compiler and version info
263 CanonicalizePluginName(const wxString
& name
,
264 wxPluginCategory cat
= wxDL_PLUGIN_GUI
);
266 // return plugin directory on platforms where it makes sense and empty
268 static wxString
GetPluginsDirectory();
271 #if WXWIN_COMPATIBILITY_2_2
272 operator bool() const { return IsLoaded(); }
276 // common part of GetSymbol() and HasSymbol()
277 void *DoGetSymbol(const wxString
& name
, bool *success
= 0) const;
279 #ifdef wxHAVE_DYNLIB_ERROR
280 // log the error after a dlxxx() function failure
282 #endif // wxHAVE_DYNLIB_ERROR
285 // platform specific shared lib suffix.
286 static const wxChar
*ms_dllext
;
288 // the handle to DLL or NULL
291 // no copy ctor/assignment operators (or we'd try to unload the library
293 DECLARE_NO_COPY_CLASS(wxDynamicLibrary
)
297 // ----------------------------------------------------------------------------
298 // wxDllLoader: low level DLL functions, use wxDynamicLibrary in your code
299 // ----------------------------------------------------------------------------
301 #if WXWIN_COMPATIBILITY_2_2 && wxUSE_DYNAMIC_LOADER
304 wxDllLoader is a class providing an interface similar to unix's dlopen().
305 It is used by wxDynamicLibrary wxLibrary and manages the actual loading of
306 DLLs and the resolving of symbols in them. There are no instances of this
307 class, it simply serves as a namespace for its static member functions.
309 class WXDLLIMPEXP_BASE wxDllLoader
313 This function loads the shared library libname into memory.
315 libname may be either the full path to the file or just the filename in
316 which case the library is searched for in all standard locations
317 (use GetDllExt() to construct the filename)
319 if success pointer is not NULL, it will be filled with true if everything
320 went ok and false otherwise
322 static wxDllType
LoadLibrary(const wxString
& name
, bool *success
= NULL
);
325 This function unloads the shared library previously loaded with
328 static void UnloadLibrary(wxDllType dll
);
331 This function returns a valid handle for the main program
332 itself or NULL if back linking is not supported by the current platform
335 static wxDllType
GetProgramHandle() { return wxDynamicLibrary::GetProgramHandle(); }
338 This function resolves a symbol in a loaded DLL, such as a
339 variable or function name.
341 dllHandle Handle of the DLL, as returned by LoadDll().
342 name Name of the symbol.
344 Returns the pointer to the symbol or NULL on error.
346 static void *GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
= 0);
348 // return the standard DLL extension (with leading dot) for this platform
349 static wxString
GetDllExt() { return wxDynamicLibrary::GetDllExt(); }
353 wxDllLoader(); // forbid construction of objects
357 // ----------------------------------------------------------------------------
359 // ----------------------------------------------------------------------------
363 class WXDLLIMPEXP_BASE wxLibrary
: public wxObject
366 wxLibrary(wxDllType handle
);
367 virtual ~wxLibrary();
369 // Get a symbol from the dynamic library
370 void *GetSymbol(const wxString
& symbname
);
372 // Create the object whose classname is "name"
373 wxObject
*CreateObject(const wxString
& name
);
376 void PrepareClasses(wxClassInfo
*first
);
381 wxHashTable classTable
;
384 // ----------------------------------------------------------------------------
386 // ----------------------------------------------------------------------------
388 class WXDLLIMPEXP_BASE wxLibraries
394 // caller is responsible for deleting the returned pointer if !NULL
395 wxLibrary
*LoadLibrary(const wxString
& basename
);
397 wxObject
*CreateObject(const wxString
& name
);
403 // ----------------------------------------------------------------------------
405 // ----------------------------------------------------------------------------
407 extern WXDLLIMPEXP_DATA_BASE(wxLibraries
) wxTheLibraries
;
409 #endif // WXWIN_COMPATIBILITY_2_2 && wxUSE_DYNAMIC_LOADER
411 // ----------------------------------------------------------------------------
412 // Interesting defines
413 // ----------------------------------------------------------------------------
415 #define WXDLL_ENTRY_FUNCTION() \
416 extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
417 const wxClassInfo *wxGetClassFirst() { \
418 return wxClassInfo::GetFirst(); \
421 #endif // wxUSE_DYNLIB_CLASS
423 #endif // _WX_DYNLIB_H__