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(__APPLE__)
16 # pragma interface "dynlib.h"
21 #if wxUSE_DYNLIB_CLASS
23 #include "wx/string.h"
25 // FIXME: can this go in private.h or something too??
26 #if defined(__WXPM__) || defined(__EMX__)
32 #include "wx/msw/private.h"
35 // ----------------------------------------------------------------------------
36 // conditional compilation
37 // ----------------------------------------------------------------------------
39 // Note: WXPM/EMX has to be tested first, since we want to use
40 // native version, even if configure detected presence of DLOPEN.
42 #if defined(__WXPM__) || defined(__EMX__) || defined(__WINDOWS__)
43 typedef HMODULE wxDllType
;
44 #elif defined(HAVE_DLOPEN)
46 typedef void *wxDllType
;
47 #elif defined(HAVE_SHL_LOAD)
49 typedef shl_t wxDllType
;
50 #elif defined(__DARWIN__)
51 typedef void *wxDllType
;
52 #elif defined(__WXMAC__)
53 typedef CFragConnectionID wxDllType
;
55 #error "Dynamic Loading classes can't be compiled on this platform, sorry."
59 // ---------------------------------------------------------------------------
61 // ---------------------------------------------------------------------------
63 //FIXME: This class isn't really common at all, it should be moved
64 // into platform dependent files.
66 // NOTE: this class is (deliberately) not virtual, do not attempt
67 // to use it polymorphically.
71 wxDL_LAZY
= 0x00000001, // resolve undefined symbols at first use
72 wxDL_NOW
= 0x00000002, // resolve undefined symbols on load
73 wxDL_GLOBAL
= 0x00000004, // export extern symbols to subsequently
75 wxDL_VERBATIM
= 0x00000008, // Attempt to load the supplied library
76 // name without appending the usual dll
77 // filename extension.
79 wxDL_NOSHARE
= 0x00000010, // load new DLL, don't reuse already loaded
83 wxDL_DEFAULT
= wxDL_LAZY
85 wxDL_DEFAULT
= wxDL_LAZY
| wxDL_GLOBAL
89 enum wxDynamicLibraryCategory
91 wxDL_LIBRARY
, // standard library
92 wxDL_MODULE
, // loadable module/plugin
97 wxDL_PLUGIN_GUI
, // plugin that uses GUI classes
98 wxDL_PLUGIN_BASE
, // wxBase-only plugin
102 class WXDLLIMPEXP_BASE wxDynamicLibrary
105 // return a valid handle for the main program itself or NULL if
106 // back linking is not supported by the current platform (e.g. Win32)
108 static wxDllType
GetProgramHandle();
110 // return the platform standard DLL extension (with leading dot)
112 static const wxChar
*GetDllExt() { return ms_dllext
; }
114 wxDynamicLibrary() : m_handle(0) {}
115 wxDynamicLibrary(const wxString
& libname
, int flags
= wxDL_DEFAULT
)
118 Load(libname
, flags
);
120 ~wxDynamicLibrary() { Unload(); }
122 // return TRUE if the library was loaded successfully
124 bool IsLoaded() const { return m_handle
!= 0; }
126 // load the library with the given name
127 // (full or not), return TRUE on success
129 bool Load(wxString libname
, int flags
= wxDL_DEFAULT
);
131 // detach the library object from its handle, i.e. prevent the object
132 // from unloading the library in its dtor -- the caller is now
133 // responsible for doing this
134 wxDllType
Detach() { wxDllType h
= m_handle
; m_handle
= 0; return h
; }
136 // unload the library, also done automatically in dtor
140 // Return the raw handle from dlopen and friends.
142 wxDllType
GetLibHandle() const { return m_handle
; }
144 // resolve a symbol in a loaded DLL, such as a variable or function
145 // name. 'name' is the (possibly mangled) name of the symbol.
146 // (use extern "C" to export unmangled names)
148 // Since it is perfectly valid for the returned symbol to actually be
149 // NULL, that is not always indication of an error. Pass and test the
150 // parameter 'success' for a true indication of success or failure to
153 // Returns a pointer to the symbol on success, or NULL if an error
154 // occurred or the symbol wasn't found.
156 void *GetSymbol(const wxString
& name
, bool *success
= 0) const;
158 #if WXWIN_COMPATIBILITY_2_2
159 operator bool() const { return IsLoaded(); }
162 // return platform-specific name of dynamic library with proper extension
163 // and prefix (e.g. "foo.dll" on Windows or "libfoo.so" on Linux)
164 static wxString
CanonicalizeName(const wxString
& name
,
165 wxDynamicLibraryCategory cat
= wxDL_LIBRARY
);
167 // return name of wxWindows plugin (adds compiler and version info
169 static wxString
CanonicalizePluginName(const wxString
& name
,
170 wxPluginCategory cat
);
172 // return plugin directory on platforms where it makes sense and empty
174 static wxString
GetPluginsDirectory();
178 // Platform specific shared lib suffix.
180 static const wxChar
*ms_dllext
;
182 // the handle to DLL or NULL
186 // no copy ctor/assignment operators
187 // or we'd try to unload the library twice
189 DECLARE_NO_COPY_CLASS(wxDynamicLibrary
)
193 // ----------------------------------------------------------------------------
194 // wxDllLoader: low level DLL functions, use wxDynamicLibrary in your code
195 // ----------------------------------------------------------------------------
197 #if WXWIN_COMPATIBILITY_2_2 && wxUSE_DYNAMIC_LOADER
200 wxDllLoader is a class providing an interface similar to unix's dlopen().
201 It is used by wxDynamicLibrary wxLibrary and manages the actual loading of
202 DLLs and the resolving of symbols in them. There are no instances of this
203 class, it simply serves as a namespace for its static member functions.
205 class WXDLLIMPEXP_BASE wxDllLoader
209 This function loads the shared library libname into memory.
211 libname may be either the full path to the file or just the filename in
212 which case the library is searched for in all standard locations
213 (use GetDllExt() to construct the filename)
215 if success pointer is not NULL, it will be filled with TRUE if everything
216 went ok and FALSE otherwise
218 static wxDllType
LoadLibrary(const wxString
& name
, bool *success
= NULL
);
221 This function unloads the shared library previously loaded with
224 static void UnloadLibrary(wxDllType dll
);
227 This function returns a valid handle for the main program
228 itself or NULL if back linking is not supported by the current platform
231 static wxDllType
GetProgramHandle() { return wxDynamicLibrary::GetProgramHandle(); }
234 This function resolves a symbol in a loaded DLL, such as a
235 variable or function name.
237 dllHandle Handle of the DLL, as returned by LoadDll().
238 name Name of the symbol.
240 Returns the pointer to the symbol or NULL on error.
242 static void *GetSymbol(wxDllType dllHandle
, const wxString
&name
, bool *success
= 0);
244 // return the standard DLL extension (with leading dot) for this platform
245 static wxString
GetDllExt() { return wxDynamicLibrary::GetDllExt(); }
249 wxDllLoader(); // forbid construction of objects
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
259 class WXDLLIMPEXP_BASE wxLibrary
: public wxObject
262 wxLibrary(wxDllType handle
);
263 virtual ~wxLibrary();
265 // Get a symbol from the dynamic library
266 void *GetSymbol(const wxString
& symbname
);
268 // Create the object whose classname is "name"
269 wxObject
*CreateObject(const wxString
& name
);
272 void PrepareClasses(wxClassInfo
*first
);
277 wxHashTable classTable
;
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 class WXDLLIMPEXP_BASE wxLibraries
290 // caller is responsible for deleting the returned pointer if !NULL
291 wxLibrary
*LoadLibrary(const wxString
& basename
);
293 wxObject
*CreateObject(const wxString
& name
);
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 extern WXDLLIMPEXP_DATA_BASE(wxLibraries
) wxTheLibraries
;
305 #endif // WXWIN_COMPATIBILITY_2_2 && wxUSE_DYNAMIC_LOADER
307 // ----------------------------------------------------------------------------
308 // Interesting defines
309 // ----------------------------------------------------------------------------
311 #define WXDLL_ENTRY_FUNCTION() \
312 extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
313 const wxClassInfo *wxGetClassFirst() { \
314 return wxClassInfo::GetFirst(); \
317 #endif // wxUSE_DYNLIB_CLASS
319 #endif // _WX_DYNLIB_H__