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__
17 #if wxUSE_DYNLIB_CLASS
19 #include "wx/string.h"
20 #include "wx/dynarray.h"
22 #if defined(__OS2__) || defined(__EMX__)
23 #include "wx/os2/private.h"
27 #include "wx/msw/private.h"
30 // note that we have our own dlerror() implementation under Darwin
31 #if (defined(HAVE_DLERROR) && !defined(__EMX__)) || defined(__DARWIN__)
32 #define wxHAVE_DYNLIB_ERROR
35 class WXDLLIMPEXP_FWD_BASE wxDynamicLibraryDetailsCreator
;
37 // ----------------------------------------------------------------------------
38 // conditional compilation
39 // ----------------------------------------------------------------------------
41 // Note: __OS2__/EMX has to be tested first, since we want to use
42 // native version, even if configure detected presence of DLOPEN.
43 #if defined(__OS2__) || defined(__EMX__) || defined(__WINDOWS__)
44 typedef HMODULE wxDllType
;
45 #elif defined(HAVE_DLOPEN)
47 typedef void *wxDllType
;
48 #elif defined(HAVE_SHL_LOAD)
50 typedef shl_t wxDllType
;
51 #elif defined(__DARWIN__)
52 typedef void *wxDllType
;
53 #elif defined(__WXMAC__)
54 #include <CodeFragments.h>
55 typedef CFragConnectionID wxDllType
;
57 #error "Dynamic Loading classes can't be compiled on this platform, sorry."
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
66 wxDL_LAZY
= 0x00000001, // resolve undefined symbols at first use
67 // (only works on some Unix versions)
68 wxDL_NOW
= 0x00000002, // resolve undefined symbols on load
69 // (default, always the case under Win32)
70 wxDL_GLOBAL
= 0x00000004, // export extern symbols to subsequently
72 wxDL_VERBATIM
= 0x00000008, // attempt to load the supplied library
73 // name without appending the usual dll
74 // filename extension.
75 wxDL_NOSHARE
= 0x00000010, // load new DLL, don't reuse already loaded
76 // (only for wxPluginManager)
78 wxDL_DEFAULT
= wxDL_NOW
// default flags correspond to Win32
81 enum wxDynamicLibraryCategory
83 wxDL_LIBRARY
, // standard library
84 wxDL_MODULE
// loadable module/plugin
89 wxDL_PLUGIN_GUI
, // plugin that uses GUI classes
90 wxDL_PLUGIN_BASE
// wxBase-only plugin
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 // when loading a function from a DLL you always have to cast the returned
98 // "void *" pointer to the correct type and, even more annoyingly, you have to
99 // repeat this type twice if you want to declare and define a function pointer
102 // this macro makes this slightly less painful by allowing you to specify the
103 // type only once, as the first parameter, and creating a variable of this type
104 // called "pfn<name>" initialized with the "name" from the "dynlib"
105 #define wxDYNLIB_FUNCTION(type, name, dynlib) \
106 type pfn ## name = (type)(dynlib).GetSymbol(_T(#name))
109 // the following macros can be used to redirect a whole library to a class and
110 // check at run-time if the library is present and contains all required
113 // notice that they are supposed to be used inside a class which has "m_ok"
114 // member variable indicating if the library had been successfully loaded
116 // helper macros constructing the name of the variable storing the function
117 // pointer and the name of its type from the function name
118 #define wxDL_METHOD_NAME(name) m_pfn ## name
119 #define wxDL_METHOD_TYPE(name) name ## _t
122 // - rettype: return type of the function, e.g. "int"
123 // - name: name of the function, e.g. "foo"
124 // - args: function signature in parentheses, e.g. "(int x, int y)"
125 // - argnames: the names of the parameters in parentheses, e.g. "(x, y)"
126 // - defret: the value to return if the library wasn't successfully loaded
127 #define wxDL_METHOD_DEFINE( rettype, name, args, argnames, defret ) \
128 typedef rettype (* wxDL_METHOD_TYPE(name)) args ; \
129 wxDL_METHOD_TYPE(name) wxDL_METHOD_NAME(name); \
131 { return m_ok ? wxDL_METHOD_NAME(name) argnames : defret; }
133 #define wxDL_VOIDMETHOD_DEFINE( name, args, argnames ) \
134 typedef void (* wxDL_METHOD_TYPE(name)) args ; \
135 wxDL_METHOD_TYPE(name) wxDL_METHOD_NAME(name); \
137 { if ( m_ok ) wxDL_METHOD_NAME(name) argnames ; }
139 #define wxDL_METHOD_LOAD(lib, name) \
140 wxDL_METHOD_NAME(name) = \
141 (wxDL_METHOD_TYPE(name)) lib.GetSymbol(#name, &m_ok); \
142 if ( !m_ok ) return false
144 // ----------------------------------------------------------------------------
145 // wxDynamicLibraryDetails: contains details about a loaded wxDynamicLibrary
146 // ----------------------------------------------------------------------------
148 class WXDLLIMPEXP_BASE wxDynamicLibraryDetails
151 // ctor, normally never used as these objects are only created by
152 // wxDynamicLibrary::ListLoaded()
153 wxDynamicLibraryDetails() { m_address
= NULL
; m_length
= 0; }
155 // get the (base) name
156 wxString
GetName() const { return m_name
; }
158 // get the full path of this object
159 wxString
GetPath() const { return m_path
; }
161 // get the load address and the extent, return true if this information is
163 bool GetAddress(void **addr
, size_t *len
) const
176 // return the version of the DLL (may be empty if no version info)
177 wxString
GetVersion() const
190 friend class wxDynamicLibraryDetailsCreator
;
193 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetails
,
194 wxDynamicLibraryDetailsArray
,
197 // ----------------------------------------------------------------------------
198 // wxDynamicLibrary: represents a handle to a DLL/shared object
199 // ----------------------------------------------------------------------------
201 class WXDLLIMPEXP_BASE wxDynamicLibrary
204 // return a valid handle for the main program itself or NULL if back
205 // linking is not supported by the current platform (e.g. Win32)
206 static wxDllType
GetProgramHandle();
208 // return the platform standard DLL extension (with leading dot)
209 static const wxString
& GetDllExt() { return ms_dllext
; }
211 wxDynamicLibrary() : m_handle(0) { }
212 wxDynamicLibrary(const wxString
& libname
, int flags
= wxDL_DEFAULT
)
215 Load(libname
, flags
);
218 // NOTE: this class is (deliberately) not virtual, do not attempt
219 // to use it polymorphically.
220 ~wxDynamicLibrary() { Unload(); }
222 // return true if the library was loaded successfully
223 bool IsLoaded() const { return m_handle
!= 0; }
225 // load the library with the given name (full or not), return true if ok
226 bool Load(const wxString
& libname
, int flags
= wxDL_DEFAULT
);
228 // raw function for loading dynamic libs: always behaves as if
229 // wxDL_VERBATIM were specified and doesn't log error message if the
230 // library couldn't be loaded but simply returns NULL
231 static wxDllType
RawLoad(const wxString
& libname
, int flags
= wxDL_DEFAULT
);
233 // detach the library object from its handle, i.e. prevent the object from
234 // unloading the library in its dtor -- the caller is now responsible for
236 wxDllType
Detach() { wxDllType h
= m_handle
; m_handle
= 0; return h
; }
238 // unload the given library handle (presumably returned by Detach() before)
239 static void Unload(wxDllType handle
);
241 // unload the library, also done automatically in dtor
242 void Unload() { if ( IsLoaded() ) { Unload(m_handle
); m_handle
= 0; } }
244 // Return the raw handle from dlopen and friends.
245 wxDllType
GetLibHandle() const { return m_handle
; }
247 // check if the given symbol is present in the library, useful to verify if
248 // a loadable module is our plugin, for example, without provoking error
249 // messages from GetSymbol()
250 bool HasSymbol(const wxString
& name
) const
253 DoGetSymbol(name
, &ok
);
257 // resolve a symbol in a loaded DLL, such as a variable or function name.
258 // 'name' is the (possibly mangled) name of the symbol. (use extern "C" to
259 // export unmangled names)
261 // Since it is perfectly valid for the returned symbol to actually be NULL,
262 // that is not always indication of an error. Pass and test the parameter
263 // 'success' for a true indication of success or failure to load the
266 // Returns a pointer to the symbol on success, or NULL if an error occurred
267 // or the symbol wasn't found.
268 void *GetSymbol(const wxString
& name
, bool *success
= NULL
) const;
270 // low-level version of GetSymbol()
271 static void *RawGetSymbol(wxDllType handle
, const wxString
& name
);
272 void *RawGetSymbol(const wxString
& name
) const
274 #if defined (__WXPM__) || defined(__EMX__)
275 return GetSymbol(name
);
277 return RawGetSymbol(m_handle
, name
);
282 // this function is useful for loading functions from the standard Windows
283 // DLLs: such functions have an 'A' (in ANSI build) or 'W' (in Unicode, or
284 // wide character build) suffix if they take string parameters
285 static void *RawGetSymbolAorW(wxDllType handle
, const wxString
& name
)
299 void *GetSymbolAorW(const wxString
& name
) const
301 return RawGetSymbolAorW(m_handle
, name
);
305 // return all modules/shared libraries in the address space of this process
307 // returns an empty array if not implemented or an error occurred
308 static wxDynamicLibraryDetailsArray
ListLoaded();
310 // return platform-specific name of dynamic library with proper extension
311 // and prefix (e.g. "foo.dll" on Windows or "libfoo.so" on Linux)
312 static wxString
CanonicalizeName(const wxString
& name
,
313 wxDynamicLibraryCategory cat
= wxDL_LIBRARY
);
315 // return name of wxWidgets plugin (adds compiler and version info
318 CanonicalizePluginName(const wxString
& name
,
319 wxPluginCategory cat
= wxDL_PLUGIN_GUI
);
321 // return plugin directory on platforms where it makes sense and empty
323 static wxString
GetPluginsDirectory();
327 // common part of GetSymbol() and HasSymbol()
328 void *DoGetSymbol(const wxString
& name
, bool *success
= 0) const;
330 #ifdef wxHAVE_DYNLIB_ERROR
331 // log the error after a dlxxx() function failure
333 #endif // wxHAVE_DYNLIB_ERROR
336 // platform specific shared lib suffix.
337 static const wxString ms_dllext
;
339 // the handle to DLL or NULL
342 // no copy ctor/assignment operators (or we'd try to unload the library
344 DECLARE_NO_COPY_CLASS(wxDynamicLibrary
)
348 // ----------------------------------------------------------------------------
349 // Interesting defines
350 // ----------------------------------------------------------------------------
352 #define WXDLL_ENTRY_FUNCTION() \
353 extern "C" WXEXPORT const wxClassInfo *wxGetClassFirst(); \
354 const wxClassInfo *wxGetClassFirst() { \
355 return wxClassInfo::GetFirst(); \
358 #endif // wxUSE_DYNLIB_CLASS
360 #endif // _WX_DYNLIB_H__